首页 > 解决方案 > 为什么 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 程序中不工作,我做错了什么?

标签: pythonarduinopyserial

解决方案


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')

推荐阅读