import sys from serial import Serial if len(sys.argv) < 3: print ('upload.py ') print ('Example:') print ('python3 ./upload.py /dev/ttyACM1 ./EncryptedFirmware.bin') exit() serialPort = sys.argv[1] srcFile = sys.argv[2] ser = Serial(serialPort, 115200) print ('Starting...') # Erasig Chip ser.write(b'e') rx = ser.read(1) if rx == b'?': # Confirm Erasig Chip ser.write(b'y') print ('Erasig Chip') rx = ser.read(1) if rx == b'$': print ('Chip Erased') else: print ('ERROR 1') exit() #ser.write(b'u') # Ordinary uploading ser.write(b'c') # Encrypted uploading rx = ser.read(1) if rx == b'>': print ('Uploading') counter = 0 with open(srcFile, "rb") as f: while True: chunk = f.read(16) if not chunk: break ser.write(chunk) rx = ser.read(1) counter += 1 print(f"Packet: {counter}") if rx != b'\x7f': print('ERROR 2') exit() rx = ser.read(1) if rx == b'$': # Run App ser.write(b'r') print('Done') else: print('ERROR 3') ser.close()