python - 为什么 pySerial 不在我的程序中写入?
问题描述
所以我在我的 arduino nano 上设置了一个程序,它从串行端口读取一个字节并根据接收到的字节是 1 还是 0 打开 LED。我在串行监视器和串行监视器上彻底测试了这个程序在 python 3 shell 中使用 pySerial,它运行良好。但是当我有一个像这样的简单程序时:
import serial
SERIAL = serial.Serial("COM4", 9600)
SERIAL.write(b'1')
董事会什么都不做。pySerial 在 python shell 中工作但在 python 程序中不工作,我做错了什么?
解决方案
After you open the serial port, delay it for minimum a second, because the Arduino resets itself when you open the serial connection and doesn't start reading from the serial immediately.
Example:
import serial
import time
SERIAL = serial.Serial(baudrate='115200', timeout=.2, port='com4') #this edit is not important
time.sleep(3) #sleep 3 seconds
SERIAL.write(b'1')
推荐阅读
- ruby-on-rails - 使用 Rails 6 进行敏捷 Web 开发,但使用 RSpac 和 FactoryBot:为什么这些请求测试会失败?
- java - 如何在 flink 流处理中使用过滤器在键控流上添加处理功能?
- javascript - 在反应中显示来自输入字段的数据
- javascript - 我正在尝试更新状态,但它不起作用
- c# - 如何将集合编辑器中的项目添加到 UserControl?
- c++ - 在 Visual Studio Code 中创建临时文件的权限被拒绝
- python - 如何获取单选按钮来更新直方图 Matplotlib Python
- reactjs - 如何将ant design类组件转换为功能组件
- java - 使用 Jsoup 将数据从网页解析到 Android 应用
- python - 可以导入pytorch但不能导入torch