当有效请求到达web服务器(requested_serial_number来自查询字符串)时,我试图使用Pythonandpywinusb-0.3.2从USB衡器读取数据。
所以我的do_GET中有以下代码
all_hids = hid.find_all_hid_devices()
if all_hids:
for index, device in enumerate(all_hids):
if device.serial_number == requested_serial_number:
device.open()
try:
device.set_raw_data_handler(scales_handler)
while True:
sleep(0.1)
except GotStableWeightException as e:
self.do_HEAD(200)
self.send_body(e.value)这是我的scales_handler (加上自定义异常定义):
def scales_handler(data):
print("Byte 0: {0}".format(data[0]))
print("Byte 1: {0}".format(data[1]))
print("Byte 2: {0}".format(data[2]))
print("Byte 3: {0}".format(data[3]))
print("Byte 4: {0}".format(data[4]))
print("Byte 5: {0}".format(data[5]))
print("All: {0}".format(data))
if data[1] == 4:
raise GotStableWeightException(data[4] + (256 * data[5]))
class GotStableWeightException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)因此,这个过程是:
HTTPServer和BaseHTTPRequestHandler开始侦听端口8000上的请求do_GET函数。do_GET执行一些基本的验证,然后搜索匹配的USB (我的第一个代码示例)。我想要达到的目标:
我希望允许数据处理程序继续从刻度读取数据,直到data[1] == 4 (这意味着从刻度表中获得了稳定的读数)。此时,我希望在我的do_GET中检索这个读取,并将其发送到等待的HTTP,关闭设备并结束该函数。
但是,我的GotStableWeightException并没有被捕获到我的do_GET中,我想是因为它被抛到了另一个线程中。我刚开始使用多线程编程,而且我很难弄清楚如何在结果与do_GET条件匹配之后才能将结果返回到data[1] == 4。
编辑
我得到了什么:
这是我在scales_handler终端上看到的输出
Byte 0: 3
Byte 1: 4
Byte 2: 2
Byte 3: 0
Byte 4: 204
Byte 5: 0
All: [3, 4, 2, 0, 204, 0]
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python33\lib\threading.py", line 639, in _bootstrap_inner
self.run()
File "C:\Python33\lib\site-packages\pywinusb-0.3.2-py3.3.egg\pywinusb\hid\core.py", line 886, in run
hid_object._process_raw_report(raw_report)
File "C:\Python33\lib\site-packages\pywinusb-0.3.2-py3.3.egg\pywinusb\hid\helpers.py", line 64, in new_function
return function_target(*args, **kw)
File "C:\Python33\lib\site-packages\pywinusb-0.3.2-py3.3.egg\pywinusb\hid\core.py", line 714, in _process_raw_report
self.__raw_handler(helpers.ReadOnlyList(raw_report))
File "C:\USB HID Scales\server.py", line 28, in scales_handler
raise GotStableWeightException(data[4] + (256 * data[5]))
GotStableWeightException: 204发布于 2013-05-02 10:51:04
所以device是一个线程化的HidDevice类。正如您所指出的,在这个线程对象中引发异常不会被处理程序( do_GET)捕获。
然而,我想知道引发异常是否真的是最简单的事情(异常往往是针对错误和问题而保留的)。为了达到你的目标,是否可以使用一个global变量来做这样的事情;
global e_value
e_value = None
def scales_handler(data):
print("Byte 0: {0}".format(data[0]))
print("Byte 1: {0}".format(data[1]))
print("Byte 2: {0}".format(data[2]))
print("Byte 3: {0}".format(data[3]))
print("Byte 4: {0}".format(data[4]))
print("Byte 5: {0}".format(data[5]))
print("All: {0}".format(data))
if data[1] == 4:
e_value = data[4] + (256 * data[5]))
devices = []
try:
for index, device in enumerate(all_hids):
if device.serial_number == requested_serial_number:
devices.append(device)
device.open()
device.set_raw_data_handler(sample_handler)
while True:
time.sleep(0.1)
finally:
for device in devices:
device.close()
if e_value is not None:
self.do_HEAD(200)
self.send_body(e_value) 我无法证明您的设备是什么,所以我应该警告您,这并不是线程安全--如果多个设备都有data[1] == 4,则只能在最后一个设备上设置e_value来访问它。(尽管使用全局数组和计数器对象来修复这个问题很简单)。
https://stackoverflow.com/questions/16335160
复制相似问题