0

I want to use an Arduino Genuino Zero as a Modbus slave. I tried to search on google, and found a library. When I try using the library, I get a timeout error.

Can anyone tell me how to solve that error? Did anyone manage to get the Arduino Modbus RTU slave library working?

  • Please provide additional details. What is the master device? Check whether baud rate, parity bit, data bit are set properly. And if possible provide the git repository of the library you are using. – goddland_16 Mar 31 '17 at 11:02
  • i find library from this – Beginner Mar 31 '17 at 13:04
  • as master i am using simulator and simulator's name is modbus poll – Beginner Mar 31 '17 at 13:05
0

I am unable to simulate and see the result right now; I hope if the library functions properly this might be a communication issue. I can add a one step debug for you to check and see what is happening.

Your device is acting as a Modbus RTU slave, So take another Arduino, make it act it like a master device, and see what response is coming from the slave device. Here is a glimpse of code I used to make arduino as a master and see the response coming in Serial terminal.

    #include <SoftwareSerial.h>
    #define rxPin 2
    #define txPin 3

    #define RS485_pin 10
    //SoftwareSerial mySerial(rxPin, txPin);
    #define mySerial Serial1


    byte test_data[8] = {0x01,0x03,0x00,0x64,0x00,0x02,0x85,0xD4};

    int data_count = 5;
    byte rcvdata[15];
    int param_value_int[7];

    void setup() {
      // put your setup code here, to run once:
    //mySerial.begin(19200,SERIAL_8E1);
    mySerial.begin(9600);
    Serial.begin(9600);
    delay(1000);
    pinMode(RS485_pin, OUTPUT);
    digitalWrite(RS485_pin,LOW);
    delay(100);
    }
    int count = 0; 
    void loop() {

      if(count%10 == 0) {
        Serial.println();
        send_data();
      }

      count++;

      while(mySerial.available()>0)
      {
        byte b=mySerial.read();
        delay(1);
        Serial.print(b,HEX);
        Serial.print(" ");
      }
      //Serial.println(count);
      while(Serial.available())
      {
        digitalWrite(RS485_pin,HIGH);
        byte a=Serial.read();
        delay(1);
        mySerial.print(a);
        digitalWrite(RS485_pin,LOW);
       // Serial.print(a);
      }


      delay(1000);
      //while(1);
    }

    void send_data() {
      Serial.println("send data");

      digitalWrite(RS485_pin,HIGH);
      delay(1);
      for(char i=0;i<8;i++){
        mySerial.write(test_data[i]);
        delay(1);
        Serial.print(test_data[i],HEX);
        Serial.print(" ");
      }

      Serial.println();
      digitalWrite(RS485_pin,LOW);
      delay(1);
    }

Here you can use Software Serial or normal Serial1 or Serial2 on devices like Mega. Make sure if it is RS485 based you have to toggle the RS485 read-write pin each time to enable and disable one feature at a time as RS 485 uses half-duplex communication.

In this case test_data[] uses Function code FC03 Read holding register. You can make your custom data. I am assuming you are familiar with all the types available.Still there is a great site [ link ] to look up to if you don't know the basics. The last two bytes are checksum. And there is an online checksum calculator available if you need also https://www.lammertbies.nl/comm/info/crc-calculation.html

In this way you can at least debug and share the result, so that we can help more.

  • Comments are not for extended discussion; this conversation has been moved to chat. – Nick Gammon Apr 5 '17 at 8:46
  • This isn't a forum. Comments on the question are for clarification of the question, not for experimenting with different ways of solving the problem. – Nick Gammon Apr 5 '17 at 8:46
0

so i edited code your as below #define mySerial Serial1

byte test_data[8] = {0x01,0x03,0x00,0x64,0x00,0x02,0x85,0xD4};

int data_count = 5;


void setup() {

mySerial.begin(9600);
Serial.begin(9600);
delay(1000);

}
int count = 0; 
void loop() {

  if(count%10 == 0) {
    Serial.println();
    send_data();
  }

  count++;

  while(mySerial.available()>0)
  {
    byte b=mySerial.read();
    delay(1);
    Serial.print(b,HEX);
    Serial.print(" ");
  }
  //Serial.println(count);
  while(Serial.available())
  {

    byte a=Serial.read();
    delay(1);
    mySerial.print(a);

  }


  delay(1000);

}

void send_data() {
  Serial.println("send data");


  delay(1);
  for(char i=0;i<8;i++){
    mySerial.write(test_data[i]);
    delay(1);
    Serial.print(test_data[i],HEX);
    Serial.print(" ");
  }

  Serial.println();

  delay(1);
  • beside in slave library, which i have uploaded. i replace 'Serial' with 'Serial1' as i am using genuino zero. – Beginner Apr 4 '17 at 7:31
  • but still i am not getting any response – Beginner Apr 4 '17 at 7:31
  • Hey do you have any actual Modbus device? – goddland_16 Apr 4 '17 at 8:13
  • you can use modscan32 software to retrieve data from slave device also. The software acts as a master device and you have slave device. – goddland_16 Apr 4 '17 at 8:15
  • 1
    connection is working fine. i am not getting any error. i used their lamp example to read coil. in that example, they set status of coil 1. but i am getting response "zero" – Beginner Apr 5 '17 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.