Commit 40fb628f authored by Yoann Sallaz Damaz's avatar Yoann Sallaz Damaz
Browse files

add wagosimu

parent dcfbd350
#!/usr/bin/env python
"""
Pymodbus Server With Callbacks
--------------------------------------------------------------------------
This is an example of adding callbacks to a running modbus server
when a value is written to it. In order for this to work, it needs
a device-mapping file.
"""
# --------------------------------------------------------------------------- #
# import the modbus libraries we need
# --------------------------------------------------------------------------- #
from pymodbus.server.asynchronous import StartTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSparseDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer
# --------------------------------------------------------------------------- #
# import the python libraries we need
# --------------------------------------------------------------------------- #
from multiprocessing import Queue, Process
# --------------------------------------------------------------------------- #
# configure the service logging
# --------------------------------------------------------------------------- #
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
# --------------------------------------------------------------------------- #
# create your custom data block with callbacks
# --------------------------------------------------------------------------- #
class CallbackDataBlock(ModbusSparseDataBlock):
""" A datablock that stores the new value in memory
and passes the operation to a message queue for further
processing.
"""
def __init__(self, devices, queue):
"""
"""
self.devices = devices
self.queue = queue
values = {k: 0 for k in devices.keys()}
values[0xbeef] = len(values) # the number of devices
values[0x2012+1] = 842 #750-842
super(CallbackDataBlock, self).__init__(values)
print("ici------------------------------")
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(CallbackDataBlock, self).setValues(address, value)
super(CallbackDataBlock, self).setValues(address+512, value)
print(address)
print(value)
if address==7:
if value[0]:
print("fin de course in")
super(CallbackDataBlock, self).setValues(2, False)
super(CallbackDataBlock, self).setValues(1, True)
else:
print("fin de course out")
super(CallbackDataBlock, self).setValues(2, True)
super(CallbackDataBlock, self).setValues(1, False)
self.queue.put((self.devices.get(address, None), value))
# --------------------------------------------------------------------------- #
# initialize your device map
# --------------------------------------------------------------------------- #
def run_callback_server():
# ----------------------------------------------------------------------- #
# initialize your data store
# ----------------------------------------------------------------------- #
queue = Queue()
devices={}
for i in range(0,10000):
devices.update({i:i})
block1 = CallbackDataBlock(devices, queue)
block2 = CallbackDataBlock(devices, queue)
store = ModbusSlaveContext(di=block1, co=block1, hr=block2, ir=block2)
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 = '2.3.0'
# ----------------------------------------------------------------------- #
# run the server you want
# ----------------------------------------------------------------------- #
StartTcpServer(context, identity=identity, address=("localhost", 5020))
if __name__ == "__main__":
run_callback_server()
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment