Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieve data/exception from a handler function
    text
    copied!<p>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 (<code>requested_serial_number</code> is coming from the query string).</p> <p>So I have the following code inside my <code>do_GET</code>:</p> <pre><code>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) </code></pre> <p>And this is my scales_handler (plus custom exception definition):</p> <pre><code>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) </code></pre> <p>So the process is:</p> <ol> <li>Python starts listening for requests on port 8000 using http.server's <code>HTTPServer</code> and <code>BaseHTTPRequestHandler</code></li> <li>A request comes in and is routed to my <code>do_GET</code> function.</li> <li>The <code>do_GET</code> performs some basic validation and then searches for a matching USB HID (my first code sample).</li> <li>If it finds a match, it opens the device and passes in a data handler.</li> </ol> <p><strong>What I'm trying to achieve:</strong></p> <p>I want to allow the data handler to keep reading from the scale until <code>data[1] == 4</code> (which indicates a stable reading from the scale). At that point, I want to retrieve this reading in my <code>do_GET</code> and send it to the waiting HTTP client, close the device and end the function.</p> <p>However, my <code>GotStableWeightException</code> isn't caught inside my <code>do_GET</code>, 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 <code>do_GET</code> once it matches the <code>data[1] == 4</code> condition.</p> <p><strong>EDIT</strong></p> <p><strong>What I get:</strong></p> <p>Here's the output I see in the terminal from <code>scales_handler</code>:</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload