【发布时间】:2012-05-24 23:15:27
【问题描述】:
我正在将一个温度值从收发器读取到串行端口中,并且我希望这个值能够更改我的 Visual Basic 表单中标签的值。这个值每隔几秒就会改变一次。我正在使用下面的代码:
Me.dataReceived.Text &= [text]
dataReceived 是我使用的标签,[text] 是我从串口读取的数据。这会导致显示数据,但不是覆盖标签,而是一个接一个地写入值。 (数据已附加)。我试图在 = 之前删除 & 但这没有用,因为什么也没出现。关于我能做什么的任何想法?
我使用的代码如下:
'To receive data into the text field
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
While (SerialPort1.IsOpen)
ReceivedText(SerialPort1.ReadExisting()) 'This is called automatically every time data is received at the Serial Port
End While
End Sub
Private Sub ReceivedText(ByVal [text] As String)
Dim temperature As String
'This function compares the creating Thread's ID with the calling Thread's ID
If Me.dataReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
'To output to the text box:
Me.dataReceived.Text = text
'To output to the label
temperature = text
Me.temp.Text = text
hmd.Text = temperature
End If
End Sub
【问题讨论】:
-
&= 符号表示追加,= 应该可以工作
-
尝试删除参数中文本周围的方括号。你有 Option Strict 吗?
-
@TamaraCaligari - Option Strict On(后果自负)