I'm trying to use Python and pywinusb-0.3.2 to read data from a USB weighing scale when a valid request hits the web server (requested_serial_number
is coming from the query string).
So I have the following code inside my 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)
And this is my scales_handler (plus custom exception definition):
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)
So the process is:
- Python starts listening for requests on port 8000 using http.server's
HTTPServer
andBaseHTTPRequestHandler
- A request comes in and is routed to my
do_GET
function. - The
do_GET
performs some basic validation and then searches for a matching USB HID (my first code sample). - If it finds a match, it opens the device and passes in a data handler.
What I'm trying to achieve:
I want to allow the data handler to keep reading from the scale until data[1] == 4
(which indicates a stable reading from the scale). At that point, I want to retrieve this reading in my do_GET
and send it to the waiting HTTP client, close the device and end the function.
However, my GotStableWeightException
isn't caught inside my do_GET
, I think because it is thrown in a different thread. I'm new to programming with multiple threads, and I'm having trouble working out how I can get the result back to do_GET
once it matches the data[1] == 4
condition.
EDIT
What I get:
Here's the output I see in the terminal from 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