Option Explicit
Dim sTemp As String
Dim yTemp() As Byte
Dim Hsum As Integer
Dim strData As String
Dim intSj() As Integer
Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.CommPort = 1
MSComm1.InputMode = comInputModeBinary '二进读取制模式
MSComm1.RThreshold = 1
MSComm1.SThreshold = 0
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim ss As Variant
Dim ww As String '定义临时变量
Dim i, a As Integer
Select Case MSComm1.CommEvent 'Select Case 语句分解事件
Case comEvReceive
ss = MSComm1.Input
sTemp = StrConv(ss, vbUnicode) '转换为Unicode
yTemp = StrConv(sTemp, vbFromUnicode) '转换为Byte
Hsum = UBound(yTemp)
Dim j As Integer
For j = 0 To Hsum '转换为16进制
If Len(Hex(yTemp(j))) = 1 Then
strData = strData & "0" & Hex(yTemp(j))
Else
strData = strData & Hex(yTemp(j))
End If
Next
zh '转换为Integer
End Select
End Sub
Public Sub zh()
Dim l As Integer
ReDim intSj((Len(strData) / 4) - 1)
For l = 1 To Len(strData) Step 4
intSj((l - 1) / 4) = Val("&H" & Mid(strData, l, 4))
Print intSj(l / 4)
Next l
End Sub