display.py~ 1.0 KB

123456789101112131415161718192021222324252627282930313233
  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. # _LOGGER.info("project_repo_url=%s", serial_port.readline().decode().strip())
  20. # _LOGGER.info("project_version=%s", serial_port.readline().decode().strip())
  21. while True:
  22. reading, = struct.unpack(">H", serial_port.read(2))
  23. voltage = reading * 5 / 1024
  24. print(f"{voltage:.02f}V")
  25. if __name__ == "__main__":
  26. _main()