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

Can't write to a serial port twice using Pyserial without closing and reopening the port in between writing data

如果我用 Pyserial 打开一个端口然后尝试将 GCode 写入打印机控制器板两次,第二个命令不会执行。如果我在第一次写入数据后关闭端口,然后重新打开它并再次写入,它工作正常。我发现 another Whosebug post 详细说明了同样的问题,但该解决方案不适用于我的代码,因为串行对象实例化默认为 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

如何解释这种行为?

已解决:问题与软件无关。发现我的棋盘的终点站总是通过使用 serial.write(b'M119\n') 触发。通过在常闭配置中连接止动器来修复。