config.py 766 B

1234567891011121314151617181920212223242526272829
  1. import sys
  2. import time
  3. import serial
  4. from serial.tools import list_ports
  5. if len(sys.argv) != 5:
  6. print("usage: config.py <SSID> <Password> <Host> <Port>")
  7. exit(1)
  8. _, *config = sys.argv
  9. for p in list_ports.comports():
  10. try:
  11. ser = serial.Serial(p.device, 38400)
  12. for i in range(5):
  13. time.sleep(3)
  14. data = str(ser.read(ser.inWaiting()), "ASCII")
  15. print(data)
  16. ser.write(bytes("CONFIG:{},{},{},{}\r\n".format(*config) + 'A'*51, "ASCII"))
  17. for k in range(20):
  18. time.sleep(1)
  19. data = str(ser.read(ser.inWaiting()), "ASCII")
  20. print(data)
  21. if data.find(config[0]) > -1:
  22. print('success')
  23. exit(0)
  24. ser.write(bytes('A'*5100, "ASCII"))
  25. except serial.SerialException as e:
  26. print(e)