pySerial 写入 Arduino Uno 得到缓冲
我有一个 Python 脚本,它使用 pySerial 将短消息写入 Arduino Uno 板上的串行端口。有一个循环,根据某些条件,循环内可能会发生多次写入,如下所示:
while True:
#Conditions block 1
if <CONDITION1>:
serial.writelines("INIT")
elif <CONDITION2>:
serial.writelines("NEW")
...
#Conditions block 2
if <CONDITION1>:
# Fetch something from the Internet
serial.writelines("CHECK")
elif <CONDITION2>:
# Fetch something from the Internet
serial.writelines("STOP")
...
但是,当我的 Arduino 板收到此消息时,它会收到第一个消息作为 INIT,但第二个消息被读取为 INITSTOP 或 INITCHECK 并且第三条消息与之前的消息连接起来。我的arduino程序以这种方式检查特定消息:
if(msg.equals("CHECK")) {
// Do something
}
else if(msg.equals("INIT")) {
// Do Something else
}
任何人都可以指导我吗?顺便说一句,我不认为问题出在 Arduino 上,因为当我使用 IDE 提供的串行监视器对其进行测试时,它工作得很好。
我尝试在每次写入之前添加最多 10 秒的睡眠时间,但这没有成功。
I have a Python script that writes short messages to the serial port on my Arduino Uno board using pySerial. There is a loop and depending on some conditions, multiple writes can happen within a loop, something like this:
while True:
#Conditions block 1
if <CONDITION1>:
serial.writelines("INIT")
elif <CONDITION2>:
serial.writelines("NEW")
...
#Conditions block 2
if <CONDITION1>:
# Fetch something from the Internet
serial.writelines("CHECK")
elif <CONDITION2>:
# Fetch something from the Internet
serial.writelines("STOP")
...
But, when my Arduino board receives this it receives the first message as INIT, but the second one is being read as INITSTOP or INITCHECK and third one gets concatenated to the previous messages. My arduino program checks for specific message in this way:
if(msg.equals("CHECK")) {
// Do something
}
else if(msg.equals("INIT")) {
// Do Something else
}
Can anyone guide me on this? BTW, I don't think the problem is with the Arduino as it works perfectly when I test it with the Serial Monitor available with the IDE.
I've tried adding sleeps of upto 10 seconds before every write, but that did not work out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
writelines
可能需要一个列表(但我现在无法检查它)。Try this this instead:
The
writelines
probably takes a list (but I can't check it now).