本文介绍了PySerial App 在 shell 中运行,不是 py 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
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).
Here is the simple code.
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?
解决方案
OK, I've figured it out!
It's necessary to use code like this before performing the write:
time.sleep(1)
ser.setDTR(level=0)
time.sleep(1)
Otherwise, the arduino automatically resets upon receiving a serial connection for some reason. yay!
这篇关于PySerial App 在 shell 中运行,不是 py 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!