-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metra
executable file
·210 lines (180 loc) · 7.42 KB
/
Metra
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''
get measement from multimeter Gossen METRAHit 29S via BD232 serial interface
works with FTDI RS232 USB connection to /dev/ttyUSB0
switch on with: SEt v SEnd <-/ OFF v on <-/
or by holding DATA/CLEAR while switching on the instrument
the meter sends the measurement as 4bit digits with a rate of 9600 bit/s
the BD232 is powered by the RTS and DTR lines
'''
import sys
import time
import argparse
from openmetra import OpenMetra
# Create the parser
ap = argparse.ArgumentParser(description='Get data from Gossen METRAHit 29S')
# Add the arguments
ap.add_argument('-c',
'--csv',
action = 'store_true',
help = 'create csv (together with -t and/or -u)')
ap.add_argument('-d',
'--device',
action = 'store',
dest = 'serial_device',
default = '/dev/ttyUSB0',
help = 'device path of serial interface, default is "/dev/ttyUSB0"' )
ap.add_argument('-f',
'--format_values',
action = 'store_true',
default = False,
help = 'print formatted values (instead of as shown on meter)'),
ap.add_argument('-g',
'--german',
action = 'store_true',
help = 'use comma as decimal separator, semicolon as field separator')
ap.add_argument('-n',
'--number',
action = 'store',
dest = 'number',
type = int,
default = 0,
help = 'get NUMBER measurement values' )
ap.add_argument('-o',
'--on-off',
dest = 'on_off',
action = 'store_true',
help = 'switch meter on, select send mode and rate and switch off after measurement')
ap.add_argument('-O',
'--overload',
dest = 'print_overload',
action = 'store_true',
help = 'print OL values as "None" instead of skipping')
ap.add_argument('-r',
'--rate',
action = 'store',
type = int,
dest = 'rate',
default = 4,
help = '''select index for 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, default: 4 (1s)''' )
ap.add_argument('-s',
'--seconds',
action = 'store',
dest = 'seconds',
type = float,
default = 0,
help = 'measure for a duration of SECONDS' )
ap.add_argument('-t',
'--timestamp',
dest = 'print_timestamp',
action = 'store_true',
help = 'print timestamp for each value')
ap.add_argument('-T',
'--timeout',
action = 'store',
type = int,
default = 10,
help = 'set timeout for serial port' )
ap.add_argument('-u',
'--unit',
dest = 'print_unit',
action = 'store_true',
help = 'print unit of measured value')
ap.add_argument('-U',
'--unit_long',
dest = 'print_unit_long',
action = 'store_true',
help = 'print unit of measured value with explanation, e.g. AC, DC, etc')
ap.add_argument('-v',
'--version',
dest = 'version',
action = 'store_true',
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()
# open connection to a Gossen Metrahit device, without argument it uses '/dev/ttyUSB0'
with OpenMetra( options.serial_device, timeout=options.timeout ) as mh:
if mh is None:
print( 'connect error', file=sys.stderr)
sys.exit()
mh.set_verbose( options.verbose )
if options.csv:
if options.german:
field_sep = ';'
else:
field_sep = ','
else:
field_sep = ' '
measurement = 0
try:
if options.on_off:
mh.wakeup()
if options.verbose:
mh.send_command( mh.CMD_FW_STATUS )
mh.decode_rsp( mh.get_cmd_response(), sys.stderr ) #
mh.set_rate( options.rate )
mh.set_mode( mh.MODE_SEND ) # switch to send mode
start_time = time.time()
while True: # measurement loop
if options.number and measurement >= options.number:
break
value = mh.get_measurement( options.format_values )
if value is None and not options.print_overload:
continue
unit = mh.get_unit()
if (options.print_unit or options.print_unit_long) and unit == '': # skip output until unit is available
continue
measure_time = time.time() - start_time
measurement += 1
if options.seconds and ( measure_time > options.seconds ): # time over
break
if options.print_timestamp: # seconds since start with 3 decimal digits
timestamp = str( round( measure_time, 3 ) )
if options.german:
timestamp = timestamp.replace( '.', ',' )
print( timestamp, end = field_sep )
if options.print_unit or options.print_unit_long:
print ( 's', end = field_sep )
if options.german:
value = value.replace( '.', ',' )
print( value, end = '' )
if options.print_unit_long:
print ( field_sep + mh.get_unit_long(), end = '' )
elif options.print_unit:
print ( field_sep + mh.get_unit(), end = '' )
if options.verbose > 1 and mh._ctmv is not None and mh.get_rs_string() is not None:
print ( field_sep + hex(mh._ctmv) + field_sep + mh.get_rs_string()
+ field_sep + mh.get_special_string(), end = '' )
if unit == 'W': # special case power -> followed by voltage and current
for t in ['v', 'c']: # display also voltage and current on the same line
sys.stdout.flush()
value = mh.get_measurement( options.format_values )
if options.german:
value = value.replace( '.', ',' )
print ( field_sep + value, end = '' )
if options.print_unit_long:
print ( field_sep + mh.get_unit_long(), end = '' )
elif options.print_unit:
print ( field_sep + mh.get_unit(), end = '' )
elif options.verbose > 1:
print ( field_sep + hex(mh._ctmv) + field_sep + mh.get_rs_string()
+ field_sep + mh.get_special_string(), end = '' )
print()
sys.stdout.flush() # update redirectet output
except KeyboardInterrupt:
print()
if options.on_off:
mh.wakeup()
mh.set_mode( mh.MODE_NORMAL ) # switch to normal mode
mh.set_mode( mh.MODE_OFF ) # switch to normal mode
sys.stdout.close() # make 'tee' happy