forked from nmakel/sdm_modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
executable file
·52 lines (42 loc) · 1.76 KB
/
example.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
#!/usr/bin/env python3
import argparse
import json
import sdm_modbus
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument("host", type=str, help="ModbusTCP address")
argparser.add_argument("port", type=int, help="ModbusTCP port")
argparser.add_argument("--timeout", type=int, default=1, help="Connection timeout")
argparser.add_argument("--unit", type=int, default=1, help="Modbus unit")
argparser.add_argument("--json", action="store_true", default=False, help="Output as JSON")
args = argparser.parse_args()
meter = sdm_modbus.SDM120(
host=args.host,
port=args.port,
timeout=args.timeout,
unit=args.unit
)
if args.json:
print(json.dumps(meter.read_all(), indent=4))
else:
print(f"{meter}:")
print("\nInput Registers:")
for k, v in meter.read_all(sdm_modbus.registerType.INPUT).items():
address, length, rtype, dtype, vtype, label, fmt, batch = meter.registers[k]
if type(fmt) is list or type(fmt) is dict:
print(f"\t{label}: {fmt[str(v)]}")
elif vtype is float:
print(f"\t{label}: {v:.2f}{fmt}")
else:
print(f"\t{label}: {v}{fmt}")
print("\nHolding Registers:")
for k, v in meter.read_all(sdm_modbus.registerType.HOLDING).items():
address, length, rtype, dtype, vtype, label, fmt, batch = meter.registers[k]
if type(fmt) is list:
print(f"\t{label}: {fmt[v]}")
elif type(fmt) is dict:
print(f"\t{label}: {fmt[str(v)]}")
elif vtype is float:
print(f"\t{label}: {v:.2f}{fmt}")
else:
print(f"\t{label}: {v}{fmt}")