Modbus TCP (not serial) master help needed

Hello, after a lot of searching, I'm here to ask for some help.

I have an Arduino Uno with an Ethernet shield. I have a data recorder that uses modbus TCP (no RS485), and I want the Arduino to poll and store 9 input registers from that data recorder (located in another room) via modbus tcp. That's it. However, there is only 1 modbus TCP master library (My Arduino Projects - Website dedicated to my arduino projects - A ModBus TCP library for the Arduino system), and it has zero documentation, and also doesn't work (does nothing at all, no serial feedback, no menu, using the provided example code). The only other modbus master library is for RS485, and the one working modbus TCP library (mudbus) is slave-only.

I don't even need a library, I just need to poll the first 9 16-bit input registers (modbus function 4) from a single modbus TCP slave (data recorder box). Modpoll works correctly in command prompt on windows, so I know I can do it, but I have no idea how to code it with the ethernet shield. I've tried using the ethernet library to send a client.println of the TCP modbus request string to port 502, but I get gibberish ascii in response after connecting.

I've successfully completed many other arduino projects, so I think that this is doable, but just needs a programmer more versed in tcp/modbus than I am.

Thanks in advance.

FYI, this is the modpoll response:

C:\>modpoll -1 -t 3 -c 9   -r 1 -m tcp 10.20.10.62
modpoll 3.4 - FieldTalk(tm) Modbus(R) Master Simulator
Copyright (c) 2002-2013 proconX Pty Ltd
Visit http://www.modbusdriver.com for Modbus libraries and tools.

Protocol configuration: MODBUS/TCP
Slave configuration...: address = 1, start reference = 1, count = 9
Communication.........: 10.20.10.62, port 502, t/o 1.00 s, poll rate 1000 ms
Data type.............: 16-bit register, input register table

-- Polling slave...
[1]: 22259
[2]: 25091
[3]: 21991
[4]: 24928
[5]: 23212
[6]: 22801
[7]: 23093
[8]: 23253
[9]: 20616

The modbus TCP protocol is here:

does nothing at all, no serial feedback, no menu, using the provided example code

You didn't post that example code (I guess it's from you because the library doesn't provide examples).
The library itself does only the ModBus stuff, you have to initialize the Ethernet Shield yourself.

Just to ensure: You have an original Ethernet Shield from Arduino, don't you?

I can understand your situation, unfortunately, there doesn't appear to be a functional Modbus master for TCP for the Arduino. I too looked a few months ago and found nothing of note that could be used.

I ended up re-thinking my whole project what I wanted to do, and decided to move to using a small ARM based board, Cubieboard II which I installed GNU/Linux Debian so I could use a TCP master library available for use with Linux/BSD.

I'm not sure what your needs are and if you are wanting to use or need to use an Arduino for your application and what you then do with the data once you have it from the Modbus slave, but maybe going to such a platfrom as a small ARM or GNU/Linux might provide a way for you as it did me.

I have used the serial library, SimpleModbusSlave that works over async serial comms such as USB/TTL/RS-485/RS-232 and it does work well, but as you say, you can not make use of that in this case.

But, like you found, there is no working TCP library for Modbus master for Arduino, not one that is well supported and used.

On my Cubieboard II, I use the libmodbus-3.1.1 library, which can do TCP as well as RTU modes.
Currently I am developing an application that uses this library that talks Modbus TCP to any number of devices, such as PLC's, solar controlling equipment and Arduino.

If this sounds to you like a solution, to use a small ARM based board, such as Cubieboard, RPi, Beagle so forth, then I can help you out. You can read more about it in my signature and see it in action.


Paul

pylon:
You didn't post that example code (I guess it's from you because the library doesn't provide examples).
The library itself does only the ModBus stuff, you have to initialize the Ethernet Shield yourself.

Just to ensure: You have an original Ethernet Shield from Arduino, don't you?

The example code is on the website My Arduino Projects - Website dedicated to my arduino projects - A ModBus TCP library for the Arduino system and is included in the OLD version of the library. The library is also strange in that the IP is set in the library itself. The example code is about the only (very incomplete, very hard to decipher) documentation you can find.

I'm using an ethernet shield (http://www.seeedstudio.com/depot/Ethernet-Shield-p-518.html) that works well enough with the standard ethernet examples.

Thanks rockwallaby for the suggestion. I might just have to do that, but I'd rather avoid it since I'm familiar with arduino and can source it easily, and moving to another platform means starting fresh for just one project. I came here looking to see if anyone could help me to do my specific task, which is to send the very specific request to the modbus slave (function 4, 9 16-bit input registers), and how to receive the response. It should be a piece of cake for those in the know, since even I was able to get some sort of response, just not the right one. I tried to make heads and tails of the library, but I gave up halfway because of the structure.

What I would do with 9 registers is store them (no need for modbus registers, just variables), put them through some calculations, and output those values on an LCD. After one minute, do another poll. That's it. Very simple, and ARM would really be overkill.

The example code is on the website My Arduino Projects - Website dedicated to my arduino projects - A ModBus TCP library for the Arduino system and is included in the OLD version of the library. The library is also strange in that the IP is set in the library itself. The example code is about the only (very incomplete, very hard to decipher) documentation you can find.

This example is for a ModBus TCP slave. I agree that there is no documentation other than the source code itself. Also the source code seems to be incomplete but at least it's point to start with given that there are no better alternatives.

To read from a slave you have to use the Req method.
Remove line 18 and replace "ServerIp" in line 82 by "remSlaveIP". That way you should at least get a connection to the slave and send the command. Unfortunately the reception of the requested data doesn't seem to be implemented yet. So without investing some time you won't be able to use this library.

Did u solve this? I have a working mdb client. Let me know if you still need it

Imanuel:
Did u solve this? I have a working mdb client. Let me know if you still need it

No, I never got it working on Arduino, other than borking the data recorder. I followed rockwallaby's advice and moved to a different platform, namely the Raspberry Pi, since I have some linux background (I looked into the cubieboard but it seemed less noob-frienly and had a smaller user base/support). The modbus TCP implementation for the pi is fairly robust/complete (in python), and after a couple weeks I'm able to :

  1. Get the data from the data recorder via modbus TCP and python, calculate the proper value, and store in mysql every minute.
  2. Put up 4 graphs of the data on the raspberry's web server every 5 minutes.

I'm working on making a arduino modbus TCP slave to read and store temperatures for the pi to poll. I'll also add some alarm and mailing functionality to the pi. I've never programmed in python before, although I've done linux sysadmin tasks (but that was 10+ years ago), so this goes to show that it's about as easy/difficult as the arduino. Good thing about this is that I can easily migrate to a more powerful server with the programs I've already written in python. The arduino has it's uses, but it's also limited in many ways, so I'm keeping it for the simpler tasks.

Basically, I needed a modbus TCP master for Arduino, and there wasn't one, and I lacked the knowhow to make my own. The pi actually suits my needs better, since it has more capability, and isn't actually that much more expensive (factoring in the extra inputs, video output, speed, memory, and ethernet).

Thanks to everyone for the help.

Hi,

I have the same issue as ccmanga.

Could someone figure out why the modbus tcp master library not working? or does anyone have an alternate library for arduino modbus tcp master?

I donot have a strong programming background and therefore would not prefer switching platform.

Please guide me.

Thanks.