vb.net2010十六进制读取串口的问题
我没有使用代理来读取串口数据.以下代码收取字符串是正常的,但十六进制不会写,新手求指教....'触发接收事件PublicSubSp_DataReceived(ByVals...
我没有使用代理来读取串口数据.以下代码收取字符串是正常的,但十六进制不会写,新手求指教.... '触发接收事件
Public Sub Sp_DataReceived(ByVal sender As
Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles
SerialPort1.DataReceived
Me.Invoke(New EventHandler(AddressOf
Sp_Receiving)) '调用接收数据函数
End Sub
'接收数据
Private Sub
Sp_Receiving(ByVal sender As Object, ByVal e As EventArgs)
Try
If InCheckBox.CheckState = CheckState.Checked Then
'如果十六进制读取被勾选则显示为十六进制
Dim inDataLen As Integer =
SerialPort1.BytesToRead() '读取缓存区字节数
If inDataLen > 0
Then
Dim inBytes(inDataLen - 1) As Byte, bytes As
Byte
Dim strHex As String = ""
SerialPort1.Read(inBytes, 0, inDataLen)
For Each bytes In
inBytes
strHex = strHex + [String].Format("{0:X2} ",
bytes)
Next
receivebytes.Text
=这里的结果应该是什么?............................................................................?
BarCountRx.Text = (Val(BarCountRx.Text) +
inDataLen).ToString '接收字节计数能正确计数
End If
Else
'如果没有勾选则显示为字符串
Dim strIncoming As String
'用来存储收到的缓存
receivebytes.Text = Str(Val(receivebytes.Text) +
SerialPort1.BytesToRead)
If SerialPort1.BytesToRead > 0
Then
Threading.Thread.Sleep(100)
'添加的延时
strIncoming = SerialPort1.ReadExisting.ToString
'读取缓冲区中的数据
SerialPort1.DiscardInBuffer()
receivebytes.Text =
strIncoming
BarCountRx.Text = (Val(BarCountRx.Text) +
strIncoming.Length).ToString '接收字节计数
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub 展开
Public Sub Sp_DataReceived(ByVal sender As
Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles
SerialPort1.DataReceived
Me.Invoke(New EventHandler(AddressOf
Sp_Receiving)) '调用接收数据函数
End Sub
'接收数据
Private Sub
Sp_Receiving(ByVal sender As Object, ByVal e As EventArgs)
Try
If InCheckBox.CheckState = CheckState.Checked Then
'如果十六进制读取被勾选则显示为十六进制
Dim inDataLen As Integer =
SerialPort1.BytesToRead() '读取缓存区字节数
If inDataLen > 0
Then
Dim inBytes(inDataLen - 1) As Byte, bytes As
Byte
Dim strHex As String = ""
SerialPort1.Read(inBytes, 0, inDataLen)
For Each bytes In
inBytes
strHex = strHex + [String].Format("{0:X2} ",
bytes)
Next
receivebytes.Text
=这里的结果应该是什么?............................................................................?
BarCountRx.Text = (Val(BarCountRx.Text) +
inDataLen).ToString '接收字节计数能正确计数
End If
Else
'如果没有勾选则显示为字符串
Dim strIncoming As String
'用来存储收到的缓存
receivebytes.Text = Str(Val(receivebytes.Text) +
SerialPort1.BytesToRead)
If SerialPort1.BytesToRead > 0
Then
Threading.Thread.Sleep(100)
'添加的延时
strIncoming = SerialPort1.ReadExisting.ToString
'读取缓冲区中的数据
SerialPort1.DiscardInBuffer()
receivebytes.Text =
strIncoming
BarCountRx.Text = (Val(BarCountRx.Text) +
strIncoming.Length).ToString '接收字节计数
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub 展开
2个回答
展开全部
不是很明白你的题意
strHex = strHex + [String].Format("{0:X2} "
这里的意思是把每个字节数据转换成了十六进制,每个字节返中梁占两个字符
如果你串口收到的4个字节数据:43,27,56,200
那么你的结果是:2B1B38C8
即receivebytes.Text="培隐2B1B38C8"
不足两位的补0
如果你串口收到的4个字漏运节数据:3,27,56,200
那么你的结果是:031B38C8
即receivebytes.Text="031B38C8"
strHex = strHex + [String].Format("{0:X2} "
这里的意思是把每个字节数据转换成了十六进制,每个字节返中梁占两个字符
如果你串口收到的4个字节数据:43,27,56,200
那么你的结果是:2B1B38C8
即receivebytes.Text="培隐2B1B38C8"
不足两位的补0
如果你串口收到的4个字漏运节数据:3,27,56,200
那么你的结果是:031B38C8
即receivebytes.Text="031B38C8"
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询