3
Pymodbus Synchronous Client Examples
4
--------------------------------------------------------------------------
6
The following is an example of how to use the synchronous modbus client
7
implementation from pymodbus.
9
It should be noted that the client can also be used with
10
the guard construct that is available in python 2.5 and up::
12
with ModbusClient('127.0.0.1') as client:
13
result = client.read_coils(1,10)
16
#---------------------------------------------------------------------------#
17
# import the various server implementations
18
#---------------------------------------------------------------------------#
19
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
20
#from pymodbus.client.sync import ModbusUdpClient as ModbusClient
21
#from pymodbus.client.sync import ModbusSerialClient as ModbusClient
23
#---------------------------------------------------------------------------#
24
# configure the client logging
25
#---------------------------------------------------------------------------#
28
log = logging.getLogger()
29
log.setLevel(logging.DEBUG)
31
#---------------------------------------------------------------------------#
32
# choose the client you want
33
#---------------------------------------------------------------------------#
34
# make sure to start an implementation to hit against. For this
35
# you can use an existing device, the reference implementation in the tools
36
# directory, or start a pymodbus server.
37
#---------------------------------------------------------------------------#
38
client = ModbusClient('127.0.0.1')
40
#---------------------------------------------------------------------------#
42
#---------------------------------------------------------------------------#
43
# simply call the methods that you would like to use. An example session
44
# is displayed below along with some assert checks. Note that some modbus
45
# implementations differentiate holding/input discrete/coils and as such
46
# you will not be able to write to these, therefore the starting values
47
# are not known to these tests. Furthermore, some use the same memory
48
# blocks for the two sets, so a change to one is a change to the other.
49
# Keep both of these cases in mind when testing as the following will
50
# _only_ pass with the supplied async modbus server (script supplied).
51
#---------------------------------------------------------------------------#
52
rq = client.write_coil(1, True)
53
rr = client.read_coils(1,1)
54
assert(rq.function_code < 0x80) # test that we are not an error
55
assert(rr.bits[0] == True) # test the expected value
57
rq = client.write_coils(1, [True]*8)
58
rr = client.read_coils(1,8)
59
assert(rq.function_code < 0x80) # test that we are not an error
60
assert(rr.bits == [True]*8) # test the expected value
62
rq = client.write_coils(1, [False]*8)
63
rr = client.read_discrete_inputs(1,8)
64
assert(rq.function_code < 0x80) # test that we are not an error
65
assert(rr.bits == [True]*8) # test the expected value
67
rq = client.write_register(1, 10)
68
rr = client.read_holding_registers(1,1)
69
assert(rq.function_code < 0x80) # test that we are not an error
70
assert(rr.registers[0] == 10) # test the expected value
72
rq = client.write_registers(1, [10]*8)
73
rr = client.read_input_registers(1,8)
74
assert(rq.function_code < 0x80) # test that we are not an error
75
assert(rr.registers == [17]*8) # test the expected value
81
'write_registers': [20]*8,
83
rq = client.readwrite_registers(**arguments)
84
rr = client.read_input_registers(1,8)
85
assert(rq.function_code < 0x80) # test that we are not an error
86
assert(rq.registers == [20]*8) # test the expected value
87
assert(rr.registers == [17]*8) # test the expected value
89
#---------------------------------------------------------------------------#
91
#---------------------------------------------------------------------------#