Browse Source

Exception tweaks

pyserial_fix
Scott Petersen 12 years ago
parent
commit
299391fea7
2 changed files with 41 additions and 26 deletions
  1. +25
    -16
      pyad2usb/ad2usb.py
  2. +16
    -10
      test.py

+ 25
- 16
pyad2usb/ad2usb.py View File

@@ -29,14 +29,13 @@ class AD2USB(object):
self._device = None self._device = None


AD2USB.find_all() AD2USB.find_all()
pass


def __del__(self): def __del__(self):
pass pass


def open(self, device=None): def open(self, device=None):
if len(self.__devices) == 0: if len(self.__devices) == 0:
raise NoDeviceError
raise NoDeviceError('No AD2USB devices present.')


if device is None: if device is None:
device = self.__devices[0] device = self.__devices[0]
@@ -75,7 +74,7 @@ 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: except usb.core.USBError, e:
print e
raise


return devices return devices


@@ -93,17 +92,22 @@ class Device(object):
def open(self, baudrate=BAUDRATE, interface=0, index=0): def open(self, baudrate=BAUDRATE, interface=0, index=0):
self._running = True self._running = True


self._device.open(self._vendor_id,
self._product_id,
interface,
index,
self._serial_number,
self._description)

self._device.set_baudrate(baudrate)
self._read_thread.start()
try:
self._device.open(self._vendor_id,
self._product_id,
interface,
index,
self._serial_number,
self._description)

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


self.on_open((self._serial_number, self._description))
self.on_open((self._serial_number, self._description))


def close(self): def close(self):
try: try:
@@ -111,7 +115,7 @@ class Device(object):
self._read_thread.stop() self._read_thread.stop()


self._device.close() self._device.close()
except FtdiError, e:
except (FtdiError, usb.core.USBError):
pass pass


self.on_close() self.on_close()
@@ -148,7 +152,8 @@ class Device(object):


time.sleep(0.01) time.sleep(0.01)
except FtdiError, e: except FtdiError, e:
pass
# TODO: I don't think we should be ignoring this
raise
else: else:
if got_line: if got_line:
ret = self._buffer ret = self._buffer
@@ -171,6 +176,10 @@ class Device(object):
self._running = True self._running = True


while self._running: while self._running:
self._device.read_line()
try:
self._device.read_line()
except (usb.core.USBError, FtdiError):
self.stop()
self._device.close()


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

+ 16
- 10
test.py View File

@@ -1,6 +1,9 @@
#!/usr/bin/env python

import pyad2usb.ad2usb import pyad2usb.ad2usb
import time import time
import signal import signal
import traceback


running = True running = True


@@ -23,17 +26,20 @@ def handle_write(sender, args):


signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGINT, signal_handler)


#pyad2usb.ad2usb.AD2USB.find_all()
try:
wut = pyad2usb.ad2usb.AD2USB()


wut = pyad2usb.ad2usb.AD2USB()
wut.on_open += handle_open
wut.on_close += handle_close
wut.on_read += handle_read
wut.on_write += handle_write
wut.on_open += handle_open
wut.on_close += handle_close
wut.on_read += handle_read
wut.on_write += handle_write


wut.open()
wut.open()


while running:
time.sleep(0.1)
while running:
time.sleep(0.1)


wut.close()
wut.close()
except Exception, err:
print 'Error: {0}'.format(str(err))
#traceback.print_exc(err)

Loading…
Cancel
Save