Skip to content

Commit

Permalink
adding the ability to override sync client framer
Browse files Browse the repository at this point in the history
  • Loading branch information
bashwork committed Dec 16, 2012
1 parent 766109b commit bdbcc51
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
7 changes: 6 additions & 1 deletion examples/common/synchronous-client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 35,12 @@
# you can use an existing device, the reference implementation in the tools
# directory, or start a pymodbus server.
#
# If you use the UDP or TCP clients, you can override the framer being used
# to use a custom implementation (say RTU over TCP). By default they use the
# socket framer::
#
# client = ModbusClient('localhost', port=5020, framer=ModbusRtuFramer)
#
# It should be noted that you can supply an ipv4 or an ipv6 host address for
# both the UDP and TCP clients.
#---------------------------------------------------------------------------#
Expand All @@ -55,7 61,6 @@
# Keep both of these cases in mind when testing as the following will
# _only_ pass with the supplied async modbus server (script supplied).
#---------------------------------------------------------------------------#
#import pdb;pdb.set_trace()
rq = client.write_coil(1, True)
rr = client.read_coils(1,1)
assert(rq.function_code < 0x80) # test that we are not an error
Expand Down
1 change: 1 addition & 0 deletions pymodbus/client/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 45,7 @@ def process():
import logging
_logger = logging.getLogger(__name__)


#---------------------------------------------------------------------------#
# A manager for the transaction identifiers
#---------------------------------------------------------------------------#
Expand Down
11 changes: 7 additions & 4 deletions pymodbus/client/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 115,19 @@ class ModbusTcpClient(BaseModbusClient):
''' Implementation of a modbus tcp client
'''

def __init__(self, host='127.0.0.1', port=Defaults.Port):
def __init__(self, host='127.0.0.1', port=Defaults.Port, framer=ModbusSocketFramer):
''' Initialize a client instance
:param host: The host to connect to (default 127.0.0.1)
:param port: The modbus port to connect to (default 502)
:param framer: The modbus framer to use (default ModbusSocketFramer)
.. note:: The host argument will accept ipv4 and ipv6 hosts
'''
self.host = host
self.port = port
self.socket = None
BaseModbusClient.__init__(self, ModbusSocketFramer(ClientDecoder()))
BaseModbusClient.__init__(self, framer(ClientDecoder()))

def connect(self):
''' Connect to the modbus tcp server
Expand Down Expand Up @@ -187,16 188,17 @@ class ModbusUdpClient(BaseModbusClient):
''' Implementation of a modbus udp client
'''

def __init__(self, host='127.0.0.1', port=Defaults.Port):
def __init__(self, host='127.0.0.1', port=Defaults.Port, framer=ModbusSocketFramer):
''' Initialize a client instance
:param host: The host to connect to (default 127.0.0.1)
:param port: The modbus port to connect to (default 502)
:param framer: The modbus framer to use (default ModbusSocketFramer)
'''
self.host = host
self.port = port
self.socket = None
BaseModbusClient.__init__(self, ModbusSocketFramer(ClientDecoder()))
BaseModbusClient.__init__(self, framer(ClientDecoder()))

@classmethod
def _get_address_family(cls, address):
Expand Down Expand Up @@ -307,6 309,7 @@ def __implementation(method):
if method == 'ascii': return ModbusAsciiFramer(ClientDecoder())
elif method == 'rtu': return ModbusRtuFramer(ClientDecoder())
elif method == 'binary': return ModbusBinaryFramer(ClientDecoder())
elif method == 'socket': return ModbusSocketFramer(ClientDecoder())
raise ParameterException("Invalid framer method requested")

def connect(self):
Expand Down

0 comments on commit bdbcc51

Please sign in to comment.