|
|
@@ -61,7 +61,7 @@ class Device(object): |
|
|
|
Constructor |
|
|
|
""" |
|
|
|
self._id = '' |
|
|
|
self._buffer = '' |
|
|
|
self._buffer = b'' |
|
|
|
self._device = None |
|
|
|
self._running = False |
|
|
|
self._read_thread = None |
|
|
@@ -171,10 +171,10 @@ class Device(object): |
|
|
|
except SSL.WantReadError: |
|
|
|
pass |
|
|
|
|
|
|
|
except CommError, err: |
|
|
|
except CommError: |
|
|
|
self._device.close() |
|
|
|
|
|
|
|
except Exception, err: |
|
|
|
except Exception: |
|
|
|
self._device.close() |
|
|
|
self._running = False |
|
|
|
raise |
|
|
@@ -226,7 +226,7 @@ class USBDevice(Device): |
|
|
|
try: |
|
|
|
cls.__devices = Ftdi.find_all(query, nocache=True) |
|
|
|
|
|
|
|
except (usb.core.USBError, FtdiError), err: |
|
|
|
except (usb.core.USBError, FtdiError) as err: |
|
|
|
raise CommError('Error enumerating AD2USB devices: {0}'.format(str(err)), err) |
|
|
|
|
|
|
|
return cls.__devices |
|
|
@@ -433,10 +433,10 @@ class USBDevice(Device): |
|
|
|
|
|
|
|
self._id = self._serial_number |
|
|
|
|
|
|
|
except (usb.core.USBError, FtdiError), err: |
|
|
|
except (usb.core.USBError, FtdiError) as err: |
|
|
|
raise NoDeviceError('Error opening device: {0}'.format(str(err)), err) |
|
|
|
|
|
|
|
except KeyError, err: |
|
|
|
except KeyError as err: |
|
|
|
raise NoDeviceError('Unsupported device. ({0:04x}:{1:04x}) You probably need a newer version of pyftdi.'.format(err[0][0], err[0][1])) |
|
|
|
|
|
|
|
else: |
|
|
@@ -478,7 +478,7 @@ class USBDevice(Device): |
|
|
|
|
|
|
|
self.on_write(data=data) |
|
|
|
|
|
|
|
except FtdiError, err: |
|
|
|
except FtdiError as err: |
|
|
|
raise CommError('Error writing to device: {0}'.format(str(err)), err) |
|
|
|
|
|
|
|
def read(self): |
|
|
@@ -493,7 +493,7 @@ class USBDevice(Device): |
|
|
|
try: |
|
|
|
ret = self._device.read_data(1) |
|
|
|
|
|
|
|
except (usb.core.USBError, FtdiError), err: |
|
|
|
except (usb.core.USBError, FtdiError) as err: |
|
|
|
raise CommError('Error reading from device: {0}'.format(str(err)), err) |
|
|
|
|
|
|
|
return ret |
|
|
@@ -518,7 +518,7 @@ class USBDevice(Device): |
|
|
|
timeout_event.reading = True |
|
|
|
|
|
|
|
if purge_buffer: |
|
|
|
self._buffer = '' |
|
|
|
self._buffer = b'' |
|
|
|
|
|
|
|
got_line, ret = False, None |
|
|
|
|
|
|
@@ -530,11 +530,11 @@ class USBDevice(Device): |
|
|
|
while timeout_event.reading: |
|
|
|
buf = self._device.read_data(1) |
|
|
|
|
|
|
|
if buf != '': |
|
|
|
if buf != b'': |
|
|
|
self._buffer += buf |
|
|
|
|
|
|
|
if buf == "\n": |
|
|
|
self._buffer = self._buffer.rstrip("\r\n") |
|
|
|
if buf == b"\n": |
|
|
|
self._buffer = self._buffer.rstrip(b"\r\n") |
|
|
|
|
|
|
|
if len(self._buffer) > 0: |
|
|
|
got_line = True |
|
|
@@ -542,12 +542,12 @@ class USBDevice(Device): |
|
|
|
else: |
|
|
|
time.sleep(0.01) |
|
|
|
|
|
|
|
except (usb.core.USBError, FtdiError), err: |
|
|
|
except (usb.core.USBError, FtdiError) as err: |
|
|
|
raise CommError('Error reading from device: {0}'.format(str(err)), err) |
|
|
|
|
|
|
|
else: |
|
|
|
if got_line: |
|
|
|
ret, self._buffer = self._buffer, '' |
|
|
|
ret, self._buffer = self._buffer, b'' |
|
|
|
|
|
|
|
self.on_read(data=ret) |
|
|
|
|
|
|
@@ -653,7 +653,7 @@ class SerialDevice(Device): |
|
|
|
else: |
|
|
|
devices = serial.tools.list_ports.comports() |
|
|
|
|
|
|
|
except serial.SerialException, err: |
|
|
|
except serial.SerialException as err: |
|
|
|
raise CommError('Error enumerating serial devices: {0}'.format(str(err)), err) |
|
|
|
|
|
|
|
return devices |
|
|
@@ -724,7 +724,7 @@ class SerialDevice(Device): |
|
|
|
# all issues with it. |
|
|
|
self._device.baudrate = baudrate |
|
|
|
|
|
|
|
except (serial.SerialException, ValueError, OSError), err: |
|
|
|
except (serial.SerialException, ValueError, OSError) as err: |
|
|
|
raise NoDeviceError('Error opening device on {0}.'.format(self._port), err) |
|
|
|
|
|
|
|
else: |
|
|
@@ -764,7 +764,7 @@ class SerialDevice(Device): |
|
|
|
except serial.SerialTimeoutException: |
|
|
|
pass |
|
|
|
|
|
|
|
except serial.SerialException, err: |
|
|
|
except serial.SerialException as err: |
|
|
|
raise CommError('Error writing to device.', err) |
|
|
|
|
|
|
|
else: |
|
|
@@ -782,7 +782,7 @@ class SerialDevice(Device): |
|
|
|
try: |
|
|
|
ret = self._device.read(1) |
|
|
|
|
|
|
|
except serial.SerialException, err: |
|
|
|
except serial.SerialException as err: |
|
|
|
raise CommError('Error reading from device: {0}'.format(str(err)), err) |
|
|
|
|
|
|
|
return ret |
|
|
@@ -807,7 +807,7 @@ class SerialDevice(Device): |
|
|
|
timeout_event.reading = True |
|
|
|
|
|
|
|
if purge_buffer: |
|
|
|
self._buffer = '' |
|
|
|
self._buffer = b'' |
|
|
|
|
|
|
|
got_line, ret = False, None |
|
|
|
|
|
|
@@ -820,11 +820,11 @@ class SerialDevice(Device): |
|
|
|
buf = self._device.read(1) |
|
|
|
|
|
|
|
# NOTE: AD2SERIAL apparently sends down \xFF on boot. |
|
|
|
if buf != '' and buf != "\xff": |
|
|
|
if buf != b'' and buf != b"\xff": |
|
|
|
self._buffer += buf |
|
|
|
|
|
|
|
if buf == "\n": |
|
|
|
self._buffer = self._buffer.rstrip("\r\n") |
|
|
|
if buf == b"\n": |
|
|
|
self._buffer = self._buffer.rstrip(b"\r\n") |
|
|
|
|
|
|
|
if len(self._buffer) > 0: |
|
|
|
got_line = True |
|
|
@@ -832,12 +832,12 @@ class SerialDevice(Device): |
|
|
|
else: |
|
|
|
time.sleep(0.01) |
|
|
|
|
|
|
|
except (OSError, serial.SerialException), err: |
|
|
|
except (OSError, serial.SerialException) as err: |
|
|
|
raise CommError('Error reading from device: {0}'.format(str(err)), err) |
|
|
|
|
|
|
|
else: |
|
|
|
if got_line: |
|
|
|
ret, self._buffer = self._buffer, '' |
|
|
|
ret, self._buffer = self._buffer, b'' |
|
|
|
|
|
|
|
self.on_read(data=ret) |
|
|
|
|
|
|
@@ -1001,7 +1001,7 @@ class SocketDevice(Device): |
|
|
|
|
|
|
|
self._id = '{0}:{1}'.format(self._host, self._port) |
|
|
|
|
|
|
|
except socket.error, err: |
|
|
|
except socket.error as err: |
|
|
|
raise NoDeviceError('Error opening device at {0}:{1}'.format(self._host, self._port), err) |
|
|
|
|
|
|
|
else: |
|
|
@@ -1054,7 +1054,7 @@ class SocketDevice(Device): |
|
|
|
|
|
|
|
self.on_write(data=data) |
|
|
|
|
|
|
|
except (SSL.Error, socket.error), err: |
|
|
|
except (SSL.Error, socket.error) as err: |
|
|
|
raise CommError('Error writing to device.', err) |
|
|
|
|
|
|
|
return data_sent |
|
|
@@ -1071,7 +1071,7 @@ class SocketDevice(Device): |
|
|
|
try: |
|
|
|
data = self._device.recv(1) |
|
|
|
|
|
|
|
except socket.error, err: |
|
|
|
except socket.error as err: |
|
|
|
raise CommError('Error while reading from device: {0}'.format(str(err)), err) |
|
|
|
|
|
|
|
return data |
|
|
@@ -1096,7 +1096,7 @@ class SocketDevice(Device): |
|
|
|
timeout_event.reading = True |
|
|
|
|
|
|
|
if purge_buffer: |
|
|
|
self._buffer = '' |
|
|
|
self._buffer = b'' |
|
|
|
|
|
|
|
got_line, ret = False, None |
|
|
|
|
|
|
@@ -1108,11 +1108,11 @@ class SocketDevice(Device): |
|
|
|
while timeout_event.reading: |
|
|
|
buf = self._device.recv(1) |
|
|
|
|
|
|
|
if buf != '': |
|
|
|
if buf != b'': |
|
|
|
self._buffer += buf |
|
|
|
|
|
|
|
if buf == "\n": |
|
|
|
self._buffer = self._buffer.rstrip("\r\n") |
|
|
|
if buf == b"\n": |
|
|
|
self._buffer = self._buffer.rstrip(b"\r\n") |
|
|
|
|
|
|
|
if len(self._buffer) > 0: |
|
|
|
got_line = True |
|
|
@@ -1120,10 +1120,10 @@ class SocketDevice(Device): |
|
|
|
else: |
|
|
|
time.sleep(0.01) |
|
|
|
|
|
|
|
except socket.error, err: |
|
|
|
except socket.error as err: |
|
|
|
raise CommError('Error reading from device: {0}'.format(str(err)), err) |
|
|
|
|
|
|
|
except SSL.SysCallError, err: |
|
|
|
except SSL.SysCallError as err: |
|
|
|
errno, msg = err |
|
|
|
raise CommError('SSL error while reading from device: {0} ({1})'.format(msg, errno)) |
|
|
|
|
|
|
@@ -1132,7 +1132,7 @@ class SocketDevice(Device): |
|
|
|
|
|
|
|
else: |
|
|
|
if got_line: |
|
|
|
ret, self._buffer = self._buffer, '' |
|
|
|
ret, self._buffer = self._buffer, b'' |
|
|
|
|
|
|
|
self.on_read(data=ret) |
|
|
|
|
|
|
@@ -1177,7 +1177,7 @@ class SocketDevice(Device): |
|
|
|
|
|
|
|
self._device = SSL.Connection(ctx, self._device) |
|
|
|
|
|
|
|
except SSL.Error, err: |
|
|
|
except SSL.Error as err: |
|
|
|
raise CommError('Error setting up SSL connection.', err) |
|
|
|
|
|
|
|
def _verify_ssl_callback(self, connection, x509, errnum, errdepth, ok): |
|
|
|