Browse Source

A few more fixes for supporting Python 2 and 3.

pyserial_fix
Scott Petersen 7 years ago
parent
commit
d69a21056c
4 changed files with 26 additions and 18 deletions
  1. +14
    -7
      alarmdecoder/devices.py
  2. +4
    -4
      alarmdecoder/util.py
  3. +7
    -6
      bin/ad2-firmwareupload
  4. +1
    -1
      setup.py

+ 14
- 7
alarmdecoder/devices.py View File

@@ -800,6 +800,10 @@ class SerialDevice(Device):
:raises: py:class:`~alarmdecoder.util.CommError`
"""
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)

except serial.SerialTimeoutException:
@@ -821,7 +825,7 @@ class SerialDevice(Device):
ret = None

try:
ret = self._device.read(1)
ret = self._device.read(1).decode('utf-8')

except serial.SerialException as err:
raise CommError('Error reading from device: {0}'.format(str(err)), err)
@@ -890,7 +894,7 @@ class SerialDevice(Device):
finally:
timer.cancel()

return ret
return ret.decode('utf-8')

def purge(self):
"""
@@ -1096,6 +1100,9 @@ class SocketDevice(Device):
data_sent = None

try:
if isinstance(data, str):
data = data.encode('utf-8')

data_sent = self._device.send(data)

if data_sent == 0:
@@ -1118,7 +1125,7 @@ class SocketDevice(Device):
data = None

try:
read_ready, _, _ = select.select([self._device], [], [], 0)
read_ready, _, _ = select.select([self._device], [], [])

if (len(read_ready) != 0):
data = self._device.recv(1)
@@ -1126,7 +1133,7 @@ class SocketDevice(Device):
except socket.error as 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):
"""
@@ -1158,7 +1165,7 @@ class SocketDevice(Device):

try:
while timeout_event.reading:
read_ready, _, _ = select.select([self._device], [], [], 0)
read_ready, _, _ = select.select([self._device], [], [])

if (len(read_ready) == 0):
time.sleep(0.01)
@@ -1166,7 +1173,7 @@ class SocketDevice(Device):

buf = self._device.recv(1)

if buf != b'':
if buf != b'' and buf != b"\xff":
ub = bytes_hack(buf)

self._buffer += ub
@@ -1203,7 +1210,7 @@ class SocketDevice(Device):
finally:
timer.cancel()

return ret
return ret.decode('utf-8')

def purge(self):
"""


+ 4
- 4
alarmdecoder/util.py View File

@@ -94,7 +94,7 @@ class Firmware(object):

if line[0] == ':':
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:
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:
try:
char = dev.read()
char = dev.read() #.decode('utf-8')

if char is not None and char != '':
if char == pattern[position]:
@@ -179,13 +179,13 @@ class Firmware(object):
try:
stage_callback(Firmware.STAGE_BOOT)
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
# that it's ready for the firmware upload.
stage_callback(Firmware.STAGE_LOAD)
dev.write("=")
read_until('!load', timeout=15.0)
read_until(u'!load', timeout=15.0)

except TimeoutError as err:
retry -= 1


+ 7
- 6
bin/ad2-firmwareupload View File

@@ -2,6 +2,7 @@

import os
import sys, time
import traceback
import alarmdecoder

def handle_firmware(stage, **kwargs):
@@ -68,12 +69,12 @@ def main():
time.sleep(3)
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:
if dev is not None:
dev.close()


+ 1
- 1
setup.py View File

@@ -14,7 +14,7 @@ if sys.version_info < (3,):
extra_requirements.append('future==0.14.3')

setup(name='alarmdecoder',
version='0.10.3',
version='0.12.2',
description='Python interface for the AlarmDecoder (AD2) family '
'of alarm devices which includes the AD2USB, AD2SERIAL and AD2PI.',
long_description=readme(),


Loading…
Cancel
Save