我正在尝试使用串行端口读取23字节的十六进制字符串 [英] I'm Trying to read a 23 byte hex string Using Serial Port
问题描述
我正在尝试创建一个可以从串口读取23字节十六进制字符串的应用程序并剪切一些字节来显示 进入一个文本框。
I'm trying to make an application that can read a 23 byte hex string from the serial port and cut some bytes to display into a text box.
像thie:501004 185555E13301005。 = 35 30 31 30 30 34 20 31 38 35 35 35 35 45 31 33 33 30 31 30 30 35 14
like thie : 501004 185555E13301005. = 35 30 31 30 30 34 20 31 38 35 35 35 35 45 31 33 33 30 31 30 30 35 14
我要显示5555E1301005
i want to display 5555E1301005
但不起作用,因为我无法在十二月削减最后一个字节(14)
but not work because i can not cut last byte (14) in dec
谢谢。
抱歉我的英语不好
以下是我的代码:
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
Private Data As New System.Text.StringBuilder
Private Sub ReceivedText(ByVal [text] As String)
If Me.TextBox1.InvokeRequired Then
Dim x As New SetTextCallBack(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
' Me.TextBox1.Text &= [text] '' Debug
Data.Append(text)
Dim StartIndex As Integer = Data.ToString.IndexOf("8")
If StartIndex > -1 Then
Dim EndIndex As Integer = Data.ToString.IndexOf(".", StartIndex)
If EndIndex > -1 Then
TextBox1.Text &= Data.ToString.Substring(StartIndex + 1, 12).Trim & vbNewLine
Data.Remove(0, EndIndex + 1)
End If
End If
Dim sent(0) As Byte ' for sent ack to device
sent(0) = &H6 ' for sent ack to device
SerialPort1.Write(sent, 0, sent.Length) ' for sent ack to device
End If
End Sub
推荐答案
但不起作用,因为我无法在dec中删除最后一个字节(14)
but not work because i can not cut last byte (14) in dec
如果数据包含字符无法显示为文字,则不应使用字符串。 数据应作为字节数组接收和处理,例如使用Read或ReadByte。 您可以在数组中搜索所需的
字节值。 修剪非显示字符后,可以将字节数组转换为字符串。
If the data includes characters that cannot display as text, then you should not use strings. The data should be received and processed as a byte array, for instance with Read or ReadByte. You can search the array for the byte values that you need. After you have trimmed the non-displaying characters you can convert the byte array to string.
这篇关于我正在尝试使用串行端口读取23字节的十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!