Modbus TCP Slave best library for use with 2 Arduino YUN modules

Does anyone have a working Modbus TCP master and slave codes for using 2 Arduino Yun modules? A detailed setup procedure would be nice also! planning to us the Wifi on each card also...

I found lots of examples but not alot of details on how to implement them propelry.

Any help will be greatly Appreciated.

Sorry Moderators if i put this in the wrong area or something!!!

Welcome to the forum.

The only Arduino library I know of that can do Modbus TCP master or slave is this one My Arduino Projects - Website dedicated to my arduino projects - A ModBus TCP library for the Arduino system
I haven't used this particular library, but it has been extended from the popular mudbus library which does Modbus TCP slave only.

I think there is some example code provided.

I'm assuming you want to connect the Arduino side of the Yuns and not the GNU/Linux side, is that correct ?


well i am just playing around with these things and trying some stuff but i would like use it as remote I/O. from what I've read that may have to go through both but i don't know enough yet to be sure.

The slave only sketch may work as i can use some free scada stuff to tinker around with it.

some of the examples I've looked at showed the need to install some file in the linux side and the the sketch on the micro-controller side.

Any idea what this is about? Do i need to define all the inputs in the example code or should it be ready to just run? thoughts?

dbaker wrote:

from what I've read that may have to go through both

Can you expand on this to clarify what you mean ?

Actually, I'm not clear on exactly what you wish to do, do you want two Arduinos communicating via Modbus on the AVR side, using the serial port ?

Or are you wishing to use the GNU/Linux side of bot Yuns to communicate with each other via Modbus ?
You mentioned SCADA, how does this connect into the system?

Are you trying to have I/O from one Arduino exchanged with the other or replicated to the other ?

I have plenty of ideas, but for them to be relevant I need to understand very clearly what you are trying to do.
If you can explain your needs and also post your code you have, then that would help.


Paul thanks for your patience with me on this.

So i have and OPC server that can collect Data from Modbus TCP devices. I’ve been using Siemens but they are too costly for a project i am toying with.

So want to have the master communicate with the OPC server. Then I would line a couple of slaves forwarding there I/O to the master to be read along with all of the masters I/O by the OPC server. So replicated to the other one is the goal.

Id like to use only the WiFi or LAN port but would like to also have serial if it comes up later down the road.


Master slave drawing.pdf (96.2 KB)

Ok, I understand better now, thanks for clarifying that.

The first thing to consider is that the Yún has its Ethernet capability on the GNU/Linux side, not the AVR side.
This means that to use the Yún as a Modbus TCP device, you need to use Linux based Modbus code, not code designed for AVR.

So, a few questions:
Is there a definite reason you need to use the Yún at all, do you need to use the Linux side especially ?

Do you need to mirror the slave data in the master, if so, why ?

I am assuming your OPC server will be in effect the Modbus TCP master and that it will need to talk to any Arduino board as Modbus TCP slaves.

If you have an Arduino as Modbus TCP slave to the OPC server, and you definitely need to have other Arduino slave data then you need to make that Arduino also a Modbus TCP master as well as slave to the OPC server.

To clarify, in your drawing, the Arduino TCP device will need to be setup as a TCP slave to the OPC server and it will also need to be setup as a Modbus TCP master for communicating to the other Arduino TCP slave.

I would suggest a simpler method, as follows:
Use Arduino UNO with Ethernet, say like a Freetronics Ether-Ten, it's an all in one board, and nicely built.
Setup each Arduino as a Modbus TCP slave, using standard AVR libraries for Modbus, simple.

Then configure your OPC server to communicate to each of the Arduino TCP slaves, which will have different IP addresses.

You will need to setup port forwarding for each Arduino in the remote router.

This method also has the advantage of being more fault tolerant, in that if one Arduino becomes unavailable, then you should still be able to communicate to the other.

Also, the complete code will be a heck of a lot simpler if you have no need for the Linux part of the Yún.
If you are certain you need the Linux part of the Yún, then, as I mentioned you will need to use a Linux library for the Modbus, and produce suitable code to operate on this component. Not necessarily an easy or quick thing to do.

I hope this helps you with designing what you need.


Paul I will study on this and get back with you. Thanks for the speedy response.

Paul are there any libraries out there for using the yun as a modbus slave? I realize it would have to go through the Linux side but is there a how to and a sketch out the that I could do some testing with?

I did get the Ethernet shield working but will likely new the yun for its extra power for running secure connections like open Vpn. First id like to get some modbus registers working on the yun.

Yes, I found one the other day after my last post, which was still hiding in the 300 odd open tabs in FireFox.
As the Yún uses open-wrt there is a makefile for the standard libmodbus I hinted to before.
The link openwrt-packages-yun/Makefile at master · arduino/openwrt-packages-yun · GitHub

I found that by googling 'libmodbus on arduino yun'

The library libmodbus is well proven and is used in many situations.
The actual site for the library is

I don't have any experience with using a Yún, rather, I use the libmodbus library on Intel and ARM boards like Cubieboard. I can only imagine the Yún will be the same in practice.

The documentation for libmodbus is quite good, and I had no rouble getting things running nicely.


I'm going to give it a go since I already have a yun. Not sure how well this will go but I'll give it a shot.

Thanks again.