Dynamic Modbus Flex

Modbus has some nodes called Flex, which means they handles values dynamically for read or write. Not sure why they call flex read for 'getter', but write is still write (not getter).

However, the client connection is still hardcoded in the node? Any way to set this dynamically also? Having used other connections like http, mqtt etc, they all support dynamic connection. Why not modbus?

Because when connection is hardcoded, we end up duplicating a lot of code per client, instead of setting it dynamically in msg variables.

Any experience or tips to make this cleaner?

Yeah I would love to wrap all that in a subflow for reusability, but if connection can't be set in msg, there's no way to get a flexible solution.

A subflow can utilise env vars.

e.g.

  • add a property to the subflow HOST and PORT
  • set up the modbus connection config to use ${HOST} and ${PORT}
    • You will also need to ensure the connection config belongs to the subflow
  • add a subflow instance and populate the new HOST and PORT fields
  • deploy

While not runtime dynamic, it will apply the value set in the subflow instance to the item in the subflow.

setup subflow template

setup modbus config inside the subflow

Add a subflow instance & set the address/port!

image

1 Like

NOTE: Each instance of the subflow will be a NEW connection (since the config belongs to the subflow template)

You should avoid adding multiple instances that connect to the same modbus device as it will create multiple connections. Instead, put logic in your subflow that reads/writes/whatever to the same config inside the subflow. you can do this using a pattern of your design (e.g. pass messages containing details like { payload: {values: [1,2,3], command: "write" }} or some other format)

Thanks a lot! Will try with env vars.

I want to have different connections to different modbuses, similar to how MQTT or HTTP connections work.

And you can. I am just warning you if you add 2nd and 3rd instance of the subflow AND point them all to the same IP address, then multiple connections will be made. Just be aware.

The solution is to encapsulate everything to do with modbus in the single instance of the subflow.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.