VernierST / godirect-py

A Python module for reading from Vernier Go Direct® Sensors using USB or BLE.
GNU General Public License v3.0
8 stars 9 forks source link

Can't Close / Quit on KeyboardInterrupt #18

Open dderiso opened 4 years ago

dderiso commented 4 years ago

Thanks for all the help so far! I'm having some issues with closing the device inside of a KeyboardInterrupt handler; I can close/quit just fine otherwise. Thanks for your help!

import logging
logging.basicConfig()
logging.getLogger('godirect').setLevel(logging.DEBUG)
logging.getLogger('pygatt').setLevel(logging.DEBUG)

from godirect import GoDirect
godirect = GoDirect(use_ble=True,use_usb=False)
print(f"GoDirect v {godirect.get_version()}")
device = godirect.get_device(threshold=-100)
if device != None and device.open(auto_start=False):
    print(f"Vernier: Connected: {device.name}")
    device.start(period=1000)
    sensors = device.get_enabled_sensors()
    while True:
        try:
            if device.read():
                for sensor in sensors:
                    print(sensor.sensor_description+": "+str(sensor.values))                  
                    sensor.clear()
        except KeyboardInterrupt:
            print("Vernier: Closing Server")
            device.close()
            godirect.quit()
WARNING:bleak.backends.corebluetooth.CentralManagerDelegate:CentralManagerDelegate is not compliant
GoDirect v 1.0.7
DEBUG:bleak.backends.corebluetooth.CentralManagerDelegate:Bluetooth powered on

...

DEBUG:godirect:REAL32: 7E  4C  A6  41
O₂ Gas: [20.787349700927734]
^CVernier: Closing Server
DEBUG:godirect:GDX >> 58  05  F6  A7  54
DEBUG:godirect:BLE WRITE: >>>58  05  F6  A7  54
DEBUG:bleak.backends.corebluetooth.client:Write Characteristic F4BF14A6-C7D5-4B6D-8AA8-DF1A7C83ADCB : b'X\x05\xf6\xa7T'
DEBUG:godirect:lengthRemaining 0 offset 5
Traceback (most recent call last):
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device_bleak.py", line 119, in _read
    response = self._responses.get(block=False)
  File "~/anaconda3/lib/python3.7/queue.py", line 167, in get
    raise Empty
_queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "vernier_test.py", line 18, in <module>
    if device.read():
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device.py", line 182, in read
    return self._GDX_read_measurement(timeout)
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device.py", line 388, in _GDX_read_measurement
    response = self._GDX_read_blocking(timeout)
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device.py", line 337, in _GDX_read_blocking
    response = self._read(timeout)
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device_bleak.py", line 122, in _read
    self._loop.run_until_complete(self._wait())
  File "~/anaconda3/lib/python3.7/asyncio/base_events.py", line 566, in run_until_complete
    self.run_forever()
  File "~/anaconda3/lib/python3.7/asyncio/base_events.py", line 534, in run_forever
    self._run_once()
  File "~/anaconda3/lib/python3.7/asyncio/base_events.py", line 1771, in _run_once
    handle._run()
  File "~/anaconda3/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "~/anaconda3/lib/python3.7/site-packages/bleak/backends/corebluetooth/__init__.py", line 43, in _handle_nsrunloop
    self.nsrunloop.runMode_beforeDate_(NSDefaultRunLoopMode, time_interval)
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device_bleak.py", line 119, in _read
    response = self._responses.get(block=False)
  File "~/anaconda3/lib/python3.7/queue.py", line 167, in get
    raise Empty
_queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "vernier_test.py", line 24, in <module>
    device.close()
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device.py", line 197, in close
    self._GDX_disconnect()
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device.py", line 700, in _GDX_disconnect
    return self._GDX_write_and_check_response(command)
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device.py", line 370, in _GDX_write_and_check_response
    response = self._GDX_read_blocking(timeout=timeout)
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device.py", line 337, in _GDX_read_blocking
    response = self._read(timeout)
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device_bleak.py", line 122, in _read
    self._loop.run_until_complete(self._wait())
  File "~/anaconda3/lib/python3.7/asyncio/base_events.py", line 577, in run_until_complete
    raise RuntimeError('Event loop stopped before Future completed.')
RuntimeError: Event loop stopped before Future completed.
ERROR:asyncio:Task exception was never retrieved
future: <Task finished coro=<Application._handle_nsrunloop() done, defined at ~/anaconda3/lib/python3.7/site-packages/bleak/backends/corebluetooth/__init__.py:38> exception=KeyboardInterrupt()>
Traceback (most recent call last):
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device_bleak.py", line 119, in _read
    response = self._responses.get(block=False)
  File "~/anaconda3/lib/python3.7/queue.py", line 167, in get
    raise Empty
_queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "vernier_test.py", line 18, in <module>
    if device.read():
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device.py", line 182, in read
    return self._GDX_read_measurement(timeout)
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device.py", line 388, in _GDX_read_measurement
    response = self._GDX_read_blocking(timeout)
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device.py", line 337, in _GDX_read_blocking
    response = self._read(timeout)
  File "~/anaconda3/lib/python3.7/site-packages/godirect/device_bleak.py", line 122, in _read
    self._loop.run_until_complete(self._wait())
  File "~/anaconda3/lib/python3.7/asyncio/base_events.py", line 566, in run_until_complete
    self.run_forever()
  File "~/anaconda3/lib/python3.7/asyncio/base_events.py", line 534, in run_forever
    self._run_once()
  File "~/anaconda3/lib/python3.7/asyncio/base_events.py", line 1771, in _run_once
    handle._run()
  File "~/anaconda3/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "~/anaconda3/lib/python3.7/site-packages/bleak/backends/corebluetooth/__init__.py", line 43, in _handle_nsrunloop
    self.nsrunloop.runMode_beforeDate_(NSDefaultRunLoopMode, time_interval)
KeyboardInterrupt