Loading salt/drone/scripts/uart_test.py +31 −16 Original line number Diff line number Diff line #!/usr/bin/python import serial from time import sleep ser = serial.Serial("/dev/ttyS0", 115200) # Open port with baud rate MTU = 32 START_BYTE1 = 0xbc START_BYTE2 = 0xcf START = [START_BYTE1, START_BYTE2] received_data = [] ser = serial.Serial("/dev/ttyS0", 115200) while True: # received_data = ser.read() # read serial port # sleep(0.03) # data_left = ser.inWaiting() # check for remaining byte # received_data += ser.read(data_left) # print(list(received_data)) # print received data # ser.write(received_data) # transmit data serially data_left = 1 received_data = "" while data_left: received_data += ser.read() sleep(0.003) data_left = ser.inWaiting() print(list(received_data)) received_data = map(ord, list(ser.read_until(chr(START_BYTE1)))) received_data += map(ord, list(ser.read(1))) if received_data[1] != START_BYTE2: print("ERROR START") continue received_data += map(ord, list(ser.read(2))) if received_data[3] > MTU: print("ERROR LENGTH") continue received_data += map(ord, list(ser.read(received_data[3]+2))) cksum0 = 0 cksum1 = 0 for c in received_data[2:-2]: cksum0 = (cksum0 + c) & 0xff cksum1 = (cksum1 + cksum0) & 0xff if cksum0 != received_data[-2] or cksum1 != received_data[-1]: print("ERROR CKSUM") print(" ".join("{:#04x}".format(n) for n in received_data)) Loading
salt/drone/scripts/uart_test.py +31 −16 Original line number Diff line number Diff line #!/usr/bin/python import serial from time import sleep ser = serial.Serial("/dev/ttyS0", 115200) # Open port with baud rate MTU = 32 START_BYTE1 = 0xbc START_BYTE2 = 0xcf START = [START_BYTE1, START_BYTE2] received_data = [] ser = serial.Serial("/dev/ttyS0", 115200) while True: # received_data = ser.read() # read serial port # sleep(0.03) # data_left = ser.inWaiting() # check for remaining byte # received_data += ser.read(data_left) # print(list(received_data)) # print received data # ser.write(received_data) # transmit data serially data_left = 1 received_data = "" while data_left: received_data += ser.read() sleep(0.003) data_left = ser.inWaiting() print(list(received_data)) received_data = map(ord, list(ser.read_until(chr(START_BYTE1)))) received_data += map(ord, list(ser.read(1))) if received_data[1] != START_BYTE2: print("ERROR START") continue received_data += map(ord, list(ser.read(2))) if received_data[3] > MTU: print("ERROR LENGTH") continue received_data += map(ord, list(ser.read(received_data[3]+2))) cksum0 = 0 cksum1 = 0 for c in received_data[2:-2]: cksum0 = (cksum0 + c) & 0xff cksum1 = (cksum1 + cksum0) & 0xff if cksum0 != received_data[-2] or cksum1 != received_data[-1]: print("ERROR CKSUM") print(" ".join("{:#04x}".format(n) for n in received_data))