近来用VB调试串口通信,通过MSComm控件,遇到一个奇怪的问题:
在接收到一个字节的时候会触发OnComm事件,但是触发此事件之后不能进入 Case comEvReceive 这个判断接收的程序段里面,
已经排除了通讯错误的可能性,单片机发送的数据也正常,百思不得其解不知道为什么?求高手指导,谢谢
下面为OnComm事件的内容:
Private Sub MSComm1_OnComm()
Dim buffer As String
Dim inbyte() As Byte
Dim num1 As Integer
On Error GoTo Err
Select Case MSComm1.CommEvent
Case comEvReceive ‘接收数据处理,comevSend为发送数据,comEventRxParity为对奇偶校验错误进行处理
MSComm1.InputLen = 0 ‘一次性全部读入
MSComm1.RThreshold = 0 ‘禁止生成事件
inbyte = MSComm1.Input
Label8.Caption = 12 ‘检测是否接收到数据
For num1 = LBound(inbyte) To UBound(inbyte)
buffer = buffer + Hex(inbyte(num1)) +chr(32)
Next num1
End Select
‘Label8.Caption = num
‘可能需要将采集到的四位数据整合起来 见调试结果
‘获取十进制测量电压数据
‘If Len(Trim(Mid(buffer, 1, 2))) = 1 Then
‘datatemp(num) = Val("&H" &mid(buffer, 3, 2) & Str("0") &mid(buffer, 1, 2)) * 0.001
‘Else
‘datatemp(num) = Val("&H" &mid(buffer, 3, 2) &mid(buffer, 1, 2)) * 0.001
‘End If
‘显示测量值
collect_time = Label12.Caption
If datatemp(num) >= 0 Then
collect_value = Format$(datatemp(num), "0.000") ‘十进制数字,保留三位小数
num = num + 1
Cells(num + 1, 1) = collect_time ‘将数据写入excel表格
Cells(num + 1, 2) = collect_value
‘Call saveExcel
Label14.Caption = collect_time ‘显示出当前数据
Label13.Caption = collect_value
End If
‘MSComm1.RThreshold = 1
Err:
End Sub