7 Represents an API frame for communicating with an XBee 13 Represents a frame of data to be sent to or which was received 21 ESCAPE_BYTES = (START_BYTE, ESCAPE_BYTE, XON_BYTE, XOFF_BYTE)
31 checksum: None -> single checksum byte 33 checksum adds all bytes of the binary, unescaped data in the 34 frame, saves the last byte of the result, and subtracts it from 35 0xFF. The final result is the checksum 40 for byte
in self.
data:
47 return chr(0xFF - total)
51 verify: 1 byte -> boolean 53 verify checksums the frame, adds the expected checksum, and 54 determines whether the result is correct. The result should 60 for byte
in self.
data:
74 len_data: None -> (MSB, LSB) 16-bit integer length, two bytes 76 len_bytes counts the number of bytes to be sent and encodes the 77 data length in two bytes, big-endian (most significant first). 79 count = len(self.
data)
80 return struct.pack(
"> h", count)
84 output: None -> valid API frame (binary data) 86 output will produce a valid API frame for transmission to an 96 self.
raw_data = APIFrame.escape(data)
102 return APIFrame.START_BYTE + data
107 escape: byte string -> byte string 109 When a 'special' byte is encountered in the given data string, 110 it is preceded by an escape byte and XORed with 0x20. 115 if byte
in APIFrame.ESCAPE_BYTES:
116 escaped_data += APIFrame.ESCAPE_BYTE
117 escaped_data += chr(0x20 ^ ord(byte))
127 Adds the given raw byte to this APIFrame. If this APIFrame is marked 128 as escaped and this byte is an escape byte, the next byte in a call 129 to fill() will be unescaped. 133 byte = chr(ord(byte) ^ 0x20)
135 elif self.
escaped and byte == APIFrame.ESCAPE_BYTE:
147 data_len = struct.unpack(
"> h", raw_len)[0]
149 remaining += data_len
154 return remaining - len(self.
raw_data)
160 Given a valid API frame, parse extracts the data contained 161 inside it and verifies it against its checksum 164 ValueError(
"parse() may only be called on a frame containing at least 3 bytes of raw data (see fill())")
170 data_len = struct.unpack(
"> h", raw_len)[0]
173 data = self.
raw_data[3:3 + data_len]
178 if not self.
verify(chksum):
179 raise ValueError(
"Invalid checksum")
def remaining_bytes(self)
def __init__(self, data='', escaped=False)