display.py 858 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env python3
  2. import argparse
  3. import logging
  4. import struct
  5. import serial
  6. _LOGGER = logging.getLogger(__name__)
  7. def _main():
  8. logging.basicConfig(
  9. level=logging.DEBUG,
  10. format="%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s",
  11. datefmt="%Y-%m-%dT%H:%M:%S%z",
  12. )
  13. argparser = argparse.ArgumentParser()
  14. argparser.add_argument("--serial-port", type=str, nargs="?", default="/dev/ttyUSB0")
  15. args = argparser.parse_args()
  16. _LOGGER.debug("args=%r", args)
  17. with serial.Serial(args.serial_port, 115200, timeout=2) as serial_port:
  18. _LOGGER.debug("serial_port.name=%s", serial_port.name)
  19. while True:
  20. reading, = struct.unpack(">H", serial_port.read(2))
  21. voltage = reading * 5 / 1024
  22. print(f"{voltage:.02f}V")
  23. if __name__ == "__main__":
  24. _main()