PySerial App 在 shell 中运行,而不是 py 脚本 [英] PySerial App runs in shell, by not py script
问题描述
我有一个非常简单的 python 脚本,它使用 pySerial 通过串行端口将数据发送到我的 Arduino.当我在 python shell 中逐行执行这个时,它工作得很好,但是当我把它放在一个.py"文件中并尝试运行它时,没有任何反应.虽然我的 UART 上的串行灯会闪烁.所以有些东西正在通过,但它是垃圾(我检查过).
I have a very simple python script that uses pySerial to send data over the serial port to my Arduino. When I execute this line-by-line in the python shell, it works just fine, but when I put it in a ".py" file, and try to run it, nothing happens. Though the serial lights on my UART do flash. So something is getting through, but it's garbage (I checked).
这是简单的代码.
import serial
ser = serial.Serial('COM3',9600,timeout=.2)
ser.write('A')
ser.close()
我已经尝试添加睡眠,但似乎没有任何解决方法.有什么想法吗?
I've already tried adding sleeps, but nothing seems to be fixing it. Any ideas?
推荐答案
好的,我想通了!
在执行写入之前需要使用这样的代码:
It's necessary to use code like this before performing the write:
time.sleep(1)
ser.setDTR(level=0)
time.sleep(1)
否则,由于某种原因,arduino 在收到串行连接时会自动重置.耶!
Otherwise, the arduino automatically resets upon receiving a serial connection for some reason. yay!
这篇关于PySerial App 在 shell 中运行,而不是 py 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!