-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetraSwitch
executable file
·117 lines (99 loc) · 4.53 KB
/
MetraSwitch
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python
# switch the meter on, set measurement rate and enable send mode
import sys
import time
import argparse
from openmetra import OpenMetra
# Create the parser
ap = argparse.ArgumentParser(allow_abbrev=False,
description=
'''Gossen METRAHit 29S: switch off or select function or select data rate or set RTC time and date.
Without specifying an action the meter is just switched on.''')
group = ap.add_mutually_exclusive_group()
# Add the arguments
ap.add_argument('-d',
'--device',
action = 'store',
default = '/dev/ttyUSB0',
help = 'device path of serial interface, default is "/dev/ttyUSB0"' )
group.add_argument('--off',
action = 'store_true',
help = 'switch the device off' )
group.add_argument('--function',
action = 'store',
type = int,
dest = 'function',
default = None,
help = '''select measurement function: 1:V_DC, 2:V_ACDC, 3:V_AC, 4:mA_DC, 5:mA_ACDC,
6:A_DC, 7:A_ACDC, 8:Ohm, 9:F, 10:dB, 11:Hz_Uacdc, 12:Hz_Uac, 17:Ohm buzzer,
18:Temp, 22:pulseW, 23TRMS_mains, 24:Counter, 25:Events_Uacdc, 26:Events_Uac''' )
group.add_argument('--rate',
action = 'store',
type = int,
dest = 'rate',
default = None,
help = '''select index of measurement rate: 0:50ms, 1:0.1s, 2:0.2s, 3:0.5s, 4:1s, 5:2s, 6:5s,
7:10s, 8:20s, 9:30s, 10:1min, 11:2min, 12:5min, 13:10min''' )
group.add_argument('--set_rtc',
action = 'store_true',
dest = 'set_rtc',
help = '''set meter RTC time and date from local time''' )
ap.add_argument('-v',
'--version',
action = 'store_true',
dest = 'version',
help = 'show openmetra version')
ap.add_argument('-V',
action = 'count',
dest = 'verbose',
default = 0,
help = 'increase verbosity')
# parse my argument
options = ap.parse_args()
if options.version:
print( f'OpenMetra version {OpenMetra.VERSION}')
sys.exit()
with OpenMetra( options.device ) as mh: # open connection to '/dev/ttyUSB0', the serial path can be an optional parameter
if mh is None: # could not conect
print( 'connect error', file=sys.stderr)
sys.exit()
mh.set_verbose( options.verbose )
try:
if options.off:
mh.set_mode( mh.MODE_NORMAL ) # switch to normal mode
mh.send_command( mh.CMD_FW_STATUS ) # get status
mh.decode_rsp( mh.get_cmd_response(), sys.stderr )
mh.set_mode( mh.MODE_OFF ) # switch off
elif options.function is not None:
mh.set_function( options.function ) # set measurement function
mh.decode_rsp( mh.get_cmd_response(), sys.stderr )
elif options.rate is not None:
mh.send_command( mh.CMD_FW_STATUS )
mh.decode_rsp( mh.get_cmd_response(), sys.stderr )
# rate 0: 0.05s, 1: 0.1s, 2:0.2s, 3:0.5s - FAST MODE
# 4:1s, 5: 2s, 6:5s, 7:10s, 8:20s, 9:30s, 10:60s, 11:2min, 12:5min, 13:10min
rate = options.rate
rate = mh.set_rate( rate )
print( 'Measurement rate:', rate, 's' )
if options.verbose > 1:
mh.decode_rsp( mh.get_cmd_response(), sys.stderr ) #
mh.set_mode( mh.MODE_SEND ) # switch to send mode
elif options.set_rtc:
mh.send_command( mh.CMD_FW_STATUS )
mh.decode_rsp( mh.get_cmd_response(), sys.stderr )
tm = time.localtime()
year = tm.tm_year % 100
month = tm.tm_mon - 1 # jan -> 00, .., dec -> 11
day = tm.tm_mday - 1 # 00 .. 27 or 29 or 30
hour = tm.tm_hour
minute = tm.tm_min
second = tm.tm_sec
mh.send_command( 4, 0, 0, 0, second%10, second//10, minute%10, minute//10, hour%10, hour//10 ) # set time
mh.decode_rsp( mh.get_cmd_response(), sys.stderr ) #
mh.send_command( 4, 1, 0, 0, day%10, day//10, month%10, month//10, year%10, year//10 ) # set date
mh.decode_rsp( mh.get_cmd_response(), sys.stderr ) #
else:
mh.send_command( mh.CMD_FW_STATUS )
mh.decode_rsp( mh.get_cmd_response(), sys.stderr )
except KeyboardInterrupt: # ^C pressed, stop measurement
print( '' )