Skip to content

Commit d809cb3

Browse files
committed
Testing new serial reading code.
1 parent a3ca3cd commit d809cb3

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

alarmdecoder/devices/serial_device.py

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import select
1515
import sys
1616
from .base_device import Device
17-
from ..util import CommError, TimeoutError, NoDeviceError, bytes_hack
17+
from ..util import CommError, TimeoutError, NoDeviceError, bytes_hack, filter_ad2prot_byte
1818

1919

2020
class SerialDevice(Device):
@@ -141,7 +141,7 @@ def close(self):
141141
def fileno(self):
142142
"""
143143
Returns the file number associated with the device
144-
144+
145145
:returns: int
146146
"""
147147
return self._device.fileno()
@@ -178,18 +178,18 @@ def read(self):
178178
:returns: character read from the device
179179
:raises: :py:class:`~alarmdecoder.util.CommError`
180180
"""
181-
data = ''
181+
data = b''
182182

183183
try:
184184
read_ready, _, _ = select.select([self._device.fileno()], [], [], 0.5)
185185

186186
if len(read_ready) != 0:
187-
data = self._device.read(1)
187+
data = filter_ad2prot_byte(self._device.read(1))
188188

189189
except serial.SerialException as err:
190190
raise CommError('Error reading from device: {0}'.format(str(err)), err)
191191

192-
return data.decode('utf-8')
192+
return data
193193

194194
def read_line(self, timeout=0.0, purge_buffer=False):
195195
"""
@@ -213,62 +213,46 @@ def timeout_event():
213213
if purge_buffer:
214214
self._buffer = b''
215215

216-
got_line, data = False, ''
216+
got_line, ret = False, None
217217

218218
timer = threading.Timer(timeout, timeout_event)
219219
if timeout > 0:
220220
timer.start()
221221

222-
leftovers = b''
223222
try:
224-
while timeout_event.reading and not got_line:
223+
while timeout_event.reading:
225224
read_ready, _, _ = select.select([self._device.fileno()], [], [], 0.5)
225+
226226
if len(read_ready) == 0:
227227
continue
228228

229-
bytes_avail = 0
230-
if hasattr(self._device, "in_waiting"):
231-
bytes_avail = self._device.in_waiting
232-
else:
233-
bytes_avail = self._device.inWaiting()
234-
235-
buf = self._device.read(bytes_avail)
236-
237-
for idx in range(len(buf)):
238-
c = buf[idx]
239-
240-
ub = bytes_hack(c)
241-
if sys.version_info > (3,):
242-
ub = bytes([ub])
243-
244-
# NOTE: AD2SERIAL and AD2PI apparently sends down \xFF on boot.
245-
if ub != b'' and ub != b"\xff":
246-
self._buffer += ub
229+
buf = filter_ad2prot_byte(self._device.read(1))
247230

248-
if ub == b"\n":
249-
self._buffer = self._buffer.strip(b"\r\n")
231+
if buf != b'':
232+
self._buffer += buf[0]
250233

251-
if len(self._buffer) > 0:
252-
got_line = True
253-
leftovers = buf[idx:]
254-
break
234+
if buf == b"\n":
235+
self._buffer = self._buffer.rstrip(b"\r\n")
255236

237+
if len(self._buffer) > 0:
238+
got_line = True
239+
break
256240
except (OSError, serial.SerialException) as err:
257241
raise CommError('Error reading from device: {0}'.format(str(err)), err)
258242

259243
else:
260244
if got_line:
261-
data, self._buffer = self._buffer, leftovers
245+
ret, self._buffer = self._buffer, b''
262246

263-
self.on_read(data=data)
247+
self.on_read(data=ret)
264248

265249
else:
266250
raise TimeoutError('Timeout while waiting for line terminator.')
267251

268252
finally:
269253
timer.cancel()
270254

271-
return data.decode('utf-8')
255+
return ret
272256

273257
def purge(self):
274258
"""

alarmdecoder/util.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ def bytes_hack(buf):
9393

9494
return ub
9595

96+
def filter_ad2prot_byte(buf):
97+
"""
98+
Return the byte sent in back if valid visible terminal characters or line terminators.
99+
"""
100+
c = buf[0];
101+
102+
if (c == '\n' or c == '\r'):
103+
return c
104+
if (c > 31 and c < 127):
105+
return c
106+
else:
107+
return ''
108+
96109
def read_firmware_file(file_path):
97110
"""
98111
Reads a firmware file into a dequeue for processing.

0 commit comments

Comments
 (0)