简体   繁体   English

为什么 pySerial 不在我的程序中写入?

[英]Why won't pySerial write inside of my program?

So I have a program set up on my arduino nano which reads a single byte from the serial port and turns on an LED depending on whether or not the byte received was 1 or 0. I thoroughly tested this program, both on the Serial monitor and with pySerial in the python 3 shell, and it worked fine.所以我在我的 arduino nano 上设置了一个程序,它从串行端口读取一个字节并根据接收到的字节是 1 还是 0 打开 LED。我在串行监视器和串行监视器上彻底测试了这个程序在 python 3 shell 中使用 pySerial,它运行良好。 But when I have a simple program like this:但是当我有一个像这样的简单程序时:

import serial
SERIAL = serial.Serial("COM4", 9600)
SERIAL.write(b'1')

The board doesn't do anything.董事会什么都不做。 What am I doing wrong that pySerial works in the python shell but not in a python program? 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.打开串行端口后,至少延迟一秒钟,因为 Arduino 在您打开串行连接时会自行重置并且不会立即开始从串行读取。

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM