@@ -800,6 +800,10 @@ class SerialDevice(Device): | |||||
:raises: py:class:`~alarmdecoder.util.CommError` | :raises: py:class:`~alarmdecoder.util.CommError` | ||||
""" | """ | ||||
try: | try: | ||||
# Hack to support unicode under Python 2.x | |||||
if isinstance(data, str) or (sys.version_info < (3,) and isinstance(data, unicode)): | |||||
data = data.encode('utf-8') | |||||
self._device.write(data) | self._device.write(data) | ||||
except serial.SerialTimeoutException: | except serial.SerialTimeoutException: | ||||
@@ -821,7 +825,7 @@ class SerialDevice(Device): | |||||
ret = None | ret = None | ||||
try: | try: | ||||
ret = self._device.read(1) | |||||
ret = self._device.read(1).decode('utf-8') | |||||
except serial.SerialException as err: | except serial.SerialException as err: | ||||
raise CommError('Error reading from device: {0}'.format(str(err)), err) | raise CommError('Error reading from device: {0}'.format(str(err)), err) | ||||
@@ -890,7 +894,7 @@ class SerialDevice(Device): | |||||
finally: | finally: | ||||
timer.cancel() | timer.cancel() | ||||
return ret | |||||
return ret.decode('utf-8') | |||||
def purge(self): | def purge(self): | ||||
""" | """ | ||||
@@ -1096,6 +1100,9 @@ class SocketDevice(Device): | |||||
data_sent = None | data_sent = None | ||||
try: | try: | ||||
if isinstance(data, str): | |||||
data = data.encode('utf-8') | |||||
data_sent = self._device.send(data) | data_sent = self._device.send(data) | ||||
if data_sent == 0: | if data_sent == 0: | ||||
@@ -1118,7 +1125,7 @@ class SocketDevice(Device): | |||||
data = None | data = None | ||||
try: | try: | ||||
read_ready, _, _ = select.select([self._device], [], [], 0) | |||||
read_ready, _, _ = select.select([self._device], [], []) | |||||
if (len(read_ready) != 0): | if (len(read_ready) != 0): | ||||
data = self._device.recv(1) | data = self._device.recv(1) | ||||
@@ -1126,7 +1133,7 @@ class SocketDevice(Device): | |||||
except socket.error as err: | except socket.error as err: | ||||
raise CommError('Error while reading from device: {0}'.format(str(err)), err) | raise CommError('Error while reading from device: {0}'.format(str(err)), err) | ||||
return data | |||||
return data.decode('utf-8') | |||||
def read_line(self, timeout=0.0, purge_buffer=False): | def read_line(self, timeout=0.0, purge_buffer=False): | ||||
""" | """ | ||||
@@ -1158,7 +1165,7 @@ class SocketDevice(Device): | |||||
try: | try: | ||||
while timeout_event.reading: | while timeout_event.reading: | ||||
read_ready, _, _ = select.select([self._device], [], [], 0) | |||||
read_ready, _, _ = select.select([self._device], [], []) | |||||
if (len(read_ready) == 0): | if (len(read_ready) == 0): | ||||
time.sleep(0.01) | time.sleep(0.01) | ||||
@@ -1166,7 +1173,7 @@ class SocketDevice(Device): | |||||
buf = self._device.recv(1) | buf = self._device.recv(1) | ||||
if buf != b'': | |||||
if buf != b'' and buf != b"\xff": | |||||
ub = bytes_hack(buf) | ub = bytes_hack(buf) | ||||
self._buffer += ub | self._buffer += ub | ||||
@@ -1203,7 +1210,7 @@ class SocketDevice(Device): | |||||
finally: | finally: | ||||
timer.cancel() | timer.cancel() | ||||
return ret | |||||
return ret.decode('utf-8') | |||||
def purge(self): | def purge(self): | ||||
""" | """ | ||||
@@ -94,7 +94,7 @@ class Firmware(object): | |||||
if line[0] == ':': | if line[0] == ':': | ||||
dev.write(line + "\r") | dev.write(line + "\r") | ||||
response = dev.read_line(timeout=5.0, purge_buffer=True) | |||||
response = dev.read_line(timeout=5.0, purge_buffer=True) #.decode('utf-8') | |||||
if debug: | if debug: | ||||
stage_callback(Firmware.STAGE_DEBUG, data="line={0} - line={1} response={2}".format(line_cnt, line, response)); | stage_callback(Firmware.STAGE_DEBUG, data="line={0} - line={1} response={2}".format(line_cnt, line, response)); | ||||
@@ -135,7 +135,7 @@ class Firmware(object): | |||||
while timeout_event.reading: | while timeout_event.reading: | ||||
try: | try: | ||||
char = dev.read() | |||||
char = dev.read() #.decode('utf-8') | |||||
if char is not None and char != '': | if char is not None and char != '': | ||||
if char == pattern[position]: | if char == pattern[position]: | ||||
@@ -179,13 +179,13 @@ class Firmware(object): | |||||
try: | try: | ||||
stage_callback(Firmware.STAGE_BOOT) | stage_callback(Firmware.STAGE_BOOT) | ||||
dev.write("=") | dev.write("=") | ||||
read_until('!boot', timeout=15.0) | |||||
read_until(u'!boot', timeout=15.0) | |||||
# Get ourselves into the boot loader and wait for indication | # Get ourselves into the boot loader and wait for indication | ||||
# that it's ready for the firmware upload. | # that it's ready for the firmware upload. | ||||
stage_callback(Firmware.STAGE_LOAD) | stage_callback(Firmware.STAGE_LOAD) | ||||
dev.write("=") | dev.write("=") | ||||
read_until('!load', timeout=15.0) | |||||
read_until(u'!load', timeout=15.0) | |||||
except TimeoutError as err: | except TimeoutError as err: | ||||
retry -= 1 | retry -= 1 | ||||
@@ -2,6 +2,7 @@ | |||||
import os | import os | ||||
import sys, time | import sys, time | ||||
import traceback | |||||
import alarmdecoder | import alarmdecoder | ||||
def handle_firmware(stage, **kwargs): | def handle_firmware(stage, **kwargs): | ||||
@@ -68,12 +69,12 @@ def main(): | |||||
time.sleep(3) | time.sleep(3) | ||||
alarmdecoder.util.Firmware.upload(dev, firmware, handle_firmware, debug=debug) | alarmdecoder.util.Firmware.upload(dev, firmware, handle_firmware, debug=debug) | ||||
except alarmdecoder.util.NoDeviceError, ex: | |||||
print "Error: Could not find device: {0}".format(ex) | |||||
except alarmdecoder.util.UploadError, ex: | |||||
print "Error: Error uploading firmware: {0}".format(ex) | |||||
except Exception, ex: | |||||
print "Error: {0}".format(ex) | |||||
except alarmdecoder.util.NoDeviceError as ex: | |||||
print("Error: Could not find device: {0}".format(ex)) | |||||
except alarmdecoder.util.UploadError as ex: | |||||
print("Error: Error uploading firmware: {0}".format(ex)) | |||||
except Exception as ex: | |||||
print("Error: {0}: {1}".format(ex, traceback.format_exc())) | |||||
finally: | finally: | ||||
if dev is not None: | if dev is not None: | ||||
dev.close() | dev.close() | ||||
@@ -14,7 +14,7 @@ if sys.version_info < (3,): | |||||
extra_requirements.append('future==0.14.3') | extra_requirements.append('future==0.14.3') | ||||
setup(name='alarmdecoder', | setup(name='alarmdecoder', | ||||
version='0.10.3', | |||||
version='0.12.2', | |||||
description='Python interface for the AlarmDecoder (AD2) family ' | description='Python interface for the AlarmDecoder (AD2) family ' | ||||
'of alarm devices which includes the AD2USB, AD2SERIAL and AD2PI.', | 'of alarm devices which includes the AD2USB, AD2SERIAL and AD2PI.', | ||||
long_description=readme(), | long_description=readme(), | ||||