我有一个pyserial线程挂起在一个意想不到的地方;检查串行端口是否有排队的字节。 所有串行输入都在此线程中完成;主要主要是显示管理器。init执行得很好,一旦进入运行子例程,它就会挂在语句 "Trace('In_waiting={}'.format(self.serial.in_waiting(((("上。 main 不是 CPU 占用 - 它更新显示然后等待 1.0 秒 - 代码调试得很好(天真的程序员说 ;-((。尽管有 GPIO 引脚的回调,但没有其他线程。输入来自RasPi UART引脚。
将代码挂在这个位置对我来说毫无意义。 我已经尝试过使用各种超时值,包括 None 和 0。 文档似乎清楚地表明返回 0 或 n 是唯一的选项。 我可能错过了一些明显的东西,但它让我无法理解。
class Monitor_PE_Devices_Thread(threading.Thread):
# Thread to monitor all of the PE devices. All updating of the device_list items occurs
# here.
def __init__(self):
global device_list
threading.Thread.__init__(self)
Log.Debug('PE Monitoring thread initialized')
self.buffer = ''
self.port = '/dev/ttyAMA0'
self.speed = 9600
self.serial = serial.Serial(port=self.port, baudrate=self.speed, timeout=0.5)
if self.serial.is_open: Trace('Serial port {} opened'.format(self.serial.name))
Log.Debug('Monitoring the following devices:')
for d in device_list:
Log.Debug(' DevID[{}]: Name={}'.format(d, device_list[d][0]))
def process_PE_response(self, devid, data): <block compressed>
def read_line(self): <block compressed>
def run(self):
# Main loop for PE device monitoring
global kill_threads, device_list
Log.Debug('PE Monitoring thread started')
while not kill_threads:
Trace('Top of read serial loop')
Trace('Port status={}'.format(self.serial.is_open))
Trace('In_waiting={}'.format(self.serial.in_waiting()))
if self.serial.in_waiting() > 0:
line = self.read_line()
if len(line) > 0 and line[0] == 'a':
devid = line[1:3]
data = line[3:]
Trace('Data recieved for dev[{}]: {}'.format(devid, data))
dev = device_list.get(devid, None)
if dev != None:
device_list[devid][3] = utcnow()
self.process_PE_response(devid, data)
else:
Log.Debug('Unknown device id {} - ignored'.format(devid))
else:
Log.Info('Invalid serial data: {}'.format(line))
环境:python 3,pyserial 3,Raspberry Pi 3,Buster Lite,pygame
在pySerial 3.0中,in_waiting
是一个属性,而不是一个方法。
所以当你使用self.serial.in_waiting()
时,Python 应该会引发一个TypeError
,因为in_waiting
的值在一个int
中,这不是一个可调用的。
一个小例子:
In [5]: class Test:
...: def __init__(self, a):
...: self._a = a
...:
...: @property
...: def a(self):
...: return self._a
...:
In [6]: t = Test(2)
Out[6]: <__main__.Test at 0x8042660d0>
In [7]: t.a
Out[7]: 2
In [8]: t.a()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-47b4c8d89978> in <module>
----> 1 t.a()
TypeError: 'int' object is not callable
可能是您的程序似乎挂起了,因为这发生在另一个线程中。
未经处理的异常应终止Monitor_PE_Devices_Thread.run()
。
在主线程中,如果Monitor_PE_Devices_Thread
仍在使用其is_alive()
方法运行,则更新显示测试。