请教串口的问题
Private Sub ctrMSComm_OnComm()Dim i, nInPutLen As Long
Dim bytInputByte As Variant '接收帧的变量数组
Dim arr(1) As Byte
Select Case '选择通讯事件或错误
Case comEvReceive '接收事件
If gbRecvFullFrame Then Exit Sub '接收完整的数据帧,退出接收
nInPutLen = ctrMSComm.InBufferCount 'nInPutLen值为接收缓冲区数据大小
'ReDim bytRecvByte(nInPutLen) As Byte
While ctrMSComm.InBufferCount > 0
bytInputByte = ctrMSComm.Input '接收收缓冲区中的数据写入数组,删除缓冲区里的数据
bytRecvByte(nRecvFrameLen) = bytInputByte(0) 'ctrMSComm.Input
'判断帧头
If nRecvFrameLen = 0 And bytRecvByte(nRecvFrameLen) = &H62 Then
bHeadFound = True
End If
'判断帧尾
If bHeadFound And nRecvFrameLen = bytRecvByte(1) + 3 And _
bytRecvByte(nRecvFrameLen) = &H20 Then
bTailFound = True
End If
'数据区数据求和
If bHeadFound And bTailFound Then
nCheckCount = 0
For i = 1 To bytRecvByte(1) + 1
nCheckCount = nCheckCount + bytRecvByte(i)
Next i
'求效验和,CopyMemory函数 去字符组最后两个字节
' M判断是否和效验和一致
CopyMemory arr(0), nCheckCount, 2
If arr(0) = bytRecvByte(bytRecvByte(1) + 2) Then
gbRecvFullFrame = True
Else
gbRecvFullFrame = False
End If
bHeadFound = False
bTailFound = False
nRecvFrameLen = 0
Exit Sub
End If
nRecvFrameLen = nRecvFrameLen + 1
Wend
End Select
End Sub
bytRecvByte 是全局变量 大小为 bytRecvByte(4096) 在程序中怎么老 下标溢出
问题出在帧接收错误没有清除错误的帧,请教高手怎么清除错误的帧