예제 #1
0
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
예제 #2
0
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
예제 #3
0
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
예제 #4
0
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
예제 #5
0
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
예제 #6
0
def rxpacket_buffered(ser):
    success = 0

    # Header(0x7e) + Length(2 bytes)
    buffer = ser.read(3)

    # Timeout check
    if buffer == b'':
        print('Serial read timeout - pd.rxpacket_buffered()')
        return success, buffer

    # Get entire packet
    lengthb = buffer[1:3]
    length = int.from_bytes(lengthb, 'big')
    if length == 0:
        print('Error receiving packet: Zero length')
        return success, buffer
    buffer_new = ser.read(length + 1)
    if buffer_new == b'':
        print('Serial read timeout - pd.rxpacket_buffered()')
        return success, buffer
    buffer = buffer + buffer_new
    payload = buffer[3:-1]

    # Compute payload checksum
    checksum = 0
    for i in range(length):
        checksum = checksum + buffer[i + 3]
        # overflow check
        if checksum > 255:
            checksum = checksum - 256
    checksumb = (checksum).to_bytes(1, 'big')
    checksumb = byte_diff0xff(checksumb)
    if checksumb != (buffer[-1]).to_bytes(1, 'big'):
        print('Error receiving packet: Invalid checksum')
        return success, payload

    success = 1
    print('Received XBee API frame w/ length {}'.format(length))
    return success, payload