MODBUS application

Has anyone have an example of a MODBUS application collecting PLC data points and funneling through LOSANT MQTT. Appreciate sharing

Hi @Sreyams_Jain

Yes, I have an example for this. Here is the example code:

Because you asked, I can write a blog post on this topic. But, for now, I can illuminate a bunch of things.

I wanted to create a dashboard that allowed me to control the coils ( or outputs ) of the PLC. Like so:

First I created a device with a bunch of Boolean attributes that represents all the coils ( I called mine outputs ). Like so:

Then, using a gateway and the example code on Github, I use a Losant Device Command to send a message to the PLC. The flow looked like this:

This blog post talks about the process:

The example code is also reading discrete inputs every second and sending that data to Losant.

I can also elaborate more if you need me to. If you have any things specific you want to see, please let me know. I’d be happy to help. Like I said, I’ll create a tutorial like blog post about this as well.

HI

You scenario is 90% of what we do with regards to acquisition on Losant.
We have a python agent, that polls COMAP equipment.

We decoupled the components all running on a single device.

python agents (possibly multiple) (on losant gateway) polls modbus slaves (TCP), and publishes to a redis queue.
a seperate agent listens on the redis queue and push data either directly to losant via mqtt or via a moquitto broker acting as a bridge (using QOS1 means we get replay if the link is down).

Originally had single agent doing everything (twisted async server) however dealing with all the disconnect/reconnect networking issues for different parts made the code too complex.
This model also allows us to inject additional specific data for channels (possibly not from modbus) about the device into the same mqtt stream, by simply publishing data from another source to the same redis channel.

So in summary
device <- python agent -> redis queue <- redis subscribe -> mqtt

This is all running on a pi.

We have nearly 20 engines running on a single site Around 20 different sites running at the moment with varying numbers of modbus devices

Example of one pod of engines.

I can’t directly share code, however I can outline specifics.

I am using modbus_tk package.
We parse a config file we download from the COMAP controllers which provides a map of registers (non contiguous) with names, scales, bit decoding (meaning of status bits etc)
The a config file is built listing the names of the registers, the ip address, unit address, cycle time, and what channel to publish on.

This means we can get a modbus device online in a few minutes.

Will follow up with more details.

Cheers

T

2 Likes

Thanks Tim, it is interesting project you have attempted. I would like to see a tutorial like post as you have indicated. That will really help as I am new to PLC programming.

Also I would like to see how this is all setup on Pi to test it out myself. Appreciate if you can share the code. I can send my email address.

1 Like

Hi Taron

What kind of hardware are you using for the gateway?

Hi @paul_wareham,

For this, I believe their gateway was a custom on-premises box that they had. However, this would work on an industrial Dell or HPE gateway, or as something as small as a Raspberry Pi.

We’ve been working with our new Edge Compute Component:

https://docs.losant.com/edge-compute/walkthrough/

It makes the task of this article way easier to solve. I highly recommend checking it out. For this, we have some supported gateways you can take a look at:

https://docs.losant.com/devices/edge-compute/

Let me know if you have any more questions :slight_smile:

Hi Taron,

Could you help me to understand how to use MODBUS: READ Node on the agent to read registers from a Modbus TCP Server on PLC? The installer runs the server without a Slave ID (Slave ID corresponds to a Unit ID setting in the Node as I understand), but Unit ID setting is required by Losant Node. Is there a known approach how to deal with it? Thank you

UPD. Seems to be solved. Installers gave me a wrong IP address. Unit ID = 0 in the Modbus:READ Node is able to pull registerds from Modbus TCP Server without Unit ID.

1 Like

Hi @Alexander_Kondrov,

I just got some good info on this from one of our engineers. The Unit ID/Slave ID for TCP/IP is usually 1 or 0. Unit ID is not needed since the PLC is identified by IP Address but Modbus devices using TCP/IP should have a UnitID incase they convert TCP/IP to Serial\RTU, in which case the Unit ID would be needed .

Here is also a good thread on this topic: https://modbus.control.com/thread/1026171903

1 Like

I’m not using Losant’s agent but am pretty familiar with Modbus applications as the company I work for manufactures a vibration sensor which communicates using the Modbus RTU protocol over a Serial RS485 connection to an edge device which pushes the data to Losant. If you have questions feel free to @ me and I’ll try to help where I can.

2 Likes

Be carefull here. This is very dependent on implementations.

For instance COMAP controllers may/will have different Unit ID’s due to the nature of some of their control tools. For instance their DCU controllers connect to an IBNT (TCP/IP gateway) via CANBUS, and each controller will have a seperate ID.

Their Flex controllers have their own Ethernet card and so could all be on unit 1.
However the minute you want to use their Intelimonitor software to control multiple devices through a single UI, they each need to be on a seperate ID (not for MODBUS but their propriatary netowork protocol), however setting this ID sets the MODBUS unit to the same value.

We find you need to approach each new device family very specifically :wink:

4 Likes

@Leo_Bach1 and @Tim_Hoffman, thank you for your comments. I was able to implement a Modbus TCP exchange with a target device. More Modbus TCP connections are expected, though. I will post to this thread if anything new is discovered