if status == DOWNLOAD_CMD: msginfo("Download mode command sent.") sys.exit(0) else: msgerr("Failed to send command mode.") sys.exit(-1) fd = open(blfile, 'rb') msginfo("Pinging target..."); ser.send(ping_command()) if ser.recv(5) != REPLY_PACKET: msgerr("Device didn't reply or in wrong mode.") fd.close() ser.close() sys.exit(-1) addr = UPLOAD_ADDR size = UPLOAD_BLOCK_SIZE buf = fd.read(UPLOAD_BLOCK_SIZE) size = len(buf) while size > 0: msginfo("Writing {0} bytes @ 0x{1:08x} ...".format(size, addr)) packet = write_command(addr, buf, size) addr += size ser.send(packet) if ser.recv(5) != REPLY_PACKET:
ser.send(packet) status = ser.recv(1) if (len(status) == 0): msgerr("Reply packet size is 0 bytes. Something is wrong.") break if (status == '\x42'): msgerr("Unknown reply code.") continue reply = ser.recv(len(packet) - 1) status = reply[-5] if (status == '\x07'): msgerr("NV item {0} is read-only.".format(item_num)) elif (status == '\x05'): msgerr("NV item {0} is inactive.".format(item_num)) else: msginfo("Wrote NV item number {0}.".format(item_num)) fd.close() ser.close() msginfo("Done!") if __name__ == "__main__": main(sys.argv[1:])