Arduino 的 Pyserial 问题 - 适用于 Python shell,但不适用于程序
好吧,我确信我的 Arduino 电路及其代码是正确的。我知道这一点是因为当我使用 Arduino IDE 内置的串行监视器并发送“H”时,LED 会亮起,当我发送“L”时,LED 会关闭。
现在我制作了一个 Python 程序,
import serial
ser = serial.Serial("COM4",9600)
ser.write("H")
当我运行代码时,LED 会闪烁一秒钟然后熄灭。 然而,当我在 shell 中单独执行每一行时,它的工作原理就像预期的那样。
有什么想法吗?
All right, so I am positive my Arduino circuit is correct and the code for it. I know this because when I use the serial monitor built into the Arduino IDE and send 'H' an LED lights up, when I send 'L' that LED turns off.
Now I made a Python program
import serial
ser = serial.Serial("COM4",9600)
ser.write("H")
When I run the code the LED blinks on for a second then goes back off.
However when I do each of these lines separately in the shell it works just like it is supposed to.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您打开串行端口时,这会导致 Arduino 重置。由于 Arduino 需要一些时间来启动,所有输入都会进入 bitbucket(或者可能进入引导加载程序,天知道它会做什么)。如果您插入睡眠,您将等待 Arduino 出现,以便您的串行代码。这就是它交互工作的原因;您等待软件启动所需的 1.5 秒。
我确认打开串口会重置我的 Arduino Uno;我编写了一个程序,该程序将从
setup()
例程中使 LED 闪烁 - 调用open("/dev/ttyACM0")
足以触发重置。恕我直言,这是串行支持中令人困惑且未记录的问题。When you open the serial port, this causes the Arduino to reset. Since the Arduino takes some time to bootup, all the input goes to the bitbucket (or probably to the bootloader which does god knows what with it). If you insert a sleep, you wait for the Arduino to come up so your serial code. This is why it works interactively; you were waiting the 1.5 seconds needed for the software to come up.
I confirmed that opening the serial port resets my Arduino Uno; I flashed a program which will blink the LED from the
setup()
routine -- callingopen("/dev/ttyACM0")
was sufficient to trigger the reset. This is IMHO a confusing and undocumented wrinkle in the serial support.我遇到了同样的问题,如果我从打开串行连接到写入它添加大约 2 秒的延迟,它就会起作用,1 秒是不够的。
I had the same problem and it works if I add a delay of about 2 seconds from opening the serial connection to writing on it, 1 second was not enough.
为了使它更清楚一点,我将修改代码,以便每个人都可以看到需要添加的内容!
添加睡眠语句有助于让串行打开没有任何问题!
Just to make it a bit more clear I'll modify the code so everyone can see what needs to be added!
Adding in a sleep statment helps to let the serial open up without any problems!
假设您使用的是 Arduino Uno
USB 端口和暴露在引脚
1
和0
上的 Uno 串行总线共享相同的RX/TX
线。我建议购买一个 USB 转 TTL 适配器,例如 这里的适配器,以便您可以与 Arduino 进行通信不使用 USB 端口。 Arduino IDE 有自己的方法来脱离 USB 驱动程序,以便创建虚拟串行端口。让您的 Ardunio 使用SoftwareSerial
代替。这是我在互联网上找到的一个示例,其中有人遇到了总线冲突问题。
Assuming you are using an Arduino Uno
The USB port and the Uno serial bus exposed on pins
1
and0
share the sameRX/TX
lines. I suggest getting a USB to TTL adapter like the one here so that you can communicate to the Arduino without using the USB port. The Arduino IDE has its own method for disengaging from the USB driver such that a virtual serial port can be created. Have your Ardunio useSoftwareSerial
instead.Here is an example I found on the internet where somebody had clashing bus issues.