-
Notifications
You must be signed in to change notification settings - Fork 881
/
client_calls.py
executable file
·229 lines (178 loc) · 7.74 KB
/
client_calls.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
#!/usr/bin/env python3
"""Pymodbus Client modbus all calls example.
Please see method **template_call**
for a template on how to make modbus calls and check for different
error conditions.
The handle* functions each handle a set of modbus calls with the
same register type (e.g. coils).
All available modbus calls are present.
If you are performing a request that is not available in the client
mixin, you have to perform the request like this instead::
from pymodbus.diag_message import ClearCountersRequest
from pymodbus.diag_message import ClearCountersResponse
request = ClearCountersRequest()
response = client.execute(request)
if isinstance(response, ClearCountersResponse):
... do something with the response
This example uses client_async.py and client_sync.py to handle connection,
and have the same options.
The corresponding server must be started before e.g. as:
./server_async.py
"""
import logging
import client_sync
_logger = logging.getLogger(__file__)
_logger.setLevel("DEBUG")
SLAVE = 0x01
# --------------------------------------------------
# Template on how to make modbus calls (sync/async).
# all calls follow the same schema,
# --------------------------------------------------
def template_call(client):
"""Show complete modbus call, sync version."""
try:
rr = client.read_coils(32, 1, slave=SLAVE)
except client_sync.ModbusException as exc:
txt = f"ERROR: exception in pymodbus {exc}"
_logger.error(txt)
raise exc
if rr.isError():
txt = "ERROR: pymodbus returned an error!"
_logger.error(txt)
raise client_sync.ModbusException(txt)
# Validate data
txt = f"### Template coils response: {rr.bits!s}"
_logger.debug(txt)
# ------------------------------------------------------
# Call modbus device (all possible calls are presented).
# ------------------------------------------------------
def handle_coils(client):
"""Read/Write coils."""
_logger.info("### Reading Coil different number of bits (return 8 bits multiples)")
rr = client.read_coils(1, 1, slave=SLAVE)
assert not rr.isError() # test that call was OK
assert len(rr.bits) == 8
rr = client.read_coils(1, 5, slave=SLAVE)
assert not rr.isError() # test that call was OK
assert len(rr.bits) == 8
rr = client.read_coils(1, 12, slave=SLAVE)
assert not rr.isError() # test that call was OK
assert len(rr.bits) == 16
rr = client.read_coils(1, 17, slave=SLAVE)
assert not rr.isError() # test that call was OK
assert len(rr.bits) == 24
_logger.info("### Write false/true to coils and read to verify")
client.write_coil(0, True, slave=SLAVE)
rr = client.read_coils(0, 1, slave=SLAVE)
assert not rr.isError() # test that call was OK
assert rr.bits[0] # test the expected value
client.write_coils(1, [True] * 21, slave=SLAVE)
rr = client.read_coils(1, 21, slave=SLAVE)
assert not rr.isError() # test that call was OK
resp = [True] * 21
# If the returned output quantity is not a multiple of eight,
# the remaining bits in the final data byte will be padded with zeros
# (toward the high order end of the byte).
resp.extend([False] * 3)
assert rr.bits == resp # test the expected value
_logger.info("### Write False to address 1-8 coils")
client.write_coils(1, [False] * 8, slave=SLAVE)
rr = client.read_coils(1, 8, slave=SLAVE)
assert not rr.isError() # test that call was OK
assert rr.bits == [False] * 8 # test the expected value
def handle_discrete_input(client):
"""Read discrete inputs."""
_logger.info("### Reading discrete input, Read address:0-7")
rr = client.read_discrete_inputs(0, 8, slave=SLAVE)
assert not rr.isError() # test that call was OK
assert len(rr.bits) == 8
def handle_holding_registers(client):
"""Read/write holding registers."""
_logger.info("### write holding register and read holding registers")
client.write_register(1, 10, slave=SLAVE)
rr = client.read_holding_registers(1, 1, slave=SLAVE)
assert not rr.isError() # test that call was OK
assert rr.registers[0] == 10
client.write_registers(1, [10] * 8, slave=SLAVE)
rr = client.read_holding_registers(1, 8, slave=SLAVE)
assert not rr.isError() # test that call was OK
assert rr.registers == [10] * 8
_logger.info("### write read holding registers, using **kwargs")
arguments = {
"read_address": 1,
"read_count": 8,
"write_address": 1,
"values": [256, 128, 100, 50, 25, 10, 5, 1],
}
client.readwrite_registers(slave=SLAVE, **arguments)
rr = client.read_holding_registers(1, 8, slave=SLAVE)
assert not rr.isError() # test that call was OK
assert rr.registers == arguments["values"]
def handle_input_registers(client):
"""Read input registers."""
_logger.info("### read input registers")
rr = client.read_input_registers(1, 8, slave=SLAVE)
assert not rr.isError() # test that call was OK
assert len(rr.registers) == 8
def execute_information_requests(client): # pragma no cover
"""Execute extended information requests."""
_logger.info("### Running information requests.")
rr = client.read_device_information(slave=SLAVE)
assert not rr.isError() # test that call was OK
assert rr.information[0] == b"Pymodbus"
rr = client.report_slave_id(slave=SLAVE)
assert not rr.isError() # test that call was OK
# assert not rr.status
rr = client.read_exception_status(slave=SLAVE)
assert not rr.isError() # test that call was OK
# assert not rr.status
rr = client.diag_get_comm_event_counter(slave=SLAVE)
assert not rr.isError() # test that call was OK
# assert rr.status
# assert not rr.count
rr = client.diag_get_comm_event_log(slave=SLAVE)
assert not rr.isError() # test that call was OK
# assert rr.status
# assert not (rr.event_count + rr.message_count + len(rr.events))
def execute_diagnostic_requests(client): # pragma no cover
"""Execute extended diagnostic requests."""
_logger.info("### Running diagnostic requests.")
message = b"OK"
rr = client.diag_query_data(msg=message, slave=SLAVE)
assert not rr.isError() # test that call was OK
assert rr.message == message
client.diag_restart_communication(True, slave=SLAVE)
client.diag_read_diagnostic_register(slave=SLAVE)
client.diag_change_ascii_input_delimeter(slave=SLAVE)
# NOT WORKING: await client.diag_force_listen_only(slave=SLAVE)
client.diag_clear_counters()
client.diag_read_bus_comm_error_count(slave=SLAVE)
client.diag_read_bus_exception_error_count(slave=SLAVE)
client.diag_read_slave_message_count(slave=SLAVE)
client.diag_read_slave_no_response_count(slave=SLAVE)
client.diag_read_slave_nak_count(slave=SLAVE)
client.diag_read_slave_busy_count(slave=SLAVE)
client.diag_read_bus_char_overrun_count(slave=SLAVE)
client.diag_read_iop_overrun_count(slave=SLAVE)
client.diag_clear_overrun_counter(slave=SLAVE)
# NOT WORKING client.diag_getclear_modbus_response(slave=SLAVE)
# ------------------------
# Run the calls in groups.
# ------------------------
def run_sync_calls(client):
"""Demonstrate basic read/write calls."""
template_call(client)
handle_coils(client)
handle_discrete_input(client)
handle_holding_registers(client)
handle_input_registers(client)
# awaiting fix: execute_information_requests(client)
# awaiting fix: execute_diagnostic_requests(client)
def main(cmdline=None):
"""Combine setup and run."""
client = client_sync.setup_sync_client(
description="Run synchronous client.", cmdline=cmdline
)
client_sync.run_sync_client(client, modbus_calls=run_sync_calls)
if __name__ == "__main__":
main() # pragma: no cover