Skip to content

Commit

Permalink
Adding an example and adding tcp timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Galen Collins committed Jan 5, 2015
1 parent eb51b4d commit b13c89d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
6 changes: 6 additions & 0 deletions doc/sphinx/examples/custom-datablock.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
==================================================
Custom Datablock Example
==================================================

.. literalinclude:: ../../../examples/common/custom-datablock.py

77 changes: 77 additions & 0 deletions examples/common/custom-datablock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python
'''
Pymodbus Server With Custom Datablock Side Effect
--------------------------------------------------------------------------
This is an example of performing custom logic after a value has been
written to the datastore.
'''
#---------------------------------------------------------------------------#
# import the modbus libraries we need
#---------------------------------------------------------------------------#
from pymodbus.server.async import StartTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSparseDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer

#---------------------------------------------------------------------------#
# configure the service logging
#---------------------------------------------------------------------------#

import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

#---------------------------------------------------------------------------#
# create your custom data block here
#---------------------------------------------------------------------------#

class CustomDataBlock(ModbusSparseDataBlock):
''' A datablock that stores the new value in memory
and performs a custom action after it has been stored.
'''

def setValues(self, address, value):
''' Sets the requested values of the datastore
:param address: The starting address
:param values: The new values to be set
'''
super(ModbusSparseDataBlock, self).setValues(address, value)

# whatever you want to do with the written value is done here,
# however make sure not to do too much work here or it will
# block the server, espectially if the server is being written
# to very quickly
print "wrote {} to {}".format(value, address)


#---------------------------------------------------------------------------#
# initialize your data store
#---------------------------------------------------------------------------#

block = CustomDataBlock()
store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block)
context = ModbusServerContext(slaves=store, single=True)

#---------------------------------------------------------------------------#
# initialize the server information
#---------------------------------------------------------------------------#

identity = ModbusDeviceIdentification()
identity.VendorName = 'pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'pymodbus Server'
identity.ModelName = 'pymodbus Server'
identity.MajorMinorRevision = '1.0'

#---------------------------------------------------------------------------#
# run the server you want
#---------------------------------------------------------------------------#

p = Process(target=device_writer, args=(queue,))
p.start()
StartTcpServer(context, identity=identity, address=("localhost", 5020))
9 changes: 6 additions & 3 deletions pymodbus/client/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,12 @@ def __init__(self, host='127.0.0.1', port=Defaults.Port,
: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)
:param timeout: The timeout to use for this socket (default None)
'''
self.host = host
self.port = port
self.socket = None
self.host = host
self.port = port
self.socket = None
self.timeout = kwargs.get('timeout', None)
BaseModbusClient.__init__(self, framer(ClientDecoder()), **kwargs)

@classmethod
Expand All @@ -226,6 +228,7 @@ def connect(self):
try:
family = ModbusUdpClient._get_address_family(self.host)
self.socket = socket.socket(family, socket.SOCK_DGRAM)
self.settimeout(self.timeout)
except socket.error, ex:
_logger.error('Unable to create udp socket %s' % ex)
self.close()
Expand Down

0 comments on commit b13c89d

Please sign in to comment.