简体   繁体   English

Python USB Serial在IDLE中工作,但在作为文件运行时却不能工作

[英]Python USB Serial works in IDLE but not when run as a file

I'm currently attempting to write over USB serial to an Arduino Nano of mine using Python. 我目前正在尝试使用Python通过USB串行写入我的Arduino Nano。 However, what I've discovered is that (using the exact same code), the code works perfectly when I type it into IDLE, but when I save it to a file and attempt to run from there, for some reason the Arduino is never receiving the data. 但是,我发现的是(使用完全相同的代码),当我将其键入IDLE时,该代码可以正常工作,但是当我将其保存到文件并尝试从那里运行时,由于某种原因,Arduino永远不会接收数据。 I've checked and in both locations the correct version of Python is being used (2.7.9) (I unfortunately can't use Python 3 due to other libraries I'm using). 我已经检查并在两个位置都使用了正确版本的Python(2.7.9)(很遗憾,由于我正在使用其他库,我无法使用Python 3)。

The code I'm using: 我正在使用的代码:

import serial
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=9600)
ser.write('0')
print ser.readline()

When I run it in IDLE just by typing in the lines individually, the correct behavior is seen: the Arduino responds (turning a servo) and echoes back the data it was sent, which is printed correctly. 当我通过单独输入各行在IDLE中运行它时,可以看到正确的行为:Arduino响应(转动一个伺服器)并回显已发送的数据,该数据已正确打印。 Running from a saved file however, the servo does not respond and no echo is received. 但是,从保存的文件运行时,伺服器没有响应,也没有收到回音。

Any ideas? 有任何想法吗?

I somehow missed this answer on SO before ( pySerial write() works fine in Python interpreter, but not Python script ), but it turns out that I needed to add a time.sleep(2) after opening the serial port. 我以某种方式错过了SO上的这个答案( pySerial write()在Python解释器中工作正常,但在Python脚本中却没有 ),但事实证明,我需要在打开串行端口后添加time.sleep(2)。 My guess is that in IDLE the time it took for me to type the next line accounted for this delay, but it was happening instantly in code. 我的猜测是,在IDLE中,我键入下一行所花费的时间是造成此延迟的原因,但它在代码中立即发生。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python文件阅读器不能在IDLE中运行,但在终端中可以正常工作吗? - Python file-reader will not run in IDLE but works fine in terminal? 当我从 IDLE 运行但不在命令行中运行时,Python 脚本有效吗? - Python script works when I run from IDLE but not in command line? 插入USB时自动运行python文件 - Auto run python file when usb inserted 从文件夹或桌面打开时,Python程序无法正确运行,但在IDLE中运行时,它可以正常运行 - Python program won't run correctly when opened from folder or desktop, but works fine when run in IDLE 使用Python 3 Idle运行.sh文件 - Run a .sh file with Python 3 Idle Python 脚本在 IDLE 中执行时有效,但在控制台中无效 - Python script works when executed in IDLE, but not in console 当我从空闲导入`http.server`时它工作,但是当我运行一个有'import http.server`的python文件时出现错误 - When I am importing `http.server` from the idle it works, but when I run a python file having `import http.server` there is an error Python运行py文件-在IDLE中工作正常,而不是通过命令提示符窗口7-UnicodeEncodeError - Python run py file - works OK in IDLE, not via command prompt windows 7 - UnicodeEncodeError 如何从Idle的包中运行python文件? - how to run a python file in a package from Idle? os.makedirs在IDLE中运行时有效,在计划任务中失败 - os.makedirs works when run in IDLE, fails in scheduled task
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM