我正在从收发器将温度值读取到串行端口中,并且我希望该值更改我的 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