forked from JackDoan/hm305_ctrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure_hm305.py
executable file
·62 lines (49 loc) · 1.95 KB
/
measure_hm305.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python3
import logging
import sys
from serial import SerialException
from hm305 import CRCError, CSVWriter
from hm305.hm305 import HM305
logging.basicConfig(format='%(asctime)s\t%(levelname)s\t%(message)s', level=logging.INFO)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--port', type=str, help='serial port', required=True)
parser.add_argument('--voltage', type=float, help='set voltage')
parser.add_argument('--current', type=float, help='set current')
parser.add_argument('--csv', type=str, help='path to csv')
parser.add_argument('--debug', action='store_true', help='enable verbose logging')
args = parser.parse_args()
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
try:
hm = HM305(args.port)
except SerialException:
logging.fatal(f"Port {args.port} could not open!")
sys.exit(1)
try:
if args.voltage is not None:
logging.info(f"Setting voltage: {args.voltage}")
hm.v = args.voltage
if args.current is not None:
logging.info(f"Setting current: {args.current}")
hm.i = args.current
except SerialException as ex:
logging.warning(f"Could not set measurements.")
csv_logger = CSVWriter(args.csv, delimiter=";")
is_running = True
while is_running:
try:
logging.info(f"voltage: {hm.v} V | current: {hm.i} A | power: {hm.w} W")
csv_logger.write(hm.v, hm.i, hm.w)
except SerialException as ex:
logging.warning(f"Could not read measurements. {ex}")
except KeyboardInterrupt:
is_running = False
logging.debug("cancel by user keyboard")
except CRCError as ex:
logging.warning(f"CRCError: {ex}")
except Exception as ex:
logging.error(f"unknown issue: {ex}")
logging.exception(ex)
logging.debug("Shutdown")