Modbus TCP

Hi all,

I'm facing some issue to find a Modbus TCP library with working example.
The configurarion I'm using consist of an Arudino Uno R3 + Ethernet Shield W5100+ Arduino 1.6.7 version.
For the moment I tried two libraries downloaded from:

and

I tested both libraries downloading a slave example on arduino uno and using my desktop as master.
I tried two different software with the same results Modscan and Simply Modbus TCP Client 8.0.8.

The connection between master and slave is established, the master is able to query the slave receiving a correct reply from the slave for a variable ammount of polls ( in between 20 and 100 ). After that the slave stop to reply to the master query...
Only restarting Arduino it is possible to query again the slave
Anyone faced the same problem or can suggest some possible cause or suggest a different working modbus TCP library? :o :confused:

Thanks in advance,
Abraxax

Pick one piece of code that is causing problems. Post that code HERE. Don't expect people to guess what your problem is/problems are.

I thought it was not difficult for you, skilled guys, to open a link, download a library,import on arduino ide and test an example..
Code is not mine and I didn't change nothing on the code, just test the library example setting a proper ip address for my lan network...
Anyhow here the code, thanks for you help

#include <SPI.h>
#include <Ethernet.h>
#include "MgsModbus.h"

MgsModbus Mb;
int inByte = 0; // incoming serial byte

// Ethernet settings (depending on MAC and Local network)
byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0x94, 0xB5 };
IPAddress ip(192, 168, 1, 15);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);


void setup()
{
// serial setup
Serial.begin(9600);
Serial.println("Serial interface started");

// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);   // start etehrnet interface
Serial.println("Ethernet interface started"); 

// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
  // print the value of each byte of the IP address:
  Serial.print(Ethernet.localIP()[thisByte], DEC);
  Serial.print("."); 

}
Serial.println();

// Fill MbData
//  Mb.SetBit(0,false);
Mb.MbData[0] = 1;
Mb.MbData[1] = 2;
Mb.MbData[2] = 3;
Mb.MbData[3] = 4;
Mb.MbData[4] = 5;
Mb.MbData[5] = 6;
Mb.MbData[6] = 7;
Mb.MbData[7] = 8;
Mb.MbData[8] = 9;
Mb.MbData[9] = 10;
Mb.MbData[10] = 11;
Mb.MbData[11] = 12;

// print MbData
for (int i=0;i<12;i++) {
  Serial.print("address: "); Serial.print(i); Serial.print("Data: "); Serial.println(Mb.MbData[i]);
}
// print menu
Serial.println("0 - print the first 12 words of the MbData space");
Serial.println("1 - fill MbData with 0x0000 hex");
Serial.println("2 - fill MbData with 0xFFFF hex");
Serial.println("3 - fill MbData with 0x5555 hex");
Serial.println("4 - fill MbData with 0xAAAA hex");
}

void loop()
{
if (Serial.available() > 0) {
  // get incoming byte:
  inByte = Serial.read();
  if (inByte == '0') {                                          // print MbData
    for (int i=0;i<12;i++) {
      Serial.print("address: "); Serial.print(i); Serial.print("Data: "); Serial.println(Mb.MbData[i]);
    }
  }  
  if (inByte == '1') {for (int i=0;i<12;i++) {Mb.MbData[i] = 0x0000;}}  
  if (inByte == '2') {for (int i=0;i<12;i++) {Mb.MbData[i] = 0xFFFF;}}  
  if (inByte == '3') {for (int i=0;i<12;i++) {Mb.MbData[i] = 0x5555;}}  
  if (inByte == '4') {for (int i=0;i<12;i++) {Mb.MbData[i] = 0xAAAA;}}  
}

//  Mb.MbmRun();
Mb.MbsRun();
}

Find in attachment also h and cpp files

MgsModbus.cpp (10.6 KB)

MgsModbus.h (3.32 KB)

MgsModbus.cpp (10.6 KB)

I thought it was not difficult for you, skilled guys, to open a link, download a library,import on arduino ide and test an example..

Where did you say that you were using an example sketch WITH NO MODIFICATIONS? Where did you pay for the help you are asking for?

Your sketch has no Serial.print() statements. How do you know that it is it that has stopped?

Quote
I tested both libraries downloading a slave example on arduino one and using my desktop as master.
unquote

I meant that I downloaded on Arduino Uno the slave example included in the library, I though it was fair enough but I must have explained myself badly.

Quote
I tried two different software with the same results Modscan and Simply Modbus TCP Client 8.0.8.
The master is able to query the slave receiving a correct reply from the slave for a variable ammount of polls (in between 20 and 100). After the slave stop to reply to the master query ...
unquote

I did not check it on arduino side, I saw it stop to reply to the query of the programs emulating the modbus Master, with Modscan and Simply Modbus TCP Client 8.0.8.
I've already tried hundreds of times theese two software with other modbus TCP slave devices and they work fine, so I have come to the conclusion that the arduino stopped to reply to the query for some reason

I don't understand why you can't, or won't, post code properly. It is REALLY simple to do it right.

The Mb.MbsRun() function deals with a client connection. Did you get one? Do you stop getting them at some point?

SurferTim has some code on the playground to print the status of the 4 connections that the ethernet shield can manage, assuming that you are using a W5100 based ethernet shield. Find that code, and add it to the Mb.MbsRun() function, to see if somehow the ModBus library is failing to close, and clear, a connection properly, on occasion. 4 failures, and the ethernet shield can no longer accept connections.

I don't really see the point.. why a user with Karma 2494 and 84,350 posts is so hostile with a user with Karma 0 and 15 active posts of which 7 in this conversation.. you should be an example to follow with your experience in the forum and you could even be a little kinder with newbie as I am
I will check how to post properly the code next time.
Sorry PaulS continue a troubleshooting with you and with this attitude is exhausting to me..

Coming to my original question, is you or anyone else familiar with Arduino modbus TCP IP? If yes and you have already tested/written a library including working examples can you share it?

Thanks,

hello, hope the code can help you
This is the example code project about Modbus TCP, Modbus RTU

Read coil status.
Read input status.
Read holding registers.
Read input registers.
Write single coil.
Write single register.
Write multiple registers.
Write multiple coils.
...
See full the code here