serial_discharge.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python3
  2. """
  3. XY-FZ35
  4. https://www.aliexpress.com/item/FZ35-Adjust-Constant-Current-Electronic-Load-1-5v-25v-5A-35W-Battery-Tester-Discharge-Capacity-meter/32953589108.html
  5. https://www.youtube.com/watch?v=RboBfJe4XXE
  6. https://pyserial.readthedocs.io/en/latest/pyserial_api.html
  7. https://learn.adafruit.com/li-ion-and-lipoly-batteries/voltages
  8. Serial port Control (single-chip TTL level communication)
  9. Baud Rate: 9600 bps
  10. Data bits: 8
  11. Stop bits: 1
  12. Check bit: None
  13. Flow control: None
  14. Serial Port Commands
  15. Command | Info
  16. ---------------------
  17. start | Start upload (approx 1 second between updates) (fixed rate, dont know how to change)
  18. stop | Stop upload
  19. on | Turn on Load features
  20. off | Turn off Load features
  21. read | Read product parameter settings
  22. x.xxA | Set load current
  23. OVP:xx.x | Over Voltage Protection (Set maximum load voltage)
  24. OCP:x.xx | Over Current Protection (Set maximum discharging current)
  25. OPP:xx.xx | Over Power Protection (Set maximum discharging watts)
  26. LVP:xx.x | Low Voltage Protection (Set minimum voltage)
  27. OAH:x.xxx | Over Ampere Hour Protection (Set maximum capacity) (Set to 0.000 to disable)
  28. OHP:xx:xx | Set maximum discharge time (Set to 00:00 to disable)
  29. --------------------------------------------
  30. default output for 'read': b'OVP:25.2, OCP:5.10, OPP:35.50, LVP:01.5,OAH:0.000,OHP:00:00\r\n'
  31. output for 'start' (continious):
  32. """
  33. import serial
  34. import time
  35. from sys import getsizeof
  36. with serial.Serial() as ser:
  37. ser.port = '/dev/ttyUSB0'
  38. ser.baudrate = 9600
  39. ser.bytesize = serial.EIGHTBITS
  40. ser.parity = serial.PARITY_NONE
  41. ser.stopbits = serial.STOPBITS_ONE
  42. ser.timeout = None
  43. ser.xonxoff = False
  44. ser.rtscts = False
  45. ser.dsrdtr = False
  46. ser.write_timeout = None
  47. ser.inter_byte_timeout = None
  48. ser.exclusive = None
  49. ser.open()
  50. ser.write(b'stop')
  51. print("Stopping data upload: {}".format(ser.readline().decode()), end="")
  52. ser.write(b'off')
  53. print("Turning off the load: {}".format(ser.readline().decode()), end="")
  54. ser.write(b'0.50A')
  55. print("Setting A: {}".format(ser.readline().decode()), end="")
  56. ser.write(b'OVP:04.3')
  57. print("Setting OVP: {}".format(ser.readline().decode()), end="")
  58. ser.write(b'OCP:0.60')
  59. print("Setting OCP: {}".format(ser.readline().decode()), end="")
  60. ser.write(b'OPP:05.00')
  61. print("Setting OPP: {}".format(ser.readline().decode()), end="")
  62. ser.write(b'LVP:03.2')
  63. print("Setting LVP: {}".format(ser.readline().decode()), end="")
  64. ser.write(b'OAH:0.000')
  65. print("Setting OAH: {}".format(ser.readline().decode()), end="")
  66. ser.write(b'OHP:00:00')
  67. print("Setting OHP: {}".format(ser.readline().decode()), end="")
  68. ser.write(b'read')
  69. print("Complete Setting Readout: {}".format(ser.readline().decode()), end="")
  70. ser.write(b'start')
  71. print("Starting data upload: {}".format(ser.readline().decode()), end="")
  72. ser.write(b'on')
  73. print("Turning ON the load: {}".format(ser.readline().decode()), end="")
  74. filename = "discharge_" + time.strftime("%Y%m%d-%H%M%S") + ".log"
  75. with open(filename, 'wb') as f1:
  76. spinner = "|"
  77. while True:
  78. time.sleep(1)
  79. line = ser.read(ser.inWaiting())
  80. f1.write(line)
  81. f1.flush()
  82. if getsizeof(line) == 61:
  83. print(" " + spinner + " " + line.decode().rstrip(), end="\r")
  84. elif line.decode().rstrip() == 'LVP':
  85. print()
  86. print()
  87. print("Discharge Complete")
  88. break
  89. if spinner == "|":
  90. spinner = "/"
  91. elif spinner == "/":
  92. spinner = "-"
  93. elif spinner == "-":
  94. spinner = "\\"
  95. elif spinner == "\\":
  96. spinner = "|"
  97. ser.close()