Python pySerial-使用子类的问题

Python pySerial-使用子类的问题,python,serialization,serial-port,subclass,pyserial,Python,Serialization,Serial Port,Subclass,Pyserial,我正在用Python进行一个项目,该项目使用两个或多个串行端口从我的RPi管理一些设备。当端口在同一个文件中打开时,我向serial.serial对象的不同实例发送命令,一切正常。这是一个例子: import serial device1_port = "/dev/ttyUSB0" device2_port = "/dev/ttyUSB1" # Command sent to device 1. No problem d1 = serial.Seria

我正在用Python进行一个项目,该项目使用两个或多个串行端口从我的RPi管理一些设备。当端口在同一个文件中打开时,我向serial.serial对象的不同实例发送命令,一切正常。这是一个例子:

import serial

device1_port = "/dev/ttyUSB0"
device2_port = "/dev/ttyUSB1"

# Command sent to device 1. No problem
d1 = serial.Serial(device1_port, timeout = 0.5)
d1.write(b'GET MUTE\n')
output1 = d1.readline()
print("Device 1 output: " + str(output1))


# Command sent to device 2. No problem
d2 = serial.Serial(device2_port, timeout = 1)
d2.write(b'00vP\r')
output2 = d2.readline()
print("Device 2 output: " + str(output2))
输出:

Device 1 output: b'DATA MUTE OFF\r\n'
Device 2 output: b'00vP0\r'

当我试图使用serial.serial的子类将一个设备与另一个设备分离时,问题就出现了。原因是我想用自己的方法像处理对象一样处理它们(每个设备需要很多不同的命令、状态查询…)

运行此代码时,输出为:

Device 1 output: b'DATA MUTE OFF\r\n'
_
它一直在等待第二台设备。我被迫按Ctrl+C组合键,结果是:

^CTraceback (most recent call last):
  File "./ct3.py", line 35, in <module>
    d2.command2()
  File "./ct3.py", line 23, in command2
    output = self.readline()
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 483, in read
    ready, _, _ = select.select([self.fd, self.pipe_abort_read_r], [], [], timeout.time_left())
KeyboardInterrupt

^CTraceback(最近一次通话最后一次):
文件“/ct3.py”,第35行,在
d2.命令2()
command2中第23行的文件“/ct3.py”
输出=self.readline()
文件“/usr/lib/python3/dist packages/serial/serialposix.py”,第483行,已读
就绪,u,u=select.select([self.fd,self.pipe\u abort\u read\u r],[],timeout.time\u left())
键盘中断
这两个子类之间似乎存在某种冲突,但显然我不知道我做错了什么


有人能帮帮我吗?

你不应该从你的
\uuuuu init\uuuuu
调用
serial.serial(端口,超时)
, 作为
super()。\uuuu init\uuuu(…)
已经在这样做了。看见如果您不打算更改基类的功能,那么您甚至不需要
\uuuuu init\uuuu

此外,在位置参数和关键字参数的使用方面,两个版本也存在差异
serial.serial()
的前两个位置参数是
port,baudrate
,因此需要显式使用关键字参数
timeout=

def\uuuu初始化(自身、端口、超时):
超级()

目前我没有任何线索,但在函数的开头添加了一个print语句,以查看command1是否阻塞,或者您是否输入了
command2
,然后您就被卡住了。我这样做了,并且在第二个命令中读取串行端口时可以看到它挂起。最终在另一个答案中找到了解决方案。无论如何,非常感谢你!好的,我理解。我删除了
serial.serial(port,timeout)
并保留了
super()。\uuu init\uuuu(port,timeout)
,但结果是一样的:(我看到了你的类不同的另一个原因。看到了我更新的答案。正如你所说,我没有使用timeout作为关键字参数。你救了我的命!非常感谢!
^CTraceback (most recent call last):
  File "./ct3.py", line 35, in <module>
    d2.command2()
  File "./ct3.py", line 23, in command2
    output = self.readline()
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 483, in read
    ready, _, _ = select.select([self.fd, self.pipe_abort_read_r], [], [], timeout.time_left())
KeyboardInterrupt