~ubuntu-branches/ubuntu/raring/pymodbus/raring-proposed

« back to all changes in this revision

Viewing changes to examples/common/synchronous-client.py

  • Committer: Package Import Robot
  • Author(s): W. Martin Borgert
  • Date: 2011-10-26 07:26:28 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20111026072628-fvzyi6tnb8iipomp
Tags: 0.9.0+r175-1
* Update from trunk to get a number of upstream fixes.
* Removed examples/tools/ (not present in previous version
  anyway) from source because there are different licenses
  involved. Needs clarification.
* Dont't install unit tests.
* Debian patches not necessary anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
'''
 
3
Pymodbus Synchronous Client Examples
 
4
--------------------------------------------------------------------------
 
5
 
 
6
The following is an example of how to use the synchronous modbus client
 
7
implementation from pymodbus.
 
8
 
 
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::
 
11
 
 
12
    with ModbusClient('127.0.0.1') as client:
 
13
        result = client.read_coils(1,10)
 
14
        print result
 
15
'''
 
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
 
22
 
 
23
#---------------------------------------------------------------------------# 
 
24
# configure the client logging
 
25
#---------------------------------------------------------------------------# 
 
26
import logging
 
27
logging.basicConfig()
 
28
log = logging.getLogger()
 
29
log.setLevel(logging.DEBUG)
 
30
 
 
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')
 
39
 
 
40
#---------------------------------------------------------------------------# 
 
41
# example requests
 
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
 
56
 
 
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
 
61
 
 
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
 
66
 
 
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
 
71
 
 
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
 
76
 
 
77
arguments = {
 
78
    'read_address':    1,
 
79
    'read_count':      8,
 
80
    'write_address':   1,
 
81
    'write_registers': [20]*8,
 
82
}
 
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
 
88
 
 
89
#---------------------------------------------------------------------------# 
 
90
# close the client
 
91
#---------------------------------------------------------------------------# 
 
92
client.close()