-
Notifications
You must be signed in to change notification settings - Fork 7
/
request.py
267 lines (243 loc) · 9.74 KB
/
request.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import time
import os
import minimalmodbus
import serial
try:
import pyModbusTCP.client
except:
pass
"""#######Request object for doing modbus coms##
"""
IP = "localhost"
PORT = 505
minimalmodbus.BAUDRATE = 19200
minimalmodbus.PARITY = serial.PARITY_NONE
minimalmodbus.BYTESIZE = 8
minimalmodbus.STOPBITS = 1
#############################################
class Request:
def __init__(self):
self.client = None
self.connect_errors = 0
self.checksum_errors = 0
self.multi_errors = 0
self.write_errors = 0
self.counter = 0
self.error_time = time.time()
self.wait_time = 0.01
self.prev_rate = 0
self.rate = 0
self.iter = 0
self.bus = 0
self.fclient = ""
self.response = ""
self.mode = "RTU"
self.unit = ""
self.reset = 0
self.latest_request_address = 0
self.latest_request_mode = "Single"
self.latest_request_decimals = 0
self.latest_request_count = 0
self.connection_timeout = 0
def setup(self, unit, mode):
self.unit = unit
self.mode = mode
if self.mode == "TCP":
print("Using TCP backend")
# TCP auto connect on first modbus request
try:
config = open("ipconfig", "r").readline()
print("Reading ip configuration file for IAM access", config[:-1])
config = eval(config)
self.client = pyModbusTCP.client.ModbusClient(host=config["ip"], port=config["port"], auto_open=True)
except:
print(
"Fallback localhost:505 server there may be a problem with formating the ipconfig file or it may not exist") # noPEP8
self.client = pyModbusTCP.client.ModbusClient(host=IP, port=PORT, auto_open=True, auto_close=True)
else:
print("Using RTU backend;")
ID = 1 # UNIT ID
self.bus = os.open(self.unit, os.O_RDONLY | os.O_NONBLOCK) # read bus file to drain buffers
try:
buf = os.read(self.bus, 1000) # read 1k chars to empty buffer before starting the instrument
except OSError:
pass # the buffer is empty
client = minimalmodbus.Instrument(self.unit, ID) # setup the minimal modbus client
client.debug = False
client.precalculate_read_size = True
client.timeout = 0.05
self.client = client
wait_time = 0.00
print("request object created: ", self.mode, ";\n")
test = self.comm_test()
print("Comm test: ", test, ";\n")
def close(self):
self.client.serial.close()
def comm_test(self):
print("Running Comm test;")
fd = os.open("RAM/request.log", os.O_WRONLY | os.O_CREAT)
self.modbusregister(101, 0) # Read non savecair flow address
first = self.response
os.write(fd, bytes("Testing Non-savecair address 101:" + str(first) + "\n", "utf-8"))
print("Testing Non-savecair address 101:" + str(first) + ";")
self.modbusregister(12543, 0) # Read savecair address space
second = self.response
print("Testing savecair address 12543:" + str(second) + ";")
os.write(fd, bytes("Testing savecair address 12543:" + str(second) + "\n", "utf-8"))
if ((first == 0 and second == 0)
or (first == "no data" and second == "no data")
or (first == '' and second == '')
or (first == 0 and second == 'no data')
or (first == "no data" and second == 0)):
os.write(fd, bytes("Request object Failed communications test.\n", "utf-8"))
os.close(fd)
return False
os.write(fd, bytes("Request object Passed communications test.\n", "utf-8"))
os.close(fd)
return True
def modbusregisters(self, start, count, signed=False):
self.client.precalculate_read_size = True
self.latest_request_address = start
self.latest_request_mode = "Multi"
self.latest_request_count = count
self.iter += 1
try:
self.response = "no data"
if self.mode == "RTU":
self.response = self.client.read_registers(start, count)
else:
self.response = self.client.read_holding_registers(start, count)
if signed:
for each in self.response:
if each & 0x8000:
each -= 0xFFFF
except ValueError as error:
if -1 != error.message.find("\x01\x83\x02\xc0\xf1"):
print("multi, address out of range;")
# exit()
self.checksum_errors += 1
self.modbusregisters(start, count)
except IOError:
self.connect_errors += 1
if self.connect_errors > 1000 or self.multi_errors > 1000:
self.error_review()
if self.rate < 0.99:
self.modbusregisters(start, count)
self.client.precalculate_read_size = False
def error_review(self):
delta = self.iter - self.error_time
self.error_time = self.iter
if delta != 0:
rate = float(self.connect_errors +
self.checksum_errors +
self.write_errors +
self.multi_errors) / delta
else:
rate = 0.0
if rate >= 0.99:
os.read(self.bus, 1000)
self.connection_timeout += 1
time.sleep(10)
fd = os.open("RAM/err", os.O_WRONLY)
os.lseek(fd, os.SEEK_SET, os.SEEK_END)
os.write(fd, bytes("""read error high rate,
possible no communication with unit, error rate over 99%\n""", "utf-8"))
os.fsync(fd)
os.close(fd)
self.close()
self.setup(self.unit, self.mode)
#exit(-1)
os.system("echo " + str(rate)
+ " "
+ str(self.wait_time)
+ " > RAM/error_rate")
self.connect_errors = 0
self.checksum_errors = 0
self.write_errors = 0
self.multi_errors = 0
def modbusregister(self, address, decimals):
"""
:type self: request Object for modbus comm
"""
self.latest_request_address = address
self.latest_request_mode = "Single"
self.latest_request_decimals = decimals
if self.mode == "RTU":
self.iter += 1
self.client.precalculate_read_size = True
try:
self.response = "no data"
os.read(self.bus, 20) # bus purge
self.response = self.client.read_register(
address, decimals,
signed=True)
except (IOError, ValueError, TypeError):
self.connect_errors += 1
if self.connect_errors > 1000:
self.error_review()
try:
os.read(self.bus, 20) # bus purge
except TypeError:
pass # read should not typeError here, but does somehow.
if address == 12543 and self.connect_errors >= 10:
return 0
self.modbusregister(address, decimals)
else:
data = "noData"
try:
data = self.client.read_holding_registers(address, 1)
self.response = data[0]
if decimals != 0:
self.response /= (decimals * 10)
except TypeError:
print("TCP read error on address:", address, data)
self.reset = 0 # set reset counter to 0
def write_register(self, reg, value, tries=10):
if self.mode == "RTU":
self.iter += 1
self.client.precalculate_read_size = True
try:
if tries > 0:
self.client.write_register(reg, value, 0, 6)
except (IOError, ValueError):
self.write_errors += 1
if tries > 0:
self.write_register(reg, value, tries=tries - 1)
self.modbusregister(reg, 0)
if value != self.response and tries > 0:
self.write_register(reg, value, tries=tries - 1)
if tries == 0:
fd = os.open("RAM/err", os.O_WRONLY)
os.write(fd, bytes("Write error, no tries left on register:" + str(reg) + "\n", "utf-8"))
os.close(fd)
else:
try:
valid = self.client.write_single_register(reg, value)
self.modbusregister(reg, 0)
if value != self.response and tries > 0:
self.write_errors += 1
self.write_register(reg, value, tries=tries - 1)
if tries == 0:
fd = os.open("RAM/err", os.O_WRONLY)
os.write(fd, bytes("Write error, no tries left on register:" + str(reg) + " " + str(valid) + "\n", "utf-8"))
os.close(fd)
except:
with os.open("RAM/err", os.O_WRONLY) as fd:
os.write(fd, bytes("TCP write error on addrs:" + str(reg) + "\n", "utf-8"))
if "__main__" == __name__:
# Setup serial, RS 485 to machine
if os.path.lexists("/dev/ttyUSB0"):
print("Communication started on device ttyUSB0;")
unit = "/dev/ttyUSB0"
elif os.path.lexists("/dev/serial0"):
print("Communication started on device Serial0;")
unit = "/dev/serial0"
else:
print("Communication started on device ttyAMA0;")
unit = "/dev/ttyAMA0"
req = Request()
req.setup(unit, "RTU")
req = Request()
req.setup(unit, "TCP")