Forked from
Distributed Autonomous Networked Control Lab / MicroCART
1869 commits behind, 270 commits ahead of the upstream repository.
uart_comm_send.py 833 B
#!/usr/local/bin/python3.6
import sys
import time
import serial
def create_msg(main_type, subtype, msg_id, data):
msg = bytes()
msg += b'\xBE'
msg += main_type.to_bytes(1, 'little')
msg += subtype.to_bytes(1, 'little')
msg += msg_id.to_bytes(2, 'little')
msg += len(data).to_bytes(2, 'little')
msg += data
checksum = 0
for b in msg:
checksum ^= b
msg += checksum.to_bytes(1, 'little')
return msg
def create_test_packet(size=8):
data = bytes((i % 256 for i in range(size)))
return create_msg(0, 2, 0, data)
if __name__ == '__main__':
with serial.Serial('/dev/ttyUSB0', 921600, timeout=5) as ser:
for i in range(0, 1):
time.sleep(0.5)
ser.reset_input_buffer()
ser.write(create_test_packet(8))
ser.flush()