Skip to content
David Bonnes edited this page Jan 5, 2020 · 3 revisions

The basic packet structure is:

aaa XX bbb DEVICE_ID DEVICE_ID DEVICE_ID CODE nnn PAYLOAD.....

The first step of processing a packet is to determine if a packet is valid. Only then is it worth decoding the payload.

The elements of message (received) packets and command (sent) packets include the following fields, represented here as regex expressions (using Python):

a = "(-{3}|\d{3})"             # RSSI value or sequence number
b = "( I|RP|RQ| W)"            # packet type (note the space before the I, W)
c = "(-{2}:-{6}|\d{2}:\d{6})"  # device Id (each packet has three)
d = "[0-9A-F]{4}"              # command code
e = "\d{3}"                    # payload length, in bytes
f = "([0-9A-F]{2})+"           # payload, 1-48 bytes in length

Messages

The packet structure for (received) messages is:

MESSAGE_REGEX = re.compile(f"^{a} {b} {a} {c} {c} {c} {d} {e} {f}$")

Unlike commands, they include an RSSI field. Thus, an example of packet validation could be:

if not MESSAGE_REGEX.match(raw_packet):
    raise ValueError(f"Packet structure is not valid, >> {raw_packet} <<")

if len(raw_packet[50:]) != 2 * int(raw_packet[46:49]):
    raise ValueError(f"Packet payload length is incorrect, >> {raw_packet} <<")

if int(raw_packet[46:49]) > 48:
    raise ValueError(f"Packet payload length excessive, >> {raw_packet} <<"t)

Commands

The packet structure for (sent) commands should be:

COMMAND_REGEX = re.compile(f"^{b} {a} {c} {c} {c} {d} {e} {f}$")

Sent packets do not have a RSSI field. Note that for commands useful types (field {b}, above) are probably restricted to RQ and W.

Clone this wiki locally