forked from sfera-labs/exo-sense-py-modbus
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathcommon.py
244 lines (189 loc) · 9.03 KB
/
common.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
#!/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 ..sys_imports import List, Optional, Tuple, Union
# custom packages
from .. import functions, const as Const
from ..common import CommonModbusFunctions, Request
class AsyncRequest(Request):
"""Asynchronously deconstruct request data received via TCP or Serial"""
async def send_response(self,
values: Optional[list] = None,
signed: bool = True) -> None:
"""
Send a response via the configured interface.
:param values: The values
:type values: Optional[list]
:param signed: Indicates if signed values are used
:type signed: bool
"""
await self._itf.send_response(self,
self.unit_addr,
self.function,
self.register_addr,
self.quantity,
self.data,
values,
signed)
async def send_exception(self, exception_code: int) -> None:
"""
Send an exception response.
:param exception_code: The exception code
:type exception_code: int
"""
await self._itf.send_exception_response(self,
self.unit_addr,
self.function,
exception_code)
class CommonAsyncModbusFunctions(CommonModbusFunctions):
"""Common Async Modbus functions"""
async def read_coils(self,
slave_addr: int,
starting_addr: int,
coil_qty: int) -> List[bool]:
"""@see CommonModbusFunctions.read_coils"""
modbus_pdu = functions.read_coils(starting_address=starting_addr,
quantity=coil_qty)
response = await self._send_receive(slave_addr=slave_addr,
modbus_pdu=modbus_pdu,
count=True)
status_pdu = functions.bytes_to_bool(byte_list=response,
bit_qty=coil_qty)
return status_pdu
async def read_discrete_inputs(self,
slave_addr: int,
starting_addr: int,
input_qty: int) -> List[bool]:
"""@see CommonModbusFunctions.read_discrete_inputs"""
modbus_pdu = functions.read_discrete_inputs(
starting_address=starting_addr,
quantity=input_qty)
response = await self._send_receive(slave_addr=slave_addr,
modbus_pdu=modbus_pdu,
count=True)
status_pdu = functions.bytes_to_bool(byte_list=response,
bit_qty=input_qty)
return status_pdu
async def read_holding_registers(self,
slave_addr: int,
starting_addr: int,
register_qty: int,
signed: bool = True) -> Tuple[int, ...]:
"""@see CommonModbusFunctions.read_holding_registers"""
modbus_pdu = functions.read_holding_registers(
starting_address=starting_addr,
quantity=register_qty)
response = await self._send_receive(slave_addr=slave_addr,
modbus_pdu=modbus_pdu,
count=True)
register_value = functions.to_short(byte_array=response, signed=signed)
return register_value
async def read_input_registers(self,
slave_addr: int,
starting_addr: int,
register_qty: int,
signed: bool = True) -> Tuple[int, ...]:
"""@see CommonModbusFunctions.read_input_registers"""
modbus_pdu = functions.read_input_registers(
starting_address=starting_addr,
quantity=register_qty)
response = await self._send_receive(slave_addr=slave_addr,
modbus_pdu=modbus_pdu,
count=True)
register_value = functions.to_short(byte_array=response, signed=signed)
return register_value
async def write_single_coil(self,
slave_addr: int,
output_address: int,
output_value: Union[int, bool]) -> bool:
"""@see CommonModbusFunctions.write_single_coil"""
modbus_pdu = functions.write_single_coil(output_address=output_address,
output_value=output_value)
response = await self._send_receive(slave_addr=slave_addr,
modbus_pdu=modbus_pdu,
count=False)
if response is None:
return False
operation_status = functions.validate_resp_data(
data=response,
function_code=Const.WRITE_SINGLE_COIL,
address=output_address,
value=output_value,
signed=False)
return operation_status
async def write_single_register(self,
slave_addr: int,
register_address: int,
register_value: int,
signed: bool = True) -> bool:
"""@see CommonModbusFunctions.write_single_register"""
modbus_pdu = functions.write_single_register(
register_address=register_address,
register_value=register_value,
signed=signed)
response = await self._send_receive(slave_addr=slave_addr,
modbus_pdu=modbus_pdu,
count=False)
if response is None:
return False
operation_status = functions.validate_resp_data(
data=response,
function_code=Const.WRITE_SINGLE_REGISTER,
address=register_address,
value=register_value,
signed=signed)
return operation_status
async def write_multiple_coils(self,
slave_addr: int,
starting_address: int,
output_values: list) -> bool:
"""@see CommonModbusFunctions.write_multiple_coils"""
modbus_pdu = functions.write_multiple_coils(
starting_address=starting_address,
value_list=output_values)
response = await self._send_receive(slave_addr=slave_addr,
modbus_pdu=modbus_pdu,
count=False)
if response is None:
return False
operation_status = functions.validate_resp_data(
data=response,
function_code=Const.WRITE_MULTIPLE_COILS,
address=starting_address,
quantity=len(output_values))
return operation_status
async def write_multiple_registers(self,
slave_addr: int,
starting_address: int,
register_values: List[int],
signed: bool = True) -> bool:
"""@see CommonModbusFunctions.write_multiple_registers"""
modbus_pdu = functions.write_multiple_registers(
starting_address=starting_address,
register_values=register_values,
signed=signed)
response = await self._send_receive(slave_addr=slave_addr,
modbus_pdu=modbus_pdu,
count=False)
if response is None:
return False
operation_status = functions.validate_resp_data(
data=response,
function_code=Const.WRITE_MULTIPLE_REGISTERS,
address=starting_address,
quantity=len(register_values),
signed=signed
)
return operation_status
async def _send_receive(self,
slave_addr: int,
modbus_pdu: bytes,
count: bool) -> bytes:
raise NotImplementedError("Must be overridden by subclass.")