1
votes

I'm trying to use node-red to connect to a master using Modbus and simulate slave devices, sending data to the master when it asks. I have tried looking everywhere for hints on how to get started and the only examples using the node-red-contrib-modbus module seem to be simulating the slave and the master or just the master. I know the module comes with an example labeled modbus slave, but I'm confused whether the modbus flex server acts as a slave. Originally I thought it did, however as the pi is connected to the gateway via ethernet colleagues have told me I shouldn't need to enter the IP address etc of the host as only the host needs the IP address of the slave not the other way round. I'm not sure whether to use the modbus write node or not.

1
Modbus flex server does act as a slave and works fine for testing. Is the field you are referring to as the "IP address" labelled "Bind Address"? (if so you can just leave this set to 0.0.0.0; this is used when the machine has multiple IP addresses and you only want modbus to listen on one).Brits
Great thanks, would the master just connect to the slave via the IP address of the slave then. Also, would you need the 'Modbus write' node to write the values to the server or would you just use a function that has a msg.payload containing the value, register, address etc you want to send? Finally, how are you meant to detect the request sent by the master, so that the slave only responds when it's asked to. Is this built into the Modbus server, or are other nodes needed?MattieG4

1 Answers

0
votes

Expanding upon my comment because it appears that this may answer your question. As the comment says the IP address field in the Modbus-Flex-Server is the 'Bind Address' (screenshot follows) this controls which IP address(s) the Modbus slave will listen on. Leaving this set to 0.0.0.0 means it will listen on all IP addresses setup on the machine.

Screenshot

The best way to understand how to use Modbus-Flex-Server is to take a look at the demo flow. To access this select 'Import' from the main node-red menu and select Examples->nod-red-contrib-modbus->Modbus-Slave:

Import Demo screenshot

The imported flow demonstrates a fairly simple way to set the registers (note that you could also do this via a Modbus write). The payload looks something like:

msg.payload = { 
    'value': 1, 
    'register': 'coils', 
    'address': 0 ,
    'disablemsg' : 1
} ;
return msg

would the master just connect to the slave via the IP address of the slave then

Correct. If they are on the same box just use 127.0.0.1; if on different boxes you may also need to add firewall rules.

Finally, how are you meant to detect the request sent by the master, so that the slave only responds when it's asked to

Not quite sure what you mean by this - the slave will only respond to requests that it receives. You can check the values of the modbus registers using the 'inject' topic which will cause the slave node to send its registers to the appropriate outputs (see the demo). Alternatively you can add custom code that will be run when a modbus command is received in the Modbus-Flex-Server properties.