Browse Source

Refactored exceptions so that hopefully no one will have to deal with USB/FTDI outside of us.

pyserial_fix
Scott Petersen 11 years ago
parent
commit
868941cef2
1 changed files with 13 additions and 9 deletions
  1. +13
    -9
      pyad2usb/ad2usb.py

+ 13
- 9
pyad2usb/ad2usb.py View File

@@ -9,6 +9,9 @@ import threading
class NoDeviceError(Exception): class NoDeviceError(Exception):
pass pass


class CommError(Exception):
pass

class AD2USB(object): class AD2USB(object):
on_test = event.Event('testing') on_test = event.Event('testing')


@@ -73,8 +76,8 @@ class Device(object):


try: try:
devices = Ftdi.find_all([(Device.FTDI_VENDOR_ID, Device.FTDI_PRODUCT_ID)], nocache=True) devices = Ftdi.find_all([(Device.FTDI_VENDOR_ID, Device.FTDI_PRODUCT_ID)], nocache=True)
except usb.core.USBError, e:
raise
except (usb.core.USBError, FtdiError), err:
raise CommError('Error enumerating AD2USB devices: {0}'.format(str(err)))


return devices return devices


@@ -101,9 +104,10 @@ class Device(object):
self._description) self._description)


self._device.set_baudrate(baudrate) self._device.set_baudrate(baudrate)
except (usb.core.USBError, FtdiError):
except (usb.core.USBError, FtdiError), err:
self.on_close() self.on_close()
raise

raise CommError('Error opening AD2USB device: {0}'.format(str(err)))
else: else:
self._read_thread.start() self._read_thread.start()


@@ -151,9 +155,10 @@ class Device(object):
break break


time.sleep(0.01) time.sleep(0.01)
except FtdiError, e:
# TODO: I don't think we should be ignoring this
raise
except (usb.core.USBError, FtdiError), err:
self.close()

raise CommError('Error reading from AD2USB device: {0}'.format(str(err)))
else: else:
if got_line: if got_line:
ret = self._buffer ret = self._buffer
@@ -178,8 +183,7 @@ class Device(object):
while self._running: while self._running:
try: try:
self._device.read_line() self._device.read_line()
except (usb.core.USBError, FtdiError):
except CommError, err:
self.stop() self.stop()
self._device.close()


time.sleep(0.25) time.sleep(0.25)

Loading…
Cancel
Save