-
Notifications
You must be signed in to change notification settings - Fork 55
/
pymodbus_server.py
289 lines (232 loc) · 9.22 KB
/
pymodbus_server.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
from __future__ import absolute_import, unicode_literals
from pymodbus.server.sync import ModbusSerialServer
from pymodbus.server.sync import ModbusTcpServer
from pymodbus.server.sync import ModbusSingleRequestHandler
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.transaction import ModbusRtuFramer
from pymodbus.payload import BinaryPayloadDecoder, Endian, BinaryPayloadBuilder
from threading import Thread, RLock
import logging
log = logging.getLogger(__name__)
SERVERS = {
"tcp": ModbusTcpServer,
"rtu": ModbusSerialServer
}
_FX_MAPPER = {
"coils": 1,
'discrete_inputs': 2,
'input_registers': 4,
'holding_registers': 3
}
_STORE_MAPPER = {
"coils": "c",
'discrete_inputs': 'd',
'input_registers': 'i',
'holding_registers': 'h'
}
DECODERS = {
'int16': "decode_16bit_int",
'int32': "decode_32bit_int",
'int64': "decode_64bit_int",
'uint16': "decode_16bit_uint",
'uint32': "decode_32bit_uint",
'uint64': "decode_64bit_uint",
'float32': "decode_32bit_float",
'float64': "decode_64bit_float",
}
ENCODERS = {
'int16': "add_16bit_int",
'int32': "add_32bit_int",
'int64': "add_64bit_int",
'uint16': "add_16bit_uint",
'uint32': "add_32bit_uint",
'uint64': "add_64bit_uint",
'float32': "add_32bit_float",
'float64': "add_64bit_float",
}
class CustomDataBlock(ModbusSequentialDataBlock):
def __init__(self, *args, **kwargs):
super(CustomDataBlock, self).__init__(*args, **kwargs)
self._data_lock = RLock()
def update(self, size):
with self._data_lock:
values = [self.default_value] * size
self.values.extend(values)
class CustomSingleRequestHandler(ModbusSingleRequestHandler):
def __init__(self, request, client_address, server):
self.request = request
self.client_address = client_address
self.server = server
self.running = True
self.setup()
class MbusSerialServer(ModbusSerialServer):
handler = None
def __init__(self, *args, **kwargs):
super(MbusSerialServer, self).__init__(*args, **kwargs)
self._build_handler()
def _build_handler(self):
''' A helper method to create and monkeypatch
a serial handler.
:returns: A patched handler
'''
request = self.socket
request.send = request.write
request.recv = request.read
self.handler = CustomSingleRequestHandler(request,
(self.device, self.device), self)
def serve_forever(self):
''' Callback for connecting a new client thread
:param request: The request to handle
:param client: The address of the client
'''
log.debug("Started thread to serve client")
if not self.handler:
self._build_handler()
while self.is_running:
self.handler.handle()
def server_close(self):
''' Callback for stopping the running server
'''
log.debug("Modbus server stopped")
self.is_running = False
self.handler.finish()
self.handler.running = False
self.handler = None
self.socket.close()
class ThreadedModbusServer(Thread):
def __init__(self, server):
super(ThreadedModbusServer, self).__init__(name="ModbusServerThread")
self._server = server
self.daemon = True
def run(self):
self._server.serve_forever()
def stop(self):
if isinstance(self._server, ModbusTcpServer):
self._server.shutdown()
else:
if self._server.socket:
self._server.server_close()
class ModbusSimu(object):
_server_add = ()
def __init__(self, server="tcp", *args, **kwargs):
# initialize server information
self.identity = ModbusDeviceIdentification()
self._add_device_info()
self._server_type = server
self._port = kwargs.get('port', None)
self.context = ModbusServerContext(single=False)
self.simulate = kwargs.get('simulate', False)
byte_order = kwargs.pop("byte_order", "big")
word_order = kwargs.pop("word_order", "big")
self.byte_order = Endian.Big if byte_order == "big" else Endian.Little
self.word_order = Endian.Big if word_order == "big" else Endian.Little
self.dirty = False
if server == "tcp":
self._port = int(self._port)
self._address = kwargs.get("address", "localhost")
self.server = ModbusTcpServer(self.context,
identity=self.identity,
address=(self._address, self._port))
else:
self.server = MbusSerialServer(self.context,
framer=ModbusRtuFramer,
identity=self.identity, **kwargs)
self.server_thread = ThreadedModbusServer(self.server)
def _add_device_info(self):
self.identity.VendorName = 'Riptide'
self.identity.ProductCode = 'sim-007'
self.identity.VendorUrl = 'http://github.com/riptideio/'
self.identity.ProductName = 'Modbus Server'
self.identity.ModelName = 'Modbus Server'
self.identity.MajorMinorRevision = '2.0.0'
@property
def server_type(self):
return self._server_type
@property
def port(self):
return self._port
def _add_default_slave_context(self):
return ModbusSlaveContext(
di=CustomDataBlock(0, 0),
hr=CustomDataBlock(0, 0),
co=CustomDataBlock(0, 0),
ir=CustomDataBlock(0, 0),
)
@staticmethod
def _calc_offset(block_name, address):
address = int(address)
if block_name == "coils":
return address
elif block_name == "discrete_inputs":
return address-10001 if address >= 10001 else address
elif block_name == "input_registers":
return address - 30001 if address >= 30001 else address
else:
return address - 40001 if address >= 40001 else address
def add_slave(self, slave_id):
self.context[slave_id] = self._add_default_slave_context()
def remove_slave(self, slave_id):
del self.context[slave_id]
def remove_all_slave(self):
self.context = ModbusServerContext(single=False)
def add_block(self, slave_id, block_name, block_type, starting_add, size):
slave = self.get_slave(slave_id)
if not slave.validate(_FX_MAPPER[block_name], starting_add, count=size):
slave.store[_STORE_MAPPER[block_name]].update(size)
else:
log.debug("Block '{}' on slave '{}' already exists".format(block_name, slave_id))
def remove_block(self, slave_id, block_name):
slave = self.get_slave(slave_id)
slave.store[_STORE_MAPPER[block_name]].reset()
def remove_all_blocks(self, slave_id):
slave = self.get_slave(slave_id)
slave.remove_all_blocks()
def set_values(self, slave_id, block_name, address, values):
values = values if isinstance(values, (list, tuple)) else [values]
slave = self.get_slave(slave_id)
address = self._calc_offset(block_name, address)
if slave.validate(_FX_MAPPER[block_name], address, count=len(values)):
slave.setValues(_FX_MAPPER[block_name], address, values)
def get_values(self, slave_id, block_name, address, size=1):
slave = self.get_slave(slave_id)
address = self._calc_offset(block_name, address)
if slave.validate(_FX_MAPPER[block_name], address, count=size):
return slave.getValues(_FX_MAPPER[block_name], address, int(size))
def get_slave(self, slave_id):
return self.context[slave_id]
def decode(self, slave_id, block_name, offset, formatter):
count = 1
if '32' in formatter:
count = 2
elif '64' in formatter:
count = 4
values = self.get_values(slave_id, block_name, offset, count)
if values:
decoder = BinaryPayloadDecoder.fromRegisters(
values, byteorder=self.byte_order, wordorder=self.word_order)
values = getattr(decoder, DECODERS.get(formatter))()
return values, count
def encode(self, slave_id, block_name, offset, value, formatter):
builder = BinaryPayloadBuilder(byteorder=self.byte_order,
wordorder=self.word_order)
add_method = ENCODERS.get(formatter)
if 'int' in add_method: # Temp fix
value = int(value)
getattr(builder, add_method)(value)
payload = builder.to_registers()
return self.set_values(slave_id, block_name, offset, payload)
def start(self):
if self.dirty:
self.server_thread = ThreadedModbusServer(self.server)
self.server_thread.start()
def stop(self):
self.server_thread.stop()
self.dirty = True
# if self._server_type == 'rtu':
# self._serial.close()
# self._server_add = ()
def get_slaves(self):
if self.server is not None:
return self.server._databank._slaves