forked from sfera-labs/exo-sense-py-modbus
-
Notifications
You must be signed in to change notification settings - Fork 42
/
serial.py
347 lines (269 loc) · 12.7 KB
/
serial.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
#!/usr/bin/env python
#
# Copyright (c) 2019, Pycom Limited.
#
# This software is licensed under the GNU GPL version 3 or any
# later version, with permitted additional terms. For more information
# see the Pycom Licence v1.0 document supplied with this file, or
# available at https://www.pycom.io/opensource/licensing
#
# system packages
from machine import UART
from machine import Pin
import struct
import time
import machine
# custom packages
from . import const as Const
from . import functions
from .common import Request
from .common import ModbusException
class Serial(object):
def __init__(self,
uart_id=1,
baudrate=9600,
data_bits=8,
stop_bits=1,
parity=None,
pins=None,
ctrl_pin=None):
self._uart = UART(uart_id,
baudrate=baudrate,
bits=data_bits,
parity=parity,
stop=stop_bits,
# timeout_chars=2, # WiPy only
# pins=pins # WiPy only
tx=pins[0],
rx=pins[1]
)
if ctrl_pin is not None:
self._ctrlPin = Pin(ctrl_pin, mode=Pin.OUT)
else:
self._ctrlPin = None
if baudrate <= 19200:
# 4010us (approx. 4ms) @ 9600 baud
self._t35chars = (3500000 * (data_bits + stop_bits + 2)) // baudrate
else:
self._t35chars = 1750 # 1750us (approx. 1.75ms)
def _calculate_crc16(self, data):
crc = 0xFFFF
for char in data:
crc = (crc >> 8) ^ Const.CRC16_TABLE[((crc) ^ char) & 0xFF]
return struct.pack('<H', crc)
def _bytes_to_bool(self, byte_list):
bool_list = []
for index, byte in enumerate(byte_list):
bool_list.extend([bool(byte & (1 << n)) for n in range(8)])
return bool_list
def _to_short(self, byte_array, signed=True):
response_quantity = int(len(byte_array) / 2)
fmt = '>' + (('h' if signed else 'H') * response_quantity)
return struct.unpack(fmt, byte_array)
def _exit_read(self, response):
if response[1] >= Const.ERROR_BIAS:
if len(response) < Const.ERROR_RESP_LEN:
return False
elif (Const.READ_COILS <= response[1] <= Const.READ_INPUT_REGISTER):
expected_len = Const.RESPONSE_HDR_LENGTH + 1 + response[2] + Const.CRC_LENGTH
if len(response) < expected_len:
return False
elif len(response) < Const.FIXED_RESP_LEN:
return False
return True
def _uart_read(self):
response = bytearray()
for x in range(1, 40):
if self._uart.any():
# WiPy only
# response.extend(self._uart.readall())
response.extend(self._uart.read())
# variable length function codes may require multiple reads
if self._exit_read(response):
break
time.sleep(0.05)
return response
def _uart_read_frame(self, timeout=None):
received_bytes = bytearray()
# set timeout to at least twice the time between two frames in case the
# timeout was set to zero or None
if timeout == 0 or timeout is None:
timeout = 2 * self._t35chars # in milliseconds
start_us = time.ticks_us()
# stay inside this while loop at least for the timeout time
while (time.ticks_diff(time.ticks_us(), start_us) <= timeout):
# check amount of available characters
if self._uart.any():
# remember this time in microseconds
last_byte_ts = time.ticks_us()
# do not stop reading and appending the result to the buffer
# until the time between two frames elapsed
while time.ticks_diff(time.ticks_us(), last_byte_ts) <= self._t35chars:
# WiPy only
# r = self._uart.readall()
r = self._uart.read()
# if something has been read after the first iteration of
# this inner while loop (during self._t35chars time)
if r is not None:
# append the new read stuff to the buffer
received_bytes.extend(r)
# update the timestamp of the last byte being read
last_byte_ts = time.ticks_us()
# if something has been read before the overall timeout is reached
if len(received_bytes) > 0:
return received_bytes
# return the result in case the overall timeout has been reached
return received_bytes
def _send(self, modbus_pdu, slave_addr):
serial_pdu = bytearray()
serial_pdu.append(slave_addr)
serial_pdu.extend(modbus_pdu)
crc = self._calculate_crc16(serial_pdu)
serial_pdu.extend(crc)
if self._ctrlPin:
self._ctrlPin(1)
self._uart.write(serial_pdu)
if self._ctrlPin:
while not self._uart.wait_tx_done(2):
machine.idle()
time.sleep_us(self._t35chars)
self._ctrlPin(0)
def _send_receive(self, modbus_pdu, slave_addr, count):
# flush the Rx FIFO
self._uart.read()
self._send(modbus_pdu, slave_addr)
return self._validate_resp_hdr(self._uart_read(), slave_addr, modbus_pdu[0], count)
def _validate_resp_hdr(self, response, slave_addr, function_code, count):
if len(response) == 0:
raise OSError('no data received from slave')
resp_crc = response[-Const.CRC_LENGTH:]
expected_crc = self._calculate_crc16(response[0:len(response) - Const.CRC_LENGTH])
if ((resp_crc[0] is not expected_crc[0]) or (resp_crc[1] is not expected_crc[1])):
raise OSError('invalid response CRC')
if (response[0] != slave_addr):
raise ValueError('wrong slave address')
if (response[1] == (function_code + Const.ERROR_BIAS)):
raise ValueError('slave returned exception code: {:d}'.
format(response[2]))
hdr_length = (Const.RESPONSE_HDR_LENGTH + 1) if count else Const.RESPONSE_HDR_LENGTH
return response[hdr_length:len(response) - Const.CRC_LENGTH]
def read_coils(self, slave_addr, starting_addr, coil_qty):
modbus_pdu = functions.read_coils(starting_addr, coil_qty)
resp_data = self._send_receive(modbus_pdu, slave_addr, True)
status_pdu = self._bytes_to_bool(resp_data)
return status_pdu
def read_discrete_inputs(self, slave_addr, starting_addr, input_qty):
modbus_pdu = functions.read_discrete_inputs(starting_addr, input_qty)
resp_data = self._send_receive(modbus_pdu, slave_addr, True)
status_pdu = self._bytes_to_bool(resp_data)
return status_pdu
def read_holding_registers(self,
slave_addr,
starting_addr,
register_qty,
signed=True):
modbus_pdu = functions.read_holding_registers(starting_addr, register_qty)
resp_data = self._send_receive(modbus_pdu, slave_addr, True)
register_value = self._to_short(resp_data, signed)
return register_value
def read_input_registers(self,
slave_addr,
starting_addr,
register_qty,
signed=True):
modbus_pdu = functions.read_input_registers(starting_addr,
register_qty)
resp_data = self._send_receive(modbus_pdu, slave_addr, True)
register_value = self._to_short(resp_data, signed)
return register_value
def write_single_coil(self, slave_addr, output_address, output_value):
modbus_pdu = functions.write_single_coil(output_address, output_value)
resp_data = self._send_receive(modbus_pdu, slave_addr, False)
operation_status = functions.validate_resp_data(resp_data,
Const.WRITE_SINGLE_COIL,
output_address,
value=output_value,
signed=False)
return operation_status
def write_single_register(self,
slave_addr,
register_address,
register_value,
signed=True):
modbus_pdu = functions.write_single_register(register_address,
register_value,
signed)
resp_data = self._send_receive(modbus_pdu, slave_addr, False)
operation_status = functions.validate_resp_data(resp_data,
Const.WRITE_SINGLE_REGISTER,
register_address,
value=register_value,
signed=signed)
return operation_status
def write_multiple_coils(self,
slave_addr,
starting_address,
output_values):
modbus_pdu = functions.write_multiple_coils(starting_address,
output_values)
resp_data = self._send_receive(modbus_pdu, slave_addr, False)
operation_status = functions.validate_resp_data(resp_data,
Const.WRITE_MULTIPLE_COILS,
starting_address,
quantity=len(output_values))
return operation_status
def write_multiple_registers(self,
slave_addr,
starting_address,
register_values,
signed=True):
modbus_pdu = functions.write_multiple_registers(starting_address,
register_values,
signed)
resp_data = self._send_receive(modbus_pdu, slave_addr, False)
operation_status = functions.validate_resp_data(resp_data,
Const.WRITE_MULTIPLE_REGISTERS,
starting_address,
quantity=len(register_values))
return operation_status
def send_response(self,
slave_addr,
function_code,
request_register_addr,
request_register_qty,
request_data,
values=None,
signed=True):
modbus_pdu = functions.response(function_code,
request_register_addr,
request_register_qty,
request_data,
values,
signed)
self._send(modbus_pdu, slave_addr)
def send_exception_response(self,
slave_addr,
function_code,
exception_code):
modbus_pdu = functions.exception_response(function_code,
exception_code)
self._send(modbus_pdu, slave_addr)
def get_request(self, unit_addr_list, timeout=None):
req = self._uart_read_frame(timeout)
if len(req) < 8:
return None
if req[0] not in unit_addr_list:
return None
req_crc = req[-Const.CRC_LENGTH:]
req_no_crc = req[:-Const.CRC_LENGTH]
expected_crc = self._calculate_crc16(req_no_crc)
if (req_crc[0] != expected_crc[0]) or (req_crc[1] != expected_crc[1]):
return None
try:
request = Request(self, req_no_crc)
except ModbusException as e:
self.send_exception_response(req[0],
e.function_code,
e.exception_code)
return None
return request