设置MSCOMM控件的属性InputMode = comInputModeBinary 用BYTE数组接收数据。Option Explicit Dim strData As String Dim BytReceived() As BytePrivate Sub MSComm1_OnComm() '接收数据 Dim strBuff As String Dim i As Integer Select Case MSComm1.CommEvent Case 2 MSComm1.InputLen = 0 strBuff = MSComm1.Input BytReceived() = strBuff For i = 0 To UBound(BytReceived) '接收数据处理为16进制 If Len(Hex(BytReceived(i))) = 1 Then strData = strData & "0" & Hex(BytReceived(i)) Else strData = strData & Hex(BytReceived(i)) End If Next Text1 = strData '写数据处理代码 End Select End SubPrivate Sub Form_Load() MSComm1.Settings = "9600,N,8,1" MSComm1.RThreshold = 1 MSComm1.PortOpen = True End Sub
如果要用字符串,多半都是ASCII或Unicode码
然后转换一下就可以了。看看这个函数 StrConv
用BYTE数组接收数据。Option Explicit
Dim strData As String
Dim BytReceived() As BytePrivate Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Dim i As Integer
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
For i = 0 To UBound(BytReceived) '接收数据处理为16进制
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next
Text1 = strData
'写数据处理代码
End Select
End SubPrivate Sub Form_Load()
MSComm1.Settings = "9600,N,8,1"
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub