有几点建议:
1、在comm.CommEvent事件中不要使用MsgBox,因为它是独占任务模态,会使串口接收遗漏,可改用DEBUG.PRINT ...;
2、Print #1, strReceive改为Print #1, strReceive;,就无回车换行;
3、接收字符串若是定长的,可考虑用inbuffercount判断。
Select Case comm.CommEvent
Case comEvReceive
strEndChar = Chr(3)
'判断是否接收到chr(3)
strTemp = comm.Input
txtRep.Text = strTemp
txtRep.Refresh
lCount = InStr(1, strTemp, strEndChar)
On Error GoTo ErrHandle
If lCount > 0 Then
debug.print lCount
strReceive = strReceive + strTemp
debug.print strReceive
Print #1, strReceive;
strReceive = " "
Else
strReceive = strReceive + strTemp
debug.print "strREceive is:::::" & strREceive
End If
End Select
1、在comm.CommEvent事件中不要使用MsgBox,因为它是独占任务模态,会使串口接收遗漏,可改用DEBUG.PRINT ...;
2、Print #1, strReceive改为Print #1, strReceive;,就无回车换行;
3、接收字符串若是定长的,可考虑用inbuffercount判断。
Select Case comm.CommEvent
Case comEvReceive
strEndChar = Chr(3)
'判断是否接收到chr(3)
strTemp = comm.Input
txtRep.Text = strTemp
txtRep.Refresh
lCount = InStr(1, strTemp, strEndChar)
On Error GoTo ErrHandle
If lCount > 0 Then
debug.print lCount
strReceive = strReceive + strTemp
debug.print strReceive
Print #1, strReceive;
strReceive = " "
Else
strReceive = strReceive + strTemp
debug.print "strREceive is:::::" & strREceive
End If
End Select
接收语句应当在一个循环中,反复检测是否接收完毕。在一般的通讯协议中,要在数据分组的头部发送分组长度。接收方根据长度判断是否接收完毕一个分组。如果在你的协议中没有数据长度字段,可以检测接收数据的最后字符是否chr(3)。如果是,取回数据,清空串口输入缓冲区。
如何判断在接收buffer中收到chr(3),我查了mscomm控件好像没有。
另外发送过来的数据是变长的ASCII码流,所以它在最后附上CHR(3)。to lilimaoming(李)
多谢你的热心,分数我一定会给的,不够可再加,
我又试了一下,确实存了最后一次收到的STRING,不知为什么?
我试过用write #但是每次输出的字符串用双引号标注起来,
而这是我所不希望的,所以没用