串口通信设计问题
我现在有一个功能需求是这样的。有一个MSComm控件、读取数据的代码如下
Private Sub MSComm1_OnComm()以为我有一个判断刷卡10次的需求。但是我发现。这个testCount 变量变化的幅度很大。我只操作一次硬件设备。会加N次。、如果我单点、一步步走下去。确实正常的。难道是要清楚什么缓存吗?MSComm1.InBufferCount = 0但是这个我也设置过啊。有谁能指点下吗?怎么实现好
Dim strBuff As String
Dim BytReceived() As Byte
Select Case MSComm1.CommEvent '事件发生
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
If (strBuff <> Null Or strBuff <> "") Then
If (Option1.Value = True) Then
result = strBuff
Text2.Text = Text2.Text + result
End If
If (Option2.Value = True) Then
BytReceived() = strBuff
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
result = result & "0" & Hex(BytReceived(i)) & " " '如果只有一个字符,则前补0,如F显示0F,最后补空格
Else
result = result & Hex(BytReceived(i)) & " " '方便显示观察如: 00 0F FE
End If
Next
'判断刷10次逻辑
If (testFlag = True And UBound(BytReceived) > 0) Then
testCount = testCount + 1
Label5.Caption = "刷卡成功次数:" + CStr(testCount) + " 失败次数:0" + " 成功率:100%"
If (testCount Mod 10 = 0) Then
Label5.Caption = "已经测试了10次"
End If
End If
'
Text2.Text = result
End If
End If
End Select
End Sub