Skip to content

Commit 693783b

Browse files
rahulraghu94dhoomakethu
authored andcommitted
#82 error response api (#262)
* easily to check for errors in stead of having to manually compare response codes
1 parent d990a13 commit 693783b

11 files changed

+168
-165
lines changed

examples/common/asynchronous_client.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@
66
The following is an example of how to use the asynchronous modbus
77
client implementation from pymodbus.
88
"""
9-
# --------------------------------------------------------------------------- #
9+
# --------------------------------------------------------------------------- #
1010
# import needed libraries
11-
# --------------------------------------------------------------------------- #
11+
# --------------------------------------------------------------------------- #
1212
from twisted.internet import reactor, protocol
1313
from pymodbus.constants import Defaults
1414

15-
# --------------------------------------------------------------------------- #
15+
# --------------------------------------------------------------------------- #
1616
# choose the requested modbus protocol
17-
# --------------------------------------------------------------------------- #
17+
# --------------------------------------------------------------------------- #
1818
from pymodbus.client.async import ModbusClientProtocol
1919
from pymodbus.client.async import ModbusUdpClientProtocol
2020
from pymodbus.framer.rtu_framer import ModbusRtuFramer
2121

22-
# --------------------------------------------------------------------------- #
22+
# --------------------------------------------------------------------------- #
2323
# configure the client logging
24-
# --------------------------------------------------------------------------- #
24+
# --------------------------------------------------------------------------- #
2525
import logging
2626
FORMAT = ('%(asctime)-15s %(threadName)-15s'
2727
' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
2828
logging.basicConfig(format=FORMAT)
2929
log = logging.getLogger()
3030
log.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):
8989
def 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.

examples/common/changing_framers.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@
1414
tcp transport with an RTU framer). However, please let us know of any
1515
success cases that are not documented!
1616
"""
17-
# --------------------------------------------------------------------------- #
17+
# --------------------------------------------------------------------------- #
1818
# import the modbus client and the framers
19-
# --------------------------------------------------------------------------- #
19+
# --------------------------------------------------------------------------- #
2020
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
2121

22-
# --------------------------------------------------------------------------- #
22+
# --------------------------------------------------------------------------- #
2323
# Import the modbus framer that you want
24-
# --------------------------------------------------------------------------- #
25-
# --------------------------------------------------------------------------- #
26-
#from pymodbus.transaction import ModbusSocketFramer as ModbusFramer
27-
from pymodbus.transaction import ModbusRtuFramer as ModbusFramer
24+
# --------------------------------------------------------------------------- #
25+
# --------------------------------------------------------------------------- #
26+
from pymodbus.transaction import ModbusSocketFramer as ModbusFramer
27+
# from pymodbus.transaction import ModbusRtuFramer as ModbusFramer
2828
#from pymodbus.transaction import ModbusBinaryFramer as ModbusFramer
2929
#from pymodbus.transaction import ModbusAsciiFramer as ModbusFramer
3030

31-
# --------------------------------------------------------------------------- #
31+
# --------------------------------------------------------------------------- #
3232
# configure the client logging
33-
# --------------------------------------------------------------------------- #
33+
# --------------------------------------------------------------------------- #
3434
import logging
3535
logging.basicConfig()
3636
log = logging.getLogger()
@@ -48,7 +48,7 @@
4848
# ----------------------------------------------------------------------- #
4949
rq = client.write_coil(1, True)
5050
rr = client.read_coils(1,1)
51-
assert(rq.function_code < 0x80) # test that we are not an error
51+
assert(not rq.isError()) # test that we are not an error
5252
assert(rr.bits[0] == True) # test the expected value
5353

5454
# ----------------------------------------------------------------------- #

examples/common/modbus_payload.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
2222
logging.basicConfig(format=FORMAT)
2323
log = logging.getLogger()
24-
log.setLevel(logging.DEBUG)
24+
log.setLevel(logging.INFO)
2525

2626

2727
def run_binary_payload_ex():

examples/common/synchronous_client.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
result = client.read_coils(1,10)
1414
print result
1515
"""
16-
# --------------------------------------------------------------------------- #
16+
# --------------------------------------------------------------------------- #
1717
# import the various server implementations
18-
# --------------------------------------------------------------------------- #
18+
# --------------------------------------------------------------------------- #
1919
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
2020
# from pymodbus.client.sync import ModbusUdpClient as ModbusClient
2121
# from pymodbus.client.sync import ModbusSerialClient as ModbusClient
2222

23-
# --------------------------------------------------------------------------- #
23+
# --------------------------------------------------------------------------- #
2424
# configure the client logging
25-
# --------------------------------------------------------------------------- #
25+
# --------------------------------------------------------------------------- #
2626
import logging
2727
FORMAT = ('%(asctime)-15s %(threadName)-15s '
2828
'%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
@@ -34,9 +34,9 @@
3434

3535

3636
def run_sync_client():
37-
# ------------------------------------------------------------------------#
37+
# ------------------------------------------------------------------------#
3838
# choose the client you want
39-
# ------------------------------------------------------------------------#
39+
# ------------------------------------------------------------------------#
4040
# make sure to start an implementation to hit against. For this
4141
# you can use an existing device, the reference implementation in the tools
4242
# directory, or start a pymodbus server.
@@ -60,7 +60,7 @@ def run_sync_client():
6060
# Here is an example of using these options::
6161
#
6262
# client = ModbusClient('localhost', retries=3, retry_on_empty=True)
63-
# ------------------------------------------------------------------------#
63+
# ------------------------------------------------------------------------#
6464
client = ModbusClient('localhost', port=5020)
6565
# from pymodbus.transaction import ModbusRtuFramer
6666
# client = ModbusClient('localhost', port=5020, framer=ModbusRtuFramer)
@@ -69,10 +69,10 @@ def run_sync_client():
6969
# client = ModbusClient(method='rtu', port='/dev/ptyp0', timeout=1,
7070
# baudrate=9600)
7171
client.connect()
72-
73-
# ------------------------------------------------------------------------#
72+
73+
# ------------------------------------------------------------------------#
7474
# specify slave to query
75-
# ------------------------------------------------------------------------#
75+
# ------------------------------------------------------------------------#
7676
# The slave to query is specified in an optional parameter for each
7777
# individual request. This can be done by specifying the `unit` parameter
7878
# which defaults to `0x00`
@@ -97,49 +97,49 @@ def run_sync_client():
9797
log.debug("Write to a Coil and read back")
9898
rq = client.write_coil(0, True, unit=UNIT)
9999
rr = client.read_coils(0, 1, unit=UNIT)
100-
assert(rq.function_code < 0x80) # test that we are not an error
100+
assert(not rq.isError()) # test that we are not an error
101101
assert(rr.bits[0] == True) # test the expected value
102-
102+
103103
log.debug("Write to multiple coils and read back- test 1")
104104
rq = client.write_coils(1, [True]*8, unit=UNIT)
105-
assert(rq.function_code < 0x80) # test that we are not an error
105+
assert(not rq.isError()) # test that we are not an error
106106
rr = client.read_coils(1, 21, unit=UNIT)
107-
assert(rr.function_code < 0x80) # test that we are not an error
107+
assert(not rr.isError()) # test that we are not an error
108108
resp = [True]*21
109-
109+
110110
# If the returned output quantity is not a multiple of eight,
111111
# the remaining bits in the final data byte will be padded with zeros
112112
# (toward the high order end of the byte).
113-
113+
114114
resp.extend([False]*3)
115115
assert(rr.bits == resp) # test the expected value
116-
116+
117117
log.debug("Write to multiple coils and read back - test 2")
118118
rq = client.write_coils(1, [False]*8, unit=UNIT)
119119
rr = client.read_coils(1, 8, unit=UNIT)
120-
assert(rq.function_code < 0x80) # test that we are not an error
120+
assert(not rq.isError()) # test that we are not an error
121121
assert(rr.bits == [False]*8) # test the expected value
122-
122+
123123
log.debug("Read discrete inputs")
124124
rr = client.read_discrete_inputs(0, 8, unit=UNIT)
125-
assert(rq.function_code < 0x80) # test that we are not an error
126-
125+
assert(not rq.isError()) # test that we are not an error
126+
127127
log.debug("Write to a holding register and read back")
128128
rq = client.write_register(1, 10, unit=UNIT)
129129
rr = client.read_holding_registers(1, 1, unit=UNIT)
130-
assert(rq.function_code < 0x80) # test that we are not an error
130+
assert(not rq.isError()) # test that we are not an error
131131
assert(rr.registers[0] == 10) # test the expected value
132-
132+
133133
log.debug("Write to multiple holding registers and read back")
134134
rq = client.write_registers(1, [10]*8, unit=UNIT)
135135
rr = client.read_holding_registers(1, 8, unit=UNIT)
136-
assert(rq.function_code < 0x80) # test that we are not an error
136+
assert(not rq.isError()) # test that we are not an error
137137
assert(rr.registers == [10]*8) # test the expected value
138-
138+
139139
log.debug("Read input registers")
140140
rr = client.read_input_registers(1, 8, unit=UNIT)
141-
assert(rq.function_code < 0x80) # test that we are not an error
142-
141+
assert(not rq.isError()) # test that we are not an error
142+
143143
arguments = {
144144
'read_address': 1,
145145
'read_count': 8,
@@ -149,10 +149,10 @@ def run_sync_client():
149149
log.debug("Read write registeres simulataneously")
150150
rq = client.readwrite_registers(unit=UNIT, **arguments)
151151
rr = client.read_holding_registers(1, 8, unit=UNIT)
152-
assert(rq.function_code < 0x80) # test that we are not an error
152+
assert(not rq.isError()) # test that we are not an error
153153
assert(rq.registers == [20]*8) # test the expected value
154154
assert(rr.registers == [20]*8) # test the expected value
155-
155+
156156
# ----------------------------------------------------------------------- #
157157
# close the client
158158
# ----------------------------------------------------------------------- #

0 commit comments

Comments
 (0)