vb mscomm1串口通信接收发送十六进制数据

前端之家收集整理的这篇文章主要介绍了vb mscomm1串口通信接收发送十六进制数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Private Sub Command1_Click()
Dim OutBuffer() As Byte
Dim tem As Variant
Dim e As Integer
Dim q As Integer
Dim LenOfText As Integer
LenOfText = Len(txtSum.Text) / 2 - 1
ReDim OutBuffer(LenOfText)

If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If

q = 0
For e = 1 To Len(txtSum.Text) Step 2
tem = Mid(txtSum.Text,e,2)
OutBuffer(q) = Val("&H" & tem)
Debug.Print Val("&H" & tem)
q = q + 1
Next

MSComm1.Output = OutBuffer

End Sub

Private Sub Form_Load()

MSComm1.CommPort = 1MSComm1.Settings = "19200,N,8,1"MSComm1.InputMode = comInputModeBinaryMSComm1.InputLen = 0MSComm1.InBufferSize = 1024MSComm1.OutBufferSize = 512MSComm1.PortOpen = TrueMSComm1.SThreshold = 0MSComm1.RThreshold = 1MSComm1.InBufferCount = 0MSComm1.OutBufferCount = 0End SubPrivate Sub MSComm1_OnComm()On Error Resume NextDim BytesReceived() As ByteDim buffer As StringDim HData As StringDim i As IntegerSelect Case MSComm1.CommEventCase comEvReceive '接收十六进制数据。并以十六进制显示MSComm1.InputLen = 0buffer = MSComm1.Input '接收数据至字符串中BytesReceived() = buffer '将数据转入Byte数组中For i = 0 To UBound(BytesReceived) '显示结果以十六进制显示If Len(Hex(BytesReceived(i))) = 1 ThenHData = HData & "0" & Hex(BytesReceived(i))ElseHData = HData & Hex(BytesReceived(i))End IfText1.Text = HData'最后将结果后入Text1中MSComm1.OutBufferCount = 0 '清除发送缓冲区MSComm1.InBufferCount = 0 '清除接收缓冲区MSComm1.PortOpen = FalseNextEnd SelectEnd Sub

原文链接:https://www.f2er.com/vb/261401.html

猜你在找的VB相关文章