Modbus TCP Test via Node-RED & Pymodbus

Posted on Posted in Uncategorized
  1. Run Pymodbus server with the following python script (asynchronous):
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


#---------------------------------------------------------------------------# 
# 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]
    functioncode = 3 
    slave_id = 0x01 # slave address
    address  = 0x10 # start register : 400017


#---------------------------------------------------------------------------# 
# initialize your data store
#---------------------------------------------------------------------------# 
store = ModbusSlaveContext(
    di = ModbusSequentialDataBlock(0, [17]*100),
    co = ModbusSequentialDataBlock(0, [17]*100),
    hr = ModbusSequentialDataBlock(0, [17]*100),
    ir = ModbusSequentialDataBlock(0, [17]*100))
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'

StartTcpServer(context, identity=identity, address=("127.0.0.1", 10502))

2. Start node red with the following flow:

[{"id":"f89dcd7e.71883","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"26fa2798.a2cf18","type":"modbus-flex-getter","z":"f89dcd7e.71883","name":"modbus_get","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"server":"41e4f697.aa6ba","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":981,"y":335,"wires":[["adefb720.1f8988","41a5de89.054d9"],[]]},{"id":"adefb720.1f8988","type":"modbus-response","z":"f89dcd7e.71883","name":"","registerShowMax":20,"x":1193,"y":265,"wires":[]},{"id":"ac2ace4c.2d433","type":"function","z":"f89dcd7e.71883","name":"FC3 6 [1]","func":"msg.payload = { \n   'fc': 3, \n   'unitid': 1, \n   'address': 6 , \n   'quantity': 1}; \nreturn msg;","outputs":1,"noerr":0,"x":811,"y":335,"wires":[["26fa2798.a2cf18"]]},{"id":"52efd938.cd344","type":"modbus-flex-write","z":"f89dcd7e.71883","name":"modbus_write","showStatusActivities":false,"showErrors":false,"server":"41e4f697.aa6ba","x":877,"y":248,"wires":[["adefb720.1f8988","ac2ace4c.2d433"],[]]},{"id":"196f96ea.f7c481","type":"function","z":"f89dcd7e.71883","name":"FC6 6 [1]","func":"msg.payload = { \n    value: msg.payload,\n   'fc': 6, \n   'unitid': 1, \n   'address': 6 , \n   'quantity': 1}; \nreturn msg;","outputs":1,"noerr":0,"x":683,"y":247,"wires":[["52efd938.cd344"]]},{"id":"11109147.666ebf","type":"debug","z":"f89dcd7e.71883","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1001,"y":416,"wires":[]},{"id":"754157be.20f218","type":"function","z":"f89dcd7e.71883","name":"Compare","func":"var sent = flow.get(\"sent\");\nvar received = flow.get(\"received\");\n\nif(received==sent)\nmsg.payload = \"Input equals output\";\nelse\nmsg.payload = \"Input doesn't match output\"\nreturn msg;","outputs":1,"noerr":0,"x":823,"y":415,"wires":[["11109147.666ebf"]]},{"id":"550aab8d.983f44","type":"function","z":"f89dcd7e.71883","name":"Save output","func":"//global.set(\"sent\", msg.payload.toString());\nflow.set(\"sent\", msg.payload);","outputs":1,"noerr":0,"x":705,"y":197,"wires":[[]]},{"id":"41a5de89.054d9","type":"function","z":"f89dcd7e.71883","name":"Save input","func":"flow.set(\"received\", msg.payload);\n//var date = new Date(msg.payload);\n//msg.payload = date.toString();\nvar msg={};\nreturn msg;","outputs":1,"noerr":0,"x":1163,"y":329,"wires":[["754157be.20f218"]]},{"id":"d2027b09.9ca338","type":"trigger","z":"f89dcd7e.71883","op1":"","op2":"","op1type":"str","op2type":"str","duration":"-1000","extend":false,"units":"ms","reset":"1","bytopic":"all","name":"","x":327,"y":251,"wires":[["a2a44616.c972f8"]]},{"id":"92bbeb94.76a518","type":"inject","z":"f89dcd7e.71883","name":"Start","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":113,"y":217,"wires":[["d2027b09.9ca338"]]},{"id":"56794f2d.adeb6","type":"inject","z":"f89dcd7e.71883","name":"Stop","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":111,"y":263,"wires":[["d2027b09.9ca338"]]},{"id":"a2a44616.c972f8","type":"random","z":"f89dcd7e.71883","name":"","low":"1","high":"10","inte":"true","property":"payload","x":509,"y":245,"wires":[["196f96ea.f7c481","550aab8d.983f44"]]},{"id":"41e4f697.aa6ba","type":"modbus-client","z":"","name":"Modbus-Server","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"10502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"}]

3. Configure polling time as desired (trigger time)

Leave a Reply

Your email address will not be published. Required fields are marked *