main.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/python3
  2. # -*- coding: UTF-8 -*-
  3. import serial, serial.tools.list_ports, sys, argparse
  4. def find_virtual_port():
  5. for port in serial.tools.list_ports.comports():
  6. if port.vid == 0x19d1 and port.pid == 0x0001 and port.location and "x.6" in port.location:
  7. return port.device
  8. return None
  9. # 解析命令行参数
  10. parser = argparse.ArgumentParser()
  11. parser.add_argument('-p', '--port', help='串口名称,如COM1或/dev/ttyS0')
  12. args = parser.parse_args()
  13. # 确定使用的串口
  14. port = args.port or find_virtual_port()
  15. if not port:
  16. print("错误: 未找到虚拟串口且未指定串口")
  17. sys.exit(1)
  18. print(f"使用串口: {port}")
  19. try:
  20. with serial.Serial(port, 115200, timeout=1) as ser:
  21. ser.write(b"#FOTA\n")
  22. data = ser.read(128)
  23. if data and data.startswith(b"#FOTA"):
  24. print("设备响应", data)
  25. with open("fota_uart.bin", "rb") as f:
  26. while fdata := f.read(256):
  27. print("发送升级包数据", len(fdata))
  28. ser.write(fdata)
  29. if resp := ser.read(128):
  30. print("设备响应", resp)
  31. print("发送完毕,退出")
  32. else:
  33. print("设备没响应", data)
  34. except Exception as e:
  35. print(f"错误: {e}")
  36. sys.exit(1)