MSComm1.Output = "teststart" + Chr$(13)
'For i = 1 To 20000000
'Next
result = MSComm1.Input
Do while result = ""
Loop
result = CInt(result)
If result < 36 Then
......
else
......
End If "teststart"为一个字符串命令,命令连在串口上的仪表开始测试,将会有很多数据从串口返回。
如果我加上被注释掉的
'For i = 1 To 20000000
'Next这段等待,程序就正确了
如果去掉,就陷入了死循环,证明我监测串口是否有数据上来的写法:
Do while result = ""
Loop
是错误的,有人知道该怎么写么?
Private Sub MSComm1_OnComm()
Dim GetChar As String
Select Case MSComm1.CommEvent
Case comEvReceive
GetChar = MSComm1.Input
...
Case Else
Exit Sub
End Select
或者:
Dim GetChar As String
...
MSComm1.InputLen = 0
do while(mscomm1.CommEvent=comEvReceive)
If MSComm1.InBufferCount Then
GetChar = MSComm1.Input
End If
...
doevents
loopEnd Sub
如果想用循环
Dim GetChar As String
...
MSComm1.InputLen = 0
do while(...)
If MSComm1.InBufferCount Then
GetChar = MSComm1.Input
End If
...
doevents
loopEnd Sub
或者:
Dim GetChar As String
...
MSComm1.InputLen = 0
do while(mscomm1.CommEvent ...)
If MSComm1.InBufferCount Then
GetChar = MSComm1.Input
End If
...
doevents
loopEnd Sub
2.你的for是用来做延时用的吧?用循环做延时误差很大,建议你用API函数Sleep;
3.其他的楼上两位都已经说了。
Do while result = ""
Loop
而无
doevents
会独占任务,而不能干其它事
况且你的
result = MSComm1.Input
又是在发了之后就立即只读一次,又不知有否。
按 zhp80的用oncomm去做吧。
if MSComm.InBufferCount then
mscomm.input
.......
endif
Do while result = ""
Loop
不对Do
result =result & MSComm1.Input
Loop until len(result)>="你需要的长度"欢迎使用我的串口测试软件
www.tsfigure.com.cn
若需要延时只能用timer控件,用sleep则整个程序都停止运行,更接收不到数据.