Skip to content

Commit 8f0eee4

Browse files
committed
fix #13: handle the case where the checksum byte is escaped when working in API escaped mode
1 parent 1d4d426 commit 8f0eee4

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

digi/xbee/reader.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,19 @@ def __execute_user_callbacks(self, xbee_packet, remote=None):
625625
sender=str(xbee_packet.phone_number),
626626
more_data=xbee_packet.data))
627627

628+
def __read_byte_into_packet(self, xbee_packet, operating_mode):
629+
"""
630+
Reads the next byte and appends it to ``xbee_packet``.
631+
632+
If in escaped API mode and the byte that was read was the escape byte,
633+
it will also read the next byte.
634+
"""
635+
read_byte = self.__serial_port.read_byte()
636+
xbee_packet.append(read_byte)
637+
# Read escaped bytes in API escaped mode.
638+
if operating_mode == OperatingMode.ESCAPED_API_MODE and read_byte == XBeePacket.ESCAPE_BYTE:
639+
xbee_packet.append(self.__serial_port.read_byte())
640+
628641
def __try_read_packet(self, operating_mode=OperatingMode.API_MODE):
629642
"""
630643
Reads the next packet. Starts to read when finds the start delimiter.
@@ -649,15 +662,9 @@ def __try_read_packet(self, operating_mode=OperatingMode.API_MODE):
649662
# Add packet length.
650663
xbee_packet += packet_length
651664
length = utils.length_to_int(packet_length)
652-
# Add packet payload.
653-
for _ in range(0, length):
654-
read_byte = self.__serial_port.read_byte()
655-
xbee_packet.append(read_byte)
656-
# Read escaped bytes in API escaped mode.
657-
if operating_mode == OperatingMode.ESCAPED_API_MODE and read_byte == XBeePacket.ESCAPE_BYTE:
658-
xbee_packet.append(self.__serial_port.read_byte())
659-
# Add packet checksum.
660-
xbee_packet.append(self.__serial_port.read_byte())
665+
# Add packet payload and checksum.
666+
for _ in range(0, length + 1):
667+
self.__read_byte_into_packet(xbee_packet, operating_mode)
661668
return xbee_packet
662669
except TimeoutException:
663670
return None

0 commit comments

Comments
 (0)