我们正在尝试从称重机中获得重量。
在使用COM1在Windows XP中读取Hyper-terminal时,我们得到了 正确的价值观。但是在Visual Basic 2013中读取数据(在Windows 10中 Pro)在我的笔记本电脑上使用USB到COM端口转换器电缆,我们正在获得 不同的值,如x,?,3,&等等。请提出解决方案 我们可以通过代码获得正确的数据。
我们的守则是:
With Me.mvSerialPort
.PortName = "COM11" ' as shown in device manager
.BaudRate = 2400
.Parity = Parity.None
.StopBits = StopBits.One
.DataBits = 8
.Handshake = Handshake.None
.DtrEnable = True
.RtsEnable = True
.NewLine = vbCrLf
.ReadTimeout = 1000
.WriteTimeout = 1000
End With
'Function Code
Private Function ReadCOM() As String
Do
If Me.mvThread_Stop = True Then Exit Do
Try
Try
If Me.mvSerialPort.IsOpen Then
Me.mvSerialPort.Close()
End If
Catch ex As Exception
'
End Try
Try
Me.mvSerialPort.Open()
If Me.mvSerialPort.IsOpen Then
'Thread.Sleep(200)
Thread.Sleep(Val(Me.TextBox4.Text))
Dim svMessage As String = ""
Try
'svMessage = Me.mvSerialPort.ReadLine()
svMessage = Me.mvSerialPort.ReadExisting
updateStatus(svMessage)
Catch ex As Exception
'Me.mvThread_Stop = True
End Try
End If
Me.mvSerialPort.Close()
Catch ex As Exception
Me.mvSerialPort.Close()
'
End Try
Catch ex As TimeoutException
'updateStatus(ex.ToString)
End Try
Loop
Return ""
End Function
Public Delegate Sub updateStatusDelegate(ByVal newStatus As String)
Public Sub updateStatus(ByVal newStatus As String)
Try
If Me.InvokeRequired Then
Dim upbd As New updateStatusDelegate(AddressOf updateStatus)
Me.Invoke(upbd, New Object() {newStatus})
Else
Dim svSplit() As String = Split(newStatus, " ")
Me.TextBox4.Text = svSplit(svSplit.Count - 1)
RichTextBox1.Text = newStatus & vbCrLf & RichTextBox1.Text
End If
Catch ex As Exception
'
End Try
End Sub