【求助】mscomm接收串口数据程序死机的问题!
Private Sub Form_Load()
With ctrMSComm
.CommPort = 3
.Settings = "38400,N,8,1"
.InBufferSize = 4096
.RThreshold = 1
.InBufferCount = 0
.InputLen = 0
End With
If ctrMSComm.PortOpen = False Then
ctrMSComm.PortOpen = True
If Err Then
MsgBox (Err.Description)
Exit Sub
End If
End If
End Sub
Private Sub ctrMSComm_OnComm()
Dim bytInput() As Byte
Dim intInputLen As Integer
Select Case Form1.ctrMSComm.CommEvent
Case comEvReceive
'此处添加处理接收的代码
Form1.ctrMSComm.InputMode = comInputModeBinary
intInputLen = Form1.ctrMSComm.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Form1.ctrMSComm.Input
Call InputManage(bytInput, intInputLen)
Call GetDisplayText
End Select
End Sub
Public bytReceiveByte() As Byte '接收到的字节
Public intReceiveLen As Integer '接收到的字节数
Public strHex As String '十六进制编码
'***********************************
'为输出准备文本
'保存在全局变量
'strText
'strHex
'strAddress
'总行数保存在
'intLine
'***********************************
Public Sub GetDisplayText()
Dim n As Integer
Dim intValue As Integer
Dim intHighHex As Integer
Dim intLowHex As Integer
strHex = "" '设置初值
'*****************************************
'获得16进制码
'*****************************************
For n = 1 To intReceiveLen
intValue = bytReceiveByte(n - 1)
intHighHex = intValue \ 16
intLowHex = intValue - intHighHex * 16
If intHighHex < 10 Then
intHighHex = intHighHex + 48
Else
intHighHex = intHighHex + 55
End If
If intLowHex < 10 Then
intLowHex = intLowHex + 48
Else
intLowHex = intLowHex + 55
End If
strHex = strHex + " " + Chr$(intHighHex) + Chr$(intLowHex) + " "
If (n Mod 8) = 0 Then '设置换行
strHex = strHex + Chr$(13) + Chr$(10)
End If
Form1.Text1.Text = strHex
Next n
End Sub
'**********************************
'输入处理
'处理接收到的字节流,并保存在全局变量
'bytReceiveRyte()
'**********************************
Public Sub InputManage(bytInput() As Byte, intInputLenth As Integer)
Dim n As Integer '定义变量及初始化
ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)
For n = 1 To intInputLenth Step 1
bytReceiveByte(intReceiveLen + n - 1) = bytInput(n - 1)
Next n
intReceiveLen = intReceiveLen + intInputLenth
End Sub