You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
await client.connect() # connect to device, reconnect automatically
32
139
await client.write_coil(1, True, slave=1) # set information in device
33
-
result = await client.read_coils(1, 1, slave=1) # get information from device
140
+
result = await client.read_coils(2, 3, slave=1) # get information from device
34
141
print(result.bits[0]) # use information
35
142
client.close() # Disconnect device
36
143
144
+
The line :mod:`client = ModbusAsyncTcpClient('MyDevice.lan')` only creates the object it does not activate
145
+
anything.
146
+
147
+
The line :mod:`await client.connect()` connects to the device (or comm port), if this cannot connect successfully within
148
+
the timeout it throws an exception. If connected successfully reconnecting later is handled automatically
149
+
150
+
The line :mod:`await client.write_coil(1, True, slave=1)` is an example of a write request, set address 1 to True on device 1 (slave).
151
+
152
+
The line :mod:`result = await client.read_coils(2, 1, slave=1)` is an example of a read request, get the value of address 2, 3 and 4 (count = 3) from device 1 (slave).
153
+
154
+
The last line :mod:`client.close()` closes the connection and render the object inactive.
155
+
37
156
Large parts of the implementation are shared between the different classes,
38
157
to ensure high stability and efficient maintenance.
39
158
@@ -46,14 +165,74 @@ The asynchronous client only runs in the thread where the asyncio loop is create
46
165
it does not provide mechanisms to prevent (semi)parallel calls,
47
166
that must be prevented at application level.
48
167
49
-
Client classes
50
-
--------------
51
168
52
-
.. autoclass:: pymodbus.client.ModbusBaseClient
53
-
:members:
54
-
:member-order: bysource
55
-
:show-inheritance:
169
+
Client device addressing
170
+
------------------------
171
+
172
+
With **TCP**, **TLS** and **UDP**, the tcp/ip address of the physical device is defined when creating the object.
173
+
The logical devices represented by the device is addressed with the :mod:`slave=` parameter.
174
+
175
+
With **Serial**, the comm port is defined when creating the object.
176
+
The physical devices are addressed with the :mod:`slave=` parameter.
177
+
178
+
:mod:`slave=0` is used as broadcast in order to address all devices.
179
+
However experience shows that modern devices do not allow broadcast, mostly because it is
180
+
inheriently dangerous. With :mod:`slave=0` the application can get upto 254 responses on a single request!
181
+
182
+
The simple request calls (mixin) do NOT support broadcast, if an application wants to use broadcast
183
+
it must call :mod:`client.execute` and deal with the responses.
184
+
185
+
186
+
187
+
Client response handling
188
+
------------------------
189
+
190
+
All simple request calls (mixin) return a unified result independent whether it´s a read, write or diagnostic call.
191
+
192
+
The application should evaluate the result generically::
193
+
194
+
try:
195
+
rr = await client.read_coils(1, 1, slave=1)
196
+
except ModbusException as exc:
197
+
_logger.error(f"ERROR: exception in pymodbus {exc}")
198
+
raise exc
199
+
if rr.isError():
200
+
_logger.error("ERROR: pymodbus returned an error!")
201
+
raise ModbusException(txt)
202
+
203
+
:mod:`except ModbusException as exc:` happens generally when pymodbus experiences an internal error.
204
+
There are a few situation where a unexpected response from a device can cause an exception.
205
+
206
+
:mod:`rr.isError()` is set whenever the device reports a problem.
207
+
208
+
And in case of read retrieve the data depending on type of request
209
+
210
+
- :mod:`rr.bits` is set for coils / input_register requests
211
+
- :mod:`rr.registers` is set for other requests
212
+
213
+
214
+
Client interface classes
215
+
------------------------
216
+
217
+
There are a client class for each type of communication and for asynchronous/synchronous
0 commit comments