为什么我获取串口数据时有时会乱了呢?

sosoben sosoben 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下。我用timer 每1秒分别向下位机获取X Y坐标,但有时数据会少一个位,有时X,Y值会掉转
另:为什么我不用事件触发呢?主要是除了这个命令 我对下位机还有其他命令,我想发一条命令就准确收到其值
我已经不用DOevents了,还会这样
下位机反映速度不用怀疑,都是毫秒级的


MSComm1.Output = "AXI1:POS?" & Chr(13) '对下位机发送位置请求
Dim t As Single
t = Timer
Do
'DoEvents
GetInput = MSComm1.Input
Loop Until GetInput <> "" Or Timer - t > 1 '主要是防止超时导致的死循环
Labelpos.Caption = GetInput '把位置显示出来

MSComm1.Output = "AXI2:POS?" & Chr(13)
t = Timer
Do
'DoEvents
GetInput = MSComm1.Input
Loop Until GetInput <> "" Or Timer - t > 1
Labelposy.Caption = GetInput
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
sosoben
VB基础类 7,788 成员 197,583 内容