def atcom_set(param, value): # Header bytestr = b'\x7e' # Length length = 4 + len(value) bytestr = bytestr + (length).to_bytes(2, 'big') # Frame type ftype = b'\x08' bytestr = bytestr + ftype # Frame ID fid = b'\x01' bytestr = bytestr + fid checksum = byte_sum(ftype, fid) # AT command athi = bytes(param[0], 'ascii') atlo = bytes(param[1], 'ascii') bytestr = bytestr + athi + atlo checksum = byte_sum(checksum, athi) checksum = byte_sum(checksum, atlo) # Parameter value bytestr = bytestr + value for i in range(len(value)): checksum = byte_sum(checksum, (value[i]).to_bytes(1, 'big')) # Checksum checksum = byte_diff0xff(checksum) bytestr = bytestr + checksum return bytestr
def atcom_query(param): # Header bytestr = b'\x7e' # Length length = 4 bytestr = bytestr + (length).to_bytes(2, 'big') # Frame type ftype = b'\x08' bytestr = bytestr + ftype # Frame ID fid = b'\x01' bytestr = bytestr + fid checksum = byte_sum(ftype, fid) # AT command athi = bytes(param[0], 'ascii') atlo = bytes(param[1], 'ascii') bytestr = bytestr + athi + atlo checksum = byte_sum(checksum, athi) checksum = byte_sum(checksum, atlo) # Checksum checksum = byte_diff0xff(checksum) bytestr = bytestr + checksum return bytestr
def rxpacket(ser): payload = b'' # Header: 0x7e header = ser.read(1) if header != b'\x7e': print('Error receiving packet: Header') return 0, payload # Length: 2 bytes lengthb = ser.read(2) length = int.from_bytes(lengthb, 'big') if length == 0: print('Error receiving packet: Zero length') return 0, payload # Payload checksum = b'\x00' payload = b'' for i in range(length): currbyte = ser.read(1) payload = payload + currbyte checksum = byte_sum(checksum, currbyte) # Checksum currbyte = ser.read(1) checksum = byte_diff0xff(checksum) if checksum != currbyte: print('Error receiving packet: Invalid checksum') return 0, payload print('Received XBee API frame w/ length {}'.format(length)) return 1, payload
def debug_unicast(n, dest): # Header bytestr = b'\x7e' # Length length = 17 bytestr = bytestr + (length).to_bytes(2, 'big') # Frame type ftype = b'\x10' bytestr = bytestr + ftype # Frame ID fid = b'\x01' bytestr = bytestr + fid checksum = byte_sum(ftype, fid) # 64-bit dest addr bytestr = bytestr + dest for i in range(8): checksum = byte_sum(checksum, dest[i].to_bytes(1, 'big')) # 16-bit dest addr dest16 = b'\xff\xfe' bytestr = bytestr + dest16 for i in range(2): checksum = byte_sum(checksum, dest16[i].to_bytes(1, 'big')) # Broadcast radius brad = b'\x00' bytestr = bytestr + brad checksum = byte_sum(checksum, brad) # Options opt = b'\x00' bytestr = bytestr + opt checksum = byte_sum(checksum, opt) # Data data = b'DU' if n > 255: print('Error: number of transmissions exceeds limit') return bytestr data = data + (n).to_bytes(1, 'big') bytestr = bytestr + data checksum = byte_sum(checksum, b'D') checksum = byte_sum(checksum, b'U') checksum = byte_sum(checksum, (n).to_bytes(1, 'big')) # Checksum checksum = byte_diff0xff(checksum) bytestr = bytestr + checksum return bytestr
def gen_headtail(bytestr): # Compute checksum checksum = b'\x00' for i in range(len(bytestr)): checksum = byte_sum(checksum, (bytestr[i]).to_bytes(1, 'big')) checksum = byte_diff0xff(checksum) # Generate length length = (len(bytestr)).to_bytes(2, 'big') # Append headers and checksum bytestr = b'\x7e' + length + bytestr + checksum return bytestr