~ubuntu-branches/ubuntu/precise/pymodbus/precise

« back to all changes in this revision

Viewing changes to examples/common/asynchronous-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 Asynchronous Client Examples
 
4
--------------------------------------------------------------------------
 
5
 
 
6
The following is an example of how to use the asynchronous modbus
 
7
client implementation from pymodbus.
 
8
'''
 
9
#---------------------------------------------------------------------------# 
 
10
# import needed libraries
 
11
#---------------------------------------------------------------------------# 
 
12
from twisted.internet import reactor, protocol
 
13
from pymodbus.constants import Defaults
 
14
 
 
15
#---------------------------------------------------------------------------# 
 
16
# choose the requested modbus protocol
 
17
#---------------------------------------------------------------------------# 
 
18
from pymodbus.client.async import ModbusClientProtocol
 
19
#from pymodbus.client.async import ModbusUdpClientProtocol
 
20
 
 
21
#---------------------------------------------------------------------------# 
 
22
# configure the client logging
 
23
#---------------------------------------------------------------------------# 
 
24
import logging
 
25
logging.basicConfig()
 
26
log = logging.getLogger()
 
27
log.setLevel(logging.DEBUG)
 
28
 
 
29
#---------------------------------------------------------------------------# 
 
30
# helper method to test deferred callbacks
 
31
#---------------------------------------------------------------------------# 
 
32
def dassert(deferred, callback):
 
33
    def _assertor(value): assert(value)
 
34
    deferred.addCallback(lambda r: _assertor(callback(r)))
 
35
    deferred.addErrback(lambda  _: _assertor(False))
 
36
 
 
37
#---------------------------------------------------------------------------# 
 
38
# example requests
 
39
#---------------------------------------------------------------------------# 
 
40
# simply call the methods that you would like to use. An example session
 
41
# is displayed below along with some assert checks. Note that unlike the
 
42
# synchronous version of the client, the asynchronous version returns
 
43
# deferreds which can be thought of as a handle to the callback to send
 
44
# the result of the operation.  We are handling the result using the
 
45
# deferred assert helper(dassert).
 
46
#---------------------------------------------------------------------------# 
 
47
def beginAsynchronousTest(client):
 
48
    rq = client.write_coil(1, True)
 
49
    rr = client.read_coils(1,1)
 
50
    dassert(rq, lambda r: r.function_code < 0x80)     # test that we are not an error
 
51
    dassert(rr, lambda r: r.bits[0] == True)          # test the expected value
 
52
    
 
53
    rq = client.write_coils(1, [True]*8)
 
54
    rr = client.read_coils(1,8)
 
55
    dassert(rq, lambda r: r.function_code < 0x80)     # test that we are not an error
 
56
    dassert(rr, lambda r: r.bits == [True]*8)         # test the expected value
 
57
    
 
58
    rq = client.write_coils(1, [False]*8)
 
59
    rr = client.read_discrete_inputs(1,8)
 
60
    dassert(rq, lambda r: r.function_code < 0x80)     # test that we are not an error
 
61
    dassert(rr, lambda r: r.bits == [True]*8)         # test the expected value
 
62
    
 
63
    rq = client.write_register(1, 10)
 
64
    rr = client.read_holding_registers(1,1)
 
65
    dassert(rq, lambda r: r.function_code < 0x80)     # test that we are not an error
 
66
    dassert(rr, lambda r: r.registers[0] == 10)       # test the expected value
 
67
    
 
68
    rq = client.write_registers(1, [10]*8)
 
69
    rr = client.read_input_registers(1,8)
 
70
    dassert(rq, lambda r: r.function_code < 0x80)     # test that we are not an error
 
71
    dassert(rr, lambda r: r.registers == [17]*8)      # test the expected value
 
72
    
 
73
    arguments = {
 
74
        'read_address':    1,
 
75
        'read_count':      8,
 
76
        'write_address':   1,
 
77
        'write_registers': [20]*8,
 
78
    }
 
79
    rq = client.readwrite_registers(**arguments)
 
80
    rr = client.read_input_registers(1,8)
 
81
    dassert(rq, lambda r: r.registers == [20]*8)      # test the expected value
 
82
    dassert(rr, lambda r: r.registers == [17]*8)      # test the expected value
 
83
 
 
84
    #-----------------------------------------------------------------------# 
 
85
    # close the client at some time later
 
86
    #-----------------------------------------------------------------------# 
 
87
    reactor.callLater(1, client.transport.loseConnection)
 
88
    reactor.callLater(2, reactor.stop)
 
89
 
 
90
#---------------------------------------------------------------------------# 
 
91
# choose the client you want
 
92
#---------------------------------------------------------------------------# 
 
93
# make sure to start an implementation to hit against. For this
 
94
# you can use an existing device, the reference implementation in the tools
 
95
# directory, or start a pymodbus server.
 
96
#---------------------------------------------------------------------------# 
 
97
defer = protocol.ClientCreator(reactor, ModbusClientProtocol
 
98
        ).connectTCP("localhost", Defaults.Port)
 
99
defer.addCallback(beginAsynchronousTest)
 
100
reactor.run()