pySerial 写入 Arduino Uno 得到缓冲

发布于 2024-10-09 18:05:34 字数 905 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2024-10-16 18:05:34

试试这个:

serial.write("INIT\r")

writelines 可能需要一个列表(但我现在无法检查它)。

Try this this instead:

serial.write("INIT\r")

The writelines probably takes a list (but I can't check it now).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文