同的Arduino Pyserial问题 - 工程使用Python外壳,但不是在一个程序 [英] Pyserial problem with Arduino - works with the Python shell but not in a program

查看:293
本文介绍了同的Arduino Pyserial问题 - 工程使用Python外壳,但不是在一个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我肯定我的Arduino电路是正确和code吧。我知道这是因为,当我使用内置到Arduino的IDE串行监控和发送H的LED补光灯,当我发送L是LED熄灭。

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.

现在我做了一个Python程序

Now I made a Python program

import serial
ser = serial.Serial("COM4",9600)
ser.write("H")

当我运行code中的LED的第二个闪烁上然后回到关闭。
然而,当我做这些行单独在它的工作原理就像它应该。

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.

任何想法?

推荐答案

当您打开串口,这将导致Arduino的复位。由于Arduino的需要一些时间来启动,所有的输入变为到位桶(或可能到做上帝知道它的引导程序)。如果插入的睡眠,你等待的Arduino拿出所以你的串行code表示。这就是为什么它的工作原理交互;你在等需要的软件1.5秒就上来了。

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.

我证实,打开串口重置我 Arduino的乌诺;我闪过一个程序,它会闪烁从设置LED()常规 - 调用打开(/开发/ ttyACM0)足以触发重置。这是恕我直言,在串行支持一个混乱和无证皱纹。

I confirmed that opening the serial port resets my Arduino Uno; I flashed a program which will blink the LED from the setup() routine -- calling open("/dev/ttyACM0") was sufficient to trigger the reset. This is IMHO a confusing and undocumented wrinkle in the serial support.

这篇关于同的Arduino Pyserial问题 - 工程使用Python外壳,但不是在一个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆