Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions gsmmodem/serial_comms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SerialComms(object):
# End-of-line read terminator
RX_EOL_SEQ = b'\r\n'
# End-of-response terminator
RESPONSE_TERM = re.compile('^OK|ERROR|(\+CM[ES] ERROR: \d+)|(COMMAND NOT SUPPORT)$')
RESPONSE_TERM = re.compile(r'^OK|ERROR|(\+CM[ES] ERROR: \d+)|(COMMAND NOT SUPPORT)$')
# Default timeout for serial port reads (in seconds)
timeout = 1

Expand Down Expand Up @@ -84,6 +84,15 @@ def _handleLineRead(self, line, checkForResponseTerm=True):
def _placeholderCallback(self, *args, **kwargs):
""" Placeholder callback function (does nothing) """

def safe_decode(data):
try:
return data.decode('utf-8')
except UnicodeDecodeError:
try:
return data.decode('latin-1')
except Exception:
return data.decode('utf-8', errors='ignore')

def _readLoop(self):
""" Read thread main loop
Expand All @@ -100,7 +109,7 @@ def _readLoop(self):
rxBuffer.append(ord(data))
if rxBuffer[-readTermLen:] == readTermSeq:
# A line (or other logical segment) has been read
line = rxBuffer[:-readTermLen].decode()
line = self.safe_decode(rxBuffer[:-readTermLen])
rxBuffer = bytearray()
if len(line) > 0:
#print 'calling handler'
Expand Down