[英]receive data from serial port using VB.NET, encoding pb?
在VB.NET中,我嘗試與串行端口上的設備進行通信,這是預期的結果:
ASCII (設備正確回答):
每秒發送<ABC000>以保持通信。
在Hexa(OKAY):
我試過了
Public myComPort As New SerialPort
...
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each portname As String In My.Computer.Ports.SerialPortNames
myComPort.PortName = portname
myComPort.BaudRate = 4800
myComPort.Parity = Parity.None
myComPort.DataBits = 8
myComPort.StopBits = StopBits.One
myComPort.ReadTimeout = 5000
myComPort.WriteTimeout = 2000
myComPort.Open()
AddHandler myComPort.DataReceived, SerialDataReceivedEventHandler
aTimer.Enabled = True
Next
End sub
' Define a delegate class to handle DataReceived events.
Friend Delegate Sub SerialDataReceivedEventHandlerDelegate(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
' Create an instance of the delegate.
Private SerialDataReceivedEventHandler As New SerialDataReceivedEventHandler(AddressOf DataReceived)
Friend Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
Dim bytesNber As Integer = myComPort.BytesToRead
Dim hex As String = ""
Dim hex2 As String = ""
Dim str As String = ""
Do
Dim ReceiveBuf() As Byte = New Byte() {}
Array.Resize(ReceiveBuf, bytesNber)
myComPort.Read(ReceiveBuf, 0, bytesNber)
str &= Bytes_To_String2(ReceiveBuf)
hex &= HexToString(str)
hex2 &= bytetohex(ReceiveBuf)
Loop While myComPort.BytesToRead <> 0
WriteLog("test1:" & str)
WriteLog("test2:" & hex)
WriteLog("test2b:" & hex2)
WriteLog("test3:" & HexToString(str))
End Sub
'---------------------------------------------------------
Private Function Bytes_To_String2(ByVal bytes_Input As Byte()) As String
Dim strTemp As New StringBuilder(bytes_Input.Length * 2)
For Each b As Byte In bytes_Input
strTemp.Append(Conversion.Hex(b))
Next
Return strTemp.ToString()
End Function
Function HexToString(ByVal hex As String) As String
Dim text As New System.Text.StringBuilder(hex.Length \ 2)
For i As Integer = 0 To hex.Length - 2 Step 2
text.Append(Chr(Convert.ToByte(hex.Substring(i, 2), 16)))
Next
Return text.ToString
End Function
Public Function bytetohex(buffer() As Byte) As String
bytetohex = vbNullString
For i = 0 To UBound(buffer)
bytetohex = bytetohex & Hex(buffer(i))
Next i
End Function
'---------------------------------------------------------
計時器保持通訊:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Create a timer and set a one second interval.
aTimer = New System.Timers.Timer()
aTimer.Interval = 900
' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
End Sub
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
BeginInvoke(Sub()
WriteLog("ABC000")
myComPort.WriteLine("ABC000")
End Sub)
End Sub
但是我得到了:
我也嘗試過:
myComPort.Encoding = System.Text.Encoding.ASCII
與:
Friend Sub DataReceived (...)
txtDataReceived.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
End Sub
Public Sub updateTextBox()
txtDataReceived.AppendText(myComPort.ReadExisting)
End Sub
結果:
任何想法?
在您的DataReceived
函數中,代替這個:
Dim ReceiveBuf() As Byte = New Byte() {}
Array.Resize(ReceiveBuf, bytesNber)
myComPort.Read(ReceiveBuf, 0, bytesNber)
str &= Bytes_To_String2(ReceiveBuf)
做這個:
str = myComPort.ReadExisting()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.