使用Pyserial写入串行端口两次时,如果不在写入数据之间关闭并重新打开端口,则无法写入

2024-04-23 14:14:05 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我用Pyserial打开一个端口,然后尝试将GCode写入打印机控制器板两次,则第二个命令不会执行。如果我在第一次写入数据后关闭端口,然后重新打开并再次写入,则可以正常工作。我发现了another stackoverflow篇文章详细描述了相同的问题,但解决方案不适用于我的代码,因为串行对象实例化默认为dsrdtr=False。这是我的密码:

from serial import Serial
from typing import cast
import time

serial = Serial(str('COM4'), 115200, timeout=2, writeTimeout=3) # Open port

time.sleep(1) # Wait for board to boot
serial.write(b'G00 X25\n') # Rotate stepper motor

time.sleep(2) # Wait 2 seconds or else the port will close before the motor stops rotating

serial.close()
serial.open()

time.sleep(1) # Wait for board to boot
serial.write(b'G00 X25\n') # Rotate stepper motor

什么能解释这种行为


Tags: to端口fromimportboardfortimeport