[SOLVED] Communication modbus/TCP

Hi everybody,

I am a new user of arduino, and i have done some codding on different type (mostly c, python). I am totally new about modbus protocol and internet protocols. I do understand the principles of layers and so on (I made researchs about how it works)
I am requiring your help for a personal project.

I try to collect data throught modbus/TCP protocol from socomec device (to read registers from the device, it is working with a windows software, so it must work from ardwino card?)

I do have different questions about how to doit:

  • Using the EthernetClient would allow me to send query to the device and get answers??
  • I did set up the ip server(socomec device) and the port given in its datasheet (502, as usually), I tried to send a query through print, but it doesnt work, would it work better if I use a write function??
  • Do I need to send the whole query "00 01 00 00 00 06 05 03 C5 5E 00 02" or can I skip the first 12 digits or others??
  • I have seen that trying to send the previous query like that can't work as it's longer than a "long", can I send 12 times for each word and then read an answer? is the software on the other side understand it as a sigle query?

Thank you for your time and your answers (hopefully a lot).

PS: Sorry if it's not a proper english, it is not my natal language.
King regards to all.

Cheers

Hi there,

Maybe I'll get luckier with this new post about my codding part.
I did try to use this code, but sadly it doesn't work. I think it's a problem about my write function or about the message sent. Any help please??
I am using a UNO card and an ethernet board.

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

byte mac[]     = { 
  0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };
byte ip[]      = { 
  192, 168, 10, 96 };
byte server[] = {
  192, 168, 10, 196};
byte message[]= {
 0x00, 0x01, 0x00, 0x00, 0x00, 0x06 0x05, 0x03, 0xC5, 0x5E, 0x00, 0x02};
EthernetClient client;

void setup(){
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  Serial.print("notre Adresse IP : ");
  Serial.println(Ethernet.localIP());
  Serial.println();
  // Laisse une seconde au shield Ethernet pour initialisation
  delay(1000);
  Serial.println("Connexion en cours...\n");
  if (client.connect(server, 502)) {
    Serial.println("Connexion OK\n");


  }
}

void loop()
{

  client.write(message, sizeof(message));

  // Lecture resultat requete
  if (client.available()) {
    char c = client.read();

    Serial.println(c, HEX);

  }

  // Arret client a la deconnexion du serveur
  if (!client.connected()) {
    Serial.println();
    Serial.println("Deconnexion\n");
    client.stop();


    // Arret des traitements
    for(;;)
      ;
  }
}

:

Unless you are trying to maintain a persistent connection, you should move the client.write call to setup in this case. Otherwise it sends that string to the server every iteration of the loop(), but reads only one character from the rx buffer.

  delay(1000);
  Serial.println("Connexion en cours...\n");
  if (client.connect(server, 502)) {
    Serial.println("Connexion OK\n");

// Move client.write to here    
    client.write(message, sizeof(message));
  }
}

void loop()
{

// remove client.write from here
//  client.write(message, sizeof(message));

  // Lecture resultat requete
  if (client.available()) {
    char c = client.read();

    Serial.println(c, HEX);

  }

Hi,

Indeed it might be a good idea.

In case of it's not working either, do you think that if I send the message trought socket librairy, it would works? I mean instead of using client.write(...) usind a socket.send(...)?

I havent got the socomec device to see if it would send me back something.

I do not think that would make a difference. I've had really good luck using the ethernet library using UDP and TCP protocols, even persistent connections. My longest persistent client connection was 12 hours without a single packet drop. I terminated it because I needed the computer and Arduino for other stuff.

I havent got the socomec device to see if it would send me back something.

If you do not have the device acting as the server, it will be very difficult to troubleshoot.

Hi SurferTim,

I just got back the external adress and port to connect to the socomec device, I ill try to send the query and let you know where I am standing.

tahnk you for your help

Indeed it's working better.

You were right. I supposed that what i was sending was wrong so I didn't get an correct answer, but it was correct. I missunderstood what I got back...

Thank you a lot for your help!

Sir I want to make a system where there is a server (arduino mega) it is connected to a slave (arduino uno) through Ethernet shields. and the website is available on arduino mega to view values.

Now i would like to add a temp sensor on the slave and display the value on the website. website is up and running but i want to implement MODBUS TCP between the server and slave, im stuck i dont knw how to start? A simple example for understanding? Any help would be great thanks.