我的经验是,让inputlen=0,然后根据InbufferCount来决定是否读数据 最好加上超时判断,即在一定的时间内读不到数据,就把缓冲区清空,等下一组数据 const CommOverTime=1 ... With MSComm1 tm1 = Timer Do While .InBufferCount < LEN DoEvents If Timer - tm1 >= CommOverTime Or tm1 < CommOverTime Then 'tm1 < CommOverTime 只对0点有用,如果不是夜以继日的工作,可以不考虑 Exit Do End If Loop StrReceive = .Input End With
此外,因为数据桢是10秒发送一次,大小为300BYTE左右(根据帧的类型而不同),我的程序是每4秒检查帧一次,当帧结构对时,设INPUTLEN=LEN(帧)再INPUT;INBUFFER为10240BYTE,估计够大,应该不会是缓冲区的问题。
最好加上超时判断,即在一定的时间内读不到数据,就把缓冲区清空,等下一组数据
const CommOverTime=1
...
With MSComm1
tm1 = Timer
Do While .InBufferCount < LEN
DoEvents
If Timer - tm1 >= CommOverTime Or tm1 < CommOverTime Then 'tm1 < CommOverTime 只对0点有用,如果不是夜以继日的工作,可以不考虑
Exit Do
End If
Loop
StrReceive = .Input
End With
我设置了调试点,在串口读数据不进时,inbuffercount=0。而清空缓冲区也是用.inbuffercount=0。
如果是,可以来信交流一下。