-
Notifications
You must be signed in to change notification settings - Fork 9
/
read_modbus_device.py
356 lines (320 loc) · 20.6 KB
/
read_modbus_device.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env python3
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
from datetime import datetime, timedelta
from os import path
import sys
import os
import serial
import time
import yaml
import logging
import modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
from modbus_tk import modbus_tcp
import struct
#PORT = 1
PORT = '/dev/ttyUSB0'
#PORT = '/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0'
# Change working dir to the same dir as this script
os.chdir(sys.path[0])
class DataCollector:
def __init__(self, influx_yaml, device_yaml):
self.influx_yaml = influx_yaml
self.influx_map = None
self.influx_map_last_change = -1
self.influx_inteval_save = dict()
log.info('InfluxDB:')
for influx_config in sorted(self.get_influxdb(), key=lambda x:sorted(x.keys())):
log.info('\t {} <--> {}'.format(influx_config['url'], influx_config['name']))
self.device_yaml = device_yaml
self.max_iterations = None # run indefinitely by default
self.device_map = None
self.device_map_last_change = -1
log.info('Devices:')
for device in sorted(self.get_devices(), key=lambda x:sorted(x.keys())):
log.info('\t {} <--> {}'.format( device['id'], device['name']))
def get_devices(self):
assert path.exists(self.device_yaml), 'Device map not found: %s' % self.device_yaml
if path.getmtime(self.device_yaml) != self.device_map_last_change:
try:
log.info('Reloading device map as file changed')
new_map = yaml.load(open(self.device_yaml), Loader=yaml.FullLoader)
self.device_map = new_map['devices']
self.device_map_last_change = path.getmtime(self.device_yaml)
except Exception as e:
log.warning('Failed to re-load device map, going on with the old one.')
log.warning(e)
return self.device_map
def get_influxdb(self):
assert path.exists(self.influx_yaml), 'InfluxDB map not found: %s' % self.influx_yaml
if path.getmtime(self.influx_yaml) != self.influx_map_last_change:
try:
log.info('Reloading influxDB map as file changed')
new_map = yaml.load(open(self.influx_yaml), Loader=yaml.FullLoader)
self.influx_map = new_map['influxdb']
self.influx_map_last_change = path.getmtime(self.influx_yaml)
list = 0
for influx_config in sorted(self.get_influxdb(), key=lambda x:sorted(x.keys())):
list = list + 1
self.influx_inteval_save[list] = influx_config['interval']
except Exception as e:
log.warning('Failed to re-load influxDB map, going on with the old one.')
log.warning(e)
return self.influx_map
def collect_and_store(self):
#instrument.debug = True
devices = self.get_devices()
influxdb = self.get_influxdb()
t_utc = datetime.utcnow()
t_str = t_utc.isoformat() + 'Z'
datas = dict()
device_id_name = dict() # mapping id to name
device_slave_id = dict() # mapping list to id
list = 0
for device in devices:
list = list + 1
device_id_name[list] = device['name']
device_slave_id[list] = device['id']
try:
if device['conexion'] == 'R':
masterRTU = modbus_rtu.RtuMaster(
serial.Serial(port=PORT, baudrate=device['baudrate'], bytesize=device['bytesize'], parity=device['parity'], stopbits=device['stopbits'], xonxoff=0)
)
masterRTU.set_timeout(device['timeout'])
masterRTU.set_verbose(True)
log.debug('Reading device %s.' % (device['name']))
start_time = time.time()
parameters = yaml.load(open(device['type']), Loader=yaml.FullLoader)
datas[list] = dict()
for parameter in parameters:
# If random readout errors occour, e.g. CRC check fail, test to uncomment the following row
#time.sleep(0.5) # Sleep for 500 ms between each parameter read to avoid errors
retries = 10
while retries > 0:
try:
retries -= 1
if device['function'] == 3:
if parameters[parameter][2] == 1:
resultado = masterRTU.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>f')
elif parameters[parameter][2] == 2:
resultado = masterRTU.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>l')
elif parameters[parameter][2] == 3:
resultado = masterRTU.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1])
elif parameters[parameter][2] == 4:
resultadoTemp = masterRTU.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1])
resultado = [0,0]
resultado[0] = (resultadoTemp[1]<<16)|resultadoTemp[0]
elif parameters[parameter][2] == 5:
resultado = masterRTU.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>I')
elif parameters[parameter][2] == 6:
resultado = masterRTU.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>L')
elif parameters[parameter][2] == 7:
resultadoTemp = masterRTU.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1])
resultado = [0,0]
resultado[0] = struct.unpack('f', struct.pack('I', (resultadoTemp[1]<<16)|resultadoTemp[0]))[0]
elif device['function'] == 4:
if parameters[parameter][2] == 1:
resultado = masterRTU.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>f')
elif parameters[parameter][2] == 2:
resultado = masterRTU.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>l')
elif parameters[parameter][2] == 3:
resultado = masterRTU.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1])
elif parameters[parameter][2] == 4:
resultadoTemp = masterRTU.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1])
resultado = [0,0]
resultado[0] = (resultadoTemp[1]<<16)|resultadoTemp[0]
elif parameters[parameter][2] == 5:
resultado = masterRTU.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>I')
elif parameters[parameter][2] == 6:
resultado = masterRTU.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>L')
elif parameters[parameter][2] == 7:
resultadoTemp = masterRTU.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1])
resultado = [0,0]
resultado[0] = struct.unpack('f', struct.pack('I', (resultadoTemp[1]<<16)|resultadoTemp[0]))[0]
datas[list][parameter] = resultado[0]
retries = 0
pass
except ValueError as ve:
log.warning('Value Error while reading register {} from device {}. Retries left {}.'
.format(parameters[parameter], device['id'], retries))
log.error(ve)
if retries == 0:
raise RuntimeError
except TypeError as te:
log.warning('Type Error while reading register {} from device {}. Retries left {}.'
.format(parameters[parameter], device['id'], retries))
log.error(te)
if retries == 0:
raise RuntimeError
except IOError as ie:
log.warning('IO Error while reading register {} from device {}. Retries left {}.'
.format(parameters[parameter], device['id'], retries))
log.error(ie)
if retries == 0:
raise RuntimeError
except:
log.error("Unexpected error:", sys.exc_info()[0])
raise
datas[list]['ReadTime'] = time.time() - start_time
elif device['conexion'] == 'T':
masterTCP = modbus_tcp.TcpMaster(host=device['direction'],port=device['port'])
masterTCP.set_timeout(device['timeout'])
log.debug('Reading device %s.' % (device['name']))
start_time = time.time()
parameters = yaml.load(open(device['type']), Loader=yaml.FullLoader)
datas[list] = dict()
for parameter in parameters:
# If random readout errors occour, e.g. CRC check fail, test to uncomment the following row
#time.sleep(0.01) # Sleep for 10 ms between each parameter read to avoid errors
retries = 10
while retries > 0:
try:
retries -= 1
if device['function'] == 3:
if parameters[parameter][2] == 1:
resultado = masterTCP.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>f')
elif parameters[parameter][2] == 2:
resultado = masterTCP.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>l')
elif parameters[parameter][2] == 3:
resultado = masterTCP.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1])
elif parameters[parameter][2] == 4:
resultadoTemp = masterTCP.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1])
resultado = [0,0]
resultado[0] = (resultadoTemp[1]<<16)|resultadoTemp[0]
elif parameters[parameter][2] == 5:
resultado = masterTCP.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>I')
elif parameters[parameter][2] == 6:
resultado = masterTCP.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>L')
elif parameters[parameter][2] == 7:
resultadoTemp = masterTCP.execute(device['id'], cst.READ_HOLDING_REGISTERS, parameters[parameter][0], parameters[parameter][1])
resultado = [0,0]
resultado[0] = struct.unpack('f', struct.pack('I', (resultadoTemp[1]<<16)|resultadoTemp[0]))[0]
elif device['function'] == 4:
if parameters[parameter][2] == 1:
resultado = masterTCP.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>f')
elif parameters[parameter][2] == 2:
resultado = masterTCP.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>l')
elif parameters[parameter][2] == 3:
resultado = masterTCP.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1])
elif parameters[parameter][2] == 4:
resultadoTemp = masterTCP.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1])
resultado = [0,0]
resultado[0] = (resultadoTemp[1]<<16)|resultadoTemp[0]
elif parameters[parameter][2] == 5:
resultado = masterTCP.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>I')
elif parameters[parameter][2] == 6:
resultado = masterTCP.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1], data_format='>L')
elif parameters[parameter][2] == 7:
resultadoTemp = masterTCP.execute(device['id'], cst.READ_INPUT_REGISTERS, parameters[parameter][0], parameters[parameter][1])
resultado = [0,0]
resultado[0] = struct.unpack('f', struct.pack('I', (resultadoTemp[1]<<16)|resultadoTemp[0]))[0]
datas[list][parameter] = resultado[0]
retries = 0
pass
except ValueError as ve:
log.warning('Value Error while reading register {} from device {}. Retries left {}.'
.format(parameters[parameter], device['id'], retries))
log.error(ve)
if retries == 0:
raise RuntimeError
except TypeError as te:
log.warning('Type Error while reading register {} from device {}. Retries left {}.'
.format(parameters[parameter], device['id'], retries))
log.error(te)
if retries == 0:
raise RuntimeError
except IOError as ie:
log.warning('IO Error while reading register {} from device {}. Retries left {}.'
.format(parameters[parameter], device['id'], retries))
log.error(ie)
if retries == 0:
raise RuntimeError
except:
log.error("Unexpected error:", sys.exc_info()[0])
raise
datas[list]['ReadTime'] = time.time() - start_time
except modbus_tk.modbus.ModbusError as exc:
log.error("%s- Code=%d", exc, exc.get_exception_code())
json_body = [
{
'measurement': 'ModbusLog',
'tags': {
'id': device_slave_id[device_id],
'device': device_id_name[device_id],
},
'time': t_str,
'fields': datas[device_id]
}
for device_id in datas
]
if len(json_body) > 0:
# log.debug(json_body)
list = 0
for influx_config in influxdb:
list = list + 1
if self.influx_inteval_save[list] > 0:
if self.influx_inteval_save[list] <= 1:
self.influx_inteval_save[list] = influx_config['interval']
DBclient = InfluxDBClient(url=influx_config['url'], token=influx_config['token'], org=influx_config['org'])
write_api = DBclient.write_api(write_options=SYNCHRONOUS)
try:
write_api.write(bucket=influx_config['dbname'],org=influx_config['org'],record=json_body)
log.info(t_str + ' Data written for %d meters in {}.' .format(influx_config['name']) % len(json_body) )
except Exception as e:
log.error('Data not written! in {}' .format(influx_config['name']))
log.error(e)
raise
else:
self.influx_inteval_save[list] = self.influx_inteval_save[list] - 1
else:
log.warning(t_str, 'No data sent.')
def repeat(interval_sec, max_iter, func, *args, **kwargs):
from itertools import count
import time
starttime = time.time()
for i in count():
if interval_sec > 0:
time.sleep(interval_sec - ((time.time() - starttime) % interval_sec))
if i % 1000 == 0:
log.info('Collected %d readouts' % i)
try:
func(*args, **kwargs)
except Exception as ex:
log.error(ex)
if max_iter and i >= max_iter:
return
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--interval', default=60,
help='Device readout interval (seconds), default 60')
parser.add_argument('--devices', default='devices.yml',
help='YAML file containing Device ID, name, type etc. Default "devices.yml"')
parser.add_argument('--influxdb', default='influx_config.yml',
help='YAML file containing Influx Host, port, user etc. Default "influx_config.yml"')
parser.add_argument('--log', default='CRITICAL',
help='Log levels, DEBUG, INFO, WARNING, ERROR or CRITICAL')
parser.add_argument('--logfile', default='',
help='Specify log file, if not specified the log is streamed to console')
args = parser.parse_args()
interval = int(args.interval)
loglevel = args.log.upper()
logfile = args.logfile
# Setup logging
log = logging.getLogger('modbus-logger')
log.setLevel(getattr(logging, loglevel))
if logfile:
loghandle = logging.FileHandler(logfile, 'w')
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
loghandle.setFormatter(formatter)
else:
loghandle = logging.StreamHandler()
log.addHandler(loghandle)
log.info('Started app')
collector = DataCollector(influx_yaml=args.influxdb,
device_yaml=args.devices)
repeat(interval,
max_iter=collector.max_iterations,
func=lambda: collector.collect_and_store())