123456789101112131415161718192021222324252627282930313233 |
- #!/usr/bin/env python3
- import argparse
- import logging
- import struct
- import serial
- _LOGGER = logging.getLogger(__name__)
- def _main():
- logging.basicConfig(
- level=logging.DEBUG,
- format="%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s",
- datefmt="%Y-%m-%dT%H:%M:%S%z",
- )
- argparser = argparse.ArgumentParser()
- argparser.add_argument("--serial-port", type=str, nargs="?", default="/dev/ttyUSB0")
- args = argparser.parse_args()
- _LOGGER.debug("args=%r", args)
- with serial.Serial(args.serial_port, 115200, timeout=2) as serial_port:
- _LOGGER.debug("serial_port.name=%s", serial_port.name)
- # _LOGGER.info("project_repo_url=%s", serial_port.readline().decode().strip())
- # _LOGGER.info("project_version=%s", serial_port.readline().decode().strip())
- while True:
- reading, = struct.unpack(">H", serial_port.read(2))
- voltage = reading * 5 / 1024
- print(f"{voltage:.02f}V")
- if __name__ == "__main__":
- _main()
|