12345678910111213141516171819202122232425262728293031 |
- #!/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)
- while True:
- reading, = struct.unpack(">H", serial_port.read(2))
- voltage = reading * 5 / 1024
- print(f"{voltage:.02f}V")
- if __name__ == "__main__":
- _main()
|