DEBUG:pymodbus.server.async:Client Connected [IPv4Address(TCP, '192.168.0.50', 5001)]
DEBUG:pymodbus.server.async:0x0 0x3 0x0 0x0 0x0 0xa 0x1 0x8b 0x7 0x0 0x1 0x1 0xe2 0x2 0x1 0x8
DEBUG:pymodbus.transaction:0x0 0x3 0x0 0x0 0x0 0xa 0x1 0x8b 0x7 0x0 0x1 0x1 0xe2 0x2 0x1 0x8
DEBUG:pymodbus.factory:Factory Request[139]
DEBUG:pymodbus.server.async:send: 000300000003018b01
--
You received this message because you are subscribed to the Google Groups "pymodbus" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pymodbus+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
#!/usr/bin/python
import sys
sys.path.append(r'/home/pi/pysrc')
import pydevd
pydevd.settrace('host') # replace IP with address
# of Eclipse host machine
'''
Pymodbus Server With Updating Thread
--------------------------------------------------------------------------
This is an example of having a background thread updating the
context while the server is operating. This can also be done with
a python thread::
from threading import Thread
thread = Thread(target=updating_writer, args=(context,))
thread.start()
'''
#---------------------------------------------------------------------------#
# import the modbus libraries we need
#---------------------------------------------------------------------------#
from pymodbus.server.async import StartTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer
#---------------------------------------------------------------------------#
# import the twisted libraries we need
#---------------------------------------------------------------------------#
from twisted.internet.task import LoopingCall
#---------------------------------------------------------------------------#
# configure the service logging
#---------------------------------------------------------------------------#
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
#---------------------------------------------------------------------------#
# define your callback process
#---------------------------------------------------------------------------#
def updating_writer(a):
''' A worker process that runs every so often and
updates live values of the context. It should be noted
that there is a race condition for the update.
:param arguments: The input arguments to the call
'''
log.debug("updating the context")
context = a[0]
register = 1
slave_id = 0x01
address = 0x00
values = context[slave_id].getValues(register, address, count=1)
# values = [v + 1 for v in values]
log.debug("new values: " + str(values))
# context[slave_id].setValues(register, address, values)
#---------------------------------------------------------------------------#
# initialize your data store
#---------------------------------------------------------------------------#
store = ModbusSlaveContext(
di = ModbusSequentialDataBlock(0, [0]*0xff),
co = ModbusSequentialDataBlock(0, [0]*0xff),
hr = ModbusSequentialDataBlock(0, [0]*0xff),
ir = ModbusSequentialDataBlock(0, [0]*0xff))
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
#---------------------------------------------------------------------------#
time = 5 # 5 seconds delay
loop = LoopingCall(f=updating_writer, a=(context,))
loop.start(time, now=False) # initially delay by timeOkay,
So you can use the decorator pattern around either the slave context or the datastore. Basically something like this:
class MyContext(object):
def __init__(self, context):
self.context = context # the real slave context
def setValuea(self, f, a, v):
# do your before work here
self.context.setValues(f,a,v)
# do your after work here
slave = MyContext(real_slave)
Does this make sense? Sorry I am on my phone or I would write it for you (if you need a better explanation I will send it to you when I get back to a computer).
Galen