VB2005十六进制数据发送问题
VB2005用serialport与下位机进行串口数据通信,其请求方式是发送十六进制的“12 34 56 78 0D 0A”,如何发送十六进制,现在只能发送字符串
--------------------编程问答--------------------
LZ:参考我的资源:"VB.NET2008的串口工程"
--------------------编程问答--------------------
谢谢你的回复,但是你给的连接打不开
--------------------编程问答--------------------
现在那个链接连不上,贴上代码:
Public Class Form1
Dim mSTR As String
Dim strData As String
Dim strDat As String
Dim mRecvByte() As Byte
Dim Hexsj As String
Dim form2 As Form
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim mSize As Integer = Me.SerialPort1.BytesToRead
ReDim mRecvByte(mSize - 1)
SerialPort1.Read(mRecvByte, 0, mSize)
BeginInvoke(New EventHandler(AddressOf ONMLoadA), SerialPort1.BytesToRead()) '使用委托方式显示接收到的字符串
End Sub
Sub ONMLoadA(ByVal sender As System.Object, ByVal e As System.EventArgs) '委托
strDat = ""
TextBox1.Text = ""
Dim i As Integer
For i = 0 To UBound(mRecvByte) 'mSize - 1
strData = strData & IIf(mRecvByte(i) > 15, Hex(mRecvByte(i)), "0" & Hex(mRecvByte(i))) & " "
Next
Dim sj As Byte
For i = 1 To Len(strData) Step 3 '处理为ASCII字符
sj = Val("&H" & Mid(strData, i, 2))
If sj < 32 Or sj > 128 Then '当接收字节中有Chr(0)时,其后字符被切割
strDat = strDat & "."
Else
strDat = strDat & Chr(sj)
End If
Next
TextBox1.Text = strDat '显示字符
TextBox2.Text = strData '显示为16进制
TextBox3.Text = Len(strData) / 3
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.BaudRate = 9600 '波特率设为
SerialPort1.PortName = "com1" '端口为串口COM1
SerialPort1.ReceivedBytesThreshold = 1
If SerialPort1.IsOpen = False Then SerialPort1.Open() '打开串口
Hexsj = "002702013524200239892700020020010201E9030D07"
TxtSend.Text = Hexsj
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
strData = ""
strDat = ""
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim bytHex() As Byte
ReDim bytHex((Len(Hexsj) \ 2) - 1)
Dim i As Integer
For i = 1 To Len(Hexsj) Step 2
bytHex((i - 1) / 2) = Val("&H" & Mid(Hexsj, i, 2))
Next
SerialPort1.Write(bytHex, 0, bytHex.Length)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim bytHex(0) As Byte
bytHex(0) = &HC3
SerialPort1.Write(bytHex, 0, bytHex.Length)
End Sub
End Class
补充:VB , 网络编程