在使用 minicom 打开端口之前,使用 PySerial 不起作用
using PySerial don't work until port is opened with minicom
我在 RPi3B+ 上为 Domoticz 开发了一个插件。这个插件在 Python。
我想使用 USB 串行端口向 Arduino 板发送命令。
插件打开串口,发送命令,关闭串口。它运行良好,除非在重新启动后。
重启后,端口是打开的,命令好像发给了Arduino,但是它听不懂,就好像波特率不对一样。 Arduino 的 Rx LED 正在闪烁。
如果我使用 minicom 并行打开 serial 并在不重置参数的情况下退出 minicom,那么插件将开始正常工作。
这是我的代码:
serialCmd = "gpio sh" + str( shutterId ) + "_" + order +" on for " + str( PULSE_DURATION_MS ) + "\r"
Domoticz.Debug( "Serial command : " + serialCmd )
# open serial port
try:
Domoticz.Debug( "Opening serial port : " + Parameters["SerialPort"] )
serialPort = serial.Serial( port = Parameters["SerialPort"],
baudrate = 115200,
bytesize = serial.EIGHTBITS,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
timeout = 1,
xonxoff = False,
rtscts = False,
dsrdtr = False )
except:
serialPort = None
if serialPort:
serialPort.write( serialCmd.encode( 'utf-8' ) )
serialPort.close()
serialPort = None
串口为/dev/ttyUSB0。
如果我尝试在 serial.Serial(...) 中使用 exclusive = True,它无法打开端口,就好像端口已经打开。
另外一件奇怪的事情:当Arduino板重启时,它会向串口发送一些信息。
我不能用Python中的插件用PySerial读取它,但我可以用minicom读取它。
如果我关闭 minicom 并 重置参数,每次我打开 minicom 时,minicom 都会读取此信息(无需重置 Arduino 板),就好像输入缓冲区从未被读取过一样阅读,Python 插件仍然不起作用。
如何解决问题?
这个问题是因为 Arduino nano schematic where DTR signal is connected to the reset signal which makes the Arduino to reset each time the serial port is open. This post 让我在互联网上搜索了正确的东西。这就是为什么打开 minicom 并在不重置端口的情况下关闭使其工作...
当进程不再使用串口时,DTR 线会重置,因此下次进程打开串口时,会驱动 DTR,从而使 Arduino 重新启动。
一种解决方法是 modify the board or two disable the DTR handling. This can be done by dsiable the HUPCL bit of the terminal lib。
有些人设法 fix it with pySerial but it does not work for me so I had to do as below...found here and here。
import serial
import termios
port = '/dev/ttysUSB0'
f = open(port)
attrs = termios.tcgetattr(f)
attrs[2] = attrs[2] & ~termios.HUPCL
termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
f.close()
se = serial.Serial()
se.baudrate = 115200
se.port = port
se.open()
我在 RPi3B+ 上为 Domoticz 开发了一个插件。这个插件在 Python。 我想使用 USB 串行端口向 Arduino 板发送命令。
插件打开串口,发送命令,关闭串口。它运行良好,除非在重新启动后。
重启后,端口是打开的,命令好像发给了Arduino,但是它听不懂,就好像波特率不对一样。 Arduino 的 Rx LED 正在闪烁。
如果我使用 minicom 并行打开 serial 并在不重置参数的情况下退出 minicom,那么插件将开始正常工作。
这是我的代码:
serialCmd = "gpio sh" + str( shutterId ) + "_" + order +" on for " + str( PULSE_DURATION_MS ) + "\r"
Domoticz.Debug( "Serial command : " + serialCmd )
# open serial port
try:
Domoticz.Debug( "Opening serial port : " + Parameters["SerialPort"] )
serialPort = serial.Serial( port = Parameters["SerialPort"],
baudrate = 115200,
bytesize = serial.EIGHTBITS,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
timeout = 1,
xonxoff = False,
rtscts = False,
dsrdtr = False )
except:
serialPort = None
if serialPort:
serialPort.write( serialCmd.encode( 'utf-8' ) )
serialPort.close()
serialPort = None
串口为/dev/ttyUSB0。 如果我尝试在 serial.Serial(...) 中使用 exclusive = True,它无法打开端口,就好像端口已经打开。
另外一件奇怪的事情:当Arduino板重启时,它会向串口发送一些信息。 我不能用Python中的插件用PySerial读取它,但我可以用minicom读取它。
如果我关闭 minicom 并 重置参数,每次我打开 minicom 时,minicom 都会读取此信息(无需重置 Arduino 板),就好像输入缓冲区从未被读取过一样阅读,Python 插件仍然不起作用。
如何解决问题?
这个问题是因为 Arduino nano schematic where DTR signal is connected to the reset signal which makes the Arduino to reset each time the serial port is open. This post 让我在互联网上搜索了正确的东西。这就是为什么打开 minicom 并在不重置端口的情况下关闭使其工作...
当进程不再使用串口时,DTR 线会重置,因此下次进程打开串口时,会驱动 DTR,从而使 Arduino 重新启动。
一种解决方法是 modify the board or two disable the DTR handling. This can be done by dsiable the HUPCL bit of the terminal lib。
有些人设法 fix it with pySerial but it does not work for me so I had to do as below...found here and here。
import serial
import termios
port = '/dev/ttysUSB0'
f = open(port)
attrs = termios.tcgetattr(f)
attrs[2] = attrs[2] & ~termios.HUPCL
termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
f.close()
se = serial.Serial()
se.baudrate = 115200
se.port = port
se.open()