66The following is an example of how to use the asynchronous modbus
77client implementation from pymodbus.
88"""
9- # --------------------------------------------------------------------------- #
9+ # --------------------------------------------------------------------------- #
1010# import needed libraries
11- # --------------------------------------------------------------------------- #
11+ # --------------------------------------------------------------------------- #
1212from twisted .internet import reactor , protocol
1313from pymodbus .constants import Defaults
1414
15- # --------------------------------------------------------------------------- #
15+ # --------------------------------------------------------------------------- #
1616# choose the requested modbus protocol
17- # --------------------------------------------------------------------------- #
17+ # --------------------------------------------------------------------------- #
1818from pymodbus .client .async import ModbusClientProtocol
1919from pymodbus .client .async import ModbusUdpClientProtocol
2020from pymodbus .framer .rtu_framer import ModbusRtuFramer
2121
22- # --------------------------------------------------------------------------- #
22+ # --------------------------------------------------------------------------- #
2323# configure the client logging
24- # --------------------------------------------------------------------------- #
24+ # --------------------------------------------------------------------------- #
2525import logging
2626FORMAT = ('%(asctime)-15s %(threadName)-15s'
2727 ' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s' )
2828logging .basicConfig (format = FORMAT )
2929log = logging .getLogger ()
3030log .setLevel (logging .DEBUG )
3131
32- # --------------------------------------------------------------------------- #
32+ # --------------------------------------------------------------------------- #
3333# helper method to test deferred callbacks
3434# --------------------------------------------------------------------------- #
3535
@@ -40,9 +40,9 @@ def _assertor(value):
4040 deferred .addCallback (lambda r : _assertor (callback (r )))
4141 deferred .addErrback (lambda _ : _assertor (False ))
4242
43- # --------------------------------------------------------------------------- #
43+ # --------------------------------------------------------------------------- #
4444# specify slave to query
45- # --------------------------------------------------------------------------- #
45+ # --------------------------------------------------------------------------- #
4646# The slave to query is specified in an optional parameter for each
4747# individual request. This can be done by specifying the `unit` parameter
4848# which defaults to `0x00`
@@ -64,9 +64,9 @@ def exampleRequests(client):
6464 rr .addCallback (processResponse )
6565 stopAsynchronousTest (client )
6666
67- # --------------------------------------------------------------------------- #
67+ # --------------------------------------------------------------------------- #
6868# example requests
69- # --------------------------------------------------------------------------- #
69+ # --------------------------------------------------------------------------- #
7070# simply call the methods that you would like to use. An example session
7171# is displayed below along with some assert checks. Note that unlike the
7272# synchronous version of the client, the asynchronous version returns
@@ -89,45 +89,45 @@ def stopAsynchronousTest(client):
8989def beginAsynchronousTest (client ):
9090 rq = client .write_coil (1 , True , unit = UNIT )
9191 rr = client .read_coils (1 , 1 , unit = UNIT )
92- dassert (rq , lambda r : r . function_code < 0x80 ) # test for no error
92+ dassert (rq , lambda r : not r . isError () ) # test for no error
9393 dassert (rr , lambda r : r .bits [0 ] == True ) # test the expected value
94-
94+
9595 rq = client .write_coils (1 , [True ]* 8 , unit = UNIT )
9696 rr = client .read_coils (1 , 8 , unit = UNIT )
97- dassert (rq , lambda r : r . function_code < 0x80 ) # test for no error
97+ dassert (rq , lambda r : not r . isError () ) # test for no error
9898 dassert (rr , lambda r : r .bits == [True ]* 8 ) # test the expected value
99-
99+
100100 rq = client .write_coils (1 , [False ]* 8 , unit = UNIT )
101101 rr = client .read_discrete_inputs (1 , 8 , unit = UNIT )
102- dassert (rq , lambda r : r . function_code < 0x80 ) # test for no error
102+ dassert (rq , lambda r : not r . isError () ) # test for no error
103103 dassert (rr , lambda r : r .bits == [True ]* 8 ) # test the expected value
104-
104+
105105 rq = client .write_register (1 , 10 , unit = UNIT )
106106 rr = client .read_holding_registers (1 , 1 , unit = UNIT )
107- dassert (rq , lambda r : r . function_code < 0x80 ) # test for no error
107+ dassert (rq , lambda r : not r . isError () ) # test for no error
108108 dassert (rr , lambda r : r .registers [0 ] == 10 ) # test the expected value
109-
109+
110110 rq = client .write_registers (1 , [10 ]* 8 , unit = UNIT )
111111 rr = client .read_input_registers (1 , 8 , unit = UNIT )
112- dassert (rq , lambda r : r . function_code < 0x80 ) # test for no error
112+ dassert (rq , lambda r : not r . isError () ) # test for no error
113113 dassert (rr , lambda r : r .registers == [17 ]* 8 ) # test the expected value
114-
114+
115115 arguments = {
116116 'read_address' : 1 ,
117117 'read_count' : 8 ,
118118 'write_address' : 1 ,
119119 'write_registers' : [20 ]* 8 ,
120120 }
121- rq = client .readwrite_registers (** arguments , unit = UNIT )
121+ rq = client .readwrite_registers (arguments , unit = UNIT )
122122 rr = client .read_input_registers (1 , 8 , unit = UNIT )
123123 dassert (rq , lambda r : r .registers == [20 ]* 8 ) # test the expected value
124124 dassert (rr , lambda r : r .registers == [17 ]* 8 ) # test the expected value
125125 stopAsynchronousTest (client )
126126
127127
128- # --------------------------------------------------------------------------- #
128+ # --------------------------------------------------------------------------- #
129129# extra requests
130- # --------------------------------------------------------------------------- #
130+ # --------------------------------------------------------------------------- #
131131# If you are performing a request that is not available in the client
132132# mixin, you have to perform the request like this instead::
133133#
@@ -139,11 +139,11 @@ def beginAsynchronousTest(client):
139139# if isinstance(response, ClearCountersResponse):
140140# ... do something with the response
141141#
142- # --------------------------------------------------------------------------- #
142+ # --------------------------------------------------------------------------- #
143143
144- # --------------------------------------------------------------------------- #
144+ # --------------------------------------------------------------------------- #
145145# choose the client you want
146- # --------------------------------------------------------------------------- #
146+ # --------------------------------------------------------------------------- #
147147# make sure to start an implementation to hit against. For this
148148# you can use an existing device, the reference implementation in the tools
149149# directory, or start a pymodbus server.
0 commit comments