Arduino rs485 Modbus

Hi everybody,

i am looking for somebody, who help me with my problems or give me some advice.

I need to make code from arduino modbus master as Reader data via rs485 from multiple arduino modbus Slave as writer data (measure value and write/hold data in register).

So i can not found some example code which will be working. Would anyone help me or provide functional background materials for the study?

Thak you very much

The best regadrs

Hynek

A good place to start would be the ArduinoModbus Library... it comes with examples - https://www.arduino.cc/en/ArduinoModbus/ArduinoModbus

A good place to start would be the ArduinoModbus Library...

This is only true for newer ARM based Arduinos. The AVR are less targeted by the library, it simply need too much memory for this MCUs.

There is no clear reommendation for the AVR Arduinos but there are dozens of thread in the forum to keep you started.

I created code for modbus master and slave. Arduino slave “measure” data and write data in register (holding register). After than arduino master read data via modbus (connected rs485) … I create register for 60 value but the master can read only 30 value. Here are the code.

I make some mistake ???

Master :

//Arduino modbus master

//Knihovny
#include <ModbusMaster.h> //Library for using ModbusMaster

//
#define MAX485_DE 3
#define MAX485_RE_NEG 2

//
ModbusMaster node; //object node for class ModbusMaster

//
int pocet = 59; // pocet registru k vycteni

int8_t j, result;
int16_t data[59];

//
void preTransmission() { //Function for setting stste of Pins DE & RE of RS-485
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}

void postTransmission() {
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}

//
void setup() {
Serial.println(“CIRCUIT DIGEST”);
Serial.println(“Arduino”);
Serial.println(“Modbus Master”);
delay(1000);

//
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);

//
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);

//
Serial.begin(115200); //Baud Rate as 115200

//
node.begin(1, Serial); //Slave ID as 1
node.preTransmission(preTransmission); //Callback for configuring RS-485 Transreceiver correctly
node.postTransmission(postTransmission);

//
Serial.println(“Start”);
}

void loop(){
//
uint8_t j, result;
int16_t data[pocet];
result = node.readHoldingRegisters(0, pocet);

Serial.println();
Serial.print(“Co tam mam?”);

if (result == node.ku8MBSuccess)
{
for (j = 0; j < pocet; j++)
{
data[j] = node.getResponseBuffer(j);
Serial.println();
Serial.print("Adresa registru je: ");
Serial.print(40000+j);
Serial.println();
Serial.print(“Hodnota je: “);
Serial.print(data[j]);
Serial.println(”— — —”);
}
}

delay(2000); //delay 2s

}

Slave:

//Arduino nano RS-485 Modbus Slave

//Knihovny
#include<ModbusRtu.h> //Library for using Modbus in Arduino

//promenne
Modbus bus; //Define Object bus for class modbus
int16_t modbus_array = {
11,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,11, //
22,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,22, //
33,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,33, //
}; //Array initilized with three 0 values

int p = (sizeof(modbus_array)/sizeof(modbus_array[0]));

boolean start = true;

void setup() {
Serial.begin(9600);
Serial.println(“RS-485 Modbus”); //Welcome Message
Serial.println(“Arduino Slave”);
delay(1000);

//
bus = Modbus(1,1,4); //Modbus slave ID as 1 and 1 connected via RS-485 and 4 connected to DE & RE pin of RS-485 Module
bus.begin(115200); //Modbus slave baudrate at 9600

//
Serial.println(“Start”);
Serial.println("Velikost array je: ");
Serial.println((sizeof(modbus_array)/sizeof(modbus_array[0])));
Serial.println("Velikost array pro podmínku je: ");
Serial.println(p);
delay(1000);
}

void loop() {
bus.poll(modbus_array,sizeof(modbus_array)/sizeof(modbus_array[0])); //Used to receive or write value from Master

/*
//výpis dat
for (int i = 0; i < p ; i++ ){ //Výpis dat
//Serial.println();
Serial.print("Adresa registru je: ");
Serial.print(40000+i);
Serial.println();
Serial.print("Hodnota je: ");
Serial.print(modbus_array*);*

  • Serial.println();*
  • }*
  • //delay(1000); //delay 1s*
    _ */_

}

Edit your post and insert code tags!

Sorry about it, i apologize.

Master:

//Arduino modbus master

//Knihovny
#include <ModbusMaster.h>               //Library for using ModbusMaster

//
#define MAX485_DE      3
#define MAX485_RE_NEG  2

//
ModbusMaster node;                    //object node for class ModbusMaster

//
int pocet = 59; // pocet registru k vycteni       

int8_t j, result;
int16_t data[59];

//
void preTransmission() {           //Function for setting stste of Pins DE & RE of RS-485
  digitalWrite(MAX485_RE_NEG, 1);             
  digitalWrite(MAX485_DE, 1);
}

void postTransmission() {
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}

//
void setup() {
  Serial.println("CIRCUIT DIGEST");
  Serial.println("Arduino");
  Serial.println("Modbus Master");
  delay(1000);

  //
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);


  //
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  //
  Serial.begin(115200);             //Baud Rate as 115200

  //
  node.begin(1, Serial);            //Slave ID as 1
  node.preTransmission(preTransmission);         //Callback for configuring RS-485 Transreceiver correctly
  node.postTransmission(postTransmission);

  //
  Serial.println("Start");
}

void loop(){
  //
  uint8_t j, result;
  int16_t data[pocet];
  result = node.readHoldingRegisters(0, pocet);

  Serial.println();
  Serial.print("Co tam mam?");
   
  if (result == node.ku8MBSuccess)
  {
    for (j = 0; j < pocet; j++)
    {
      data[j] = node.getResponseBuffer(j);
      Serial.println();
      Serial.print("Adresa registru je: ");
      Serial.print(40000+j);
      Serial.println();
      Serial.print("Hodnota je: ");
      Serial.print(data[j]);
      Serial.println("--- --- ---");
    }
  }

  delay(2000);        //delay 2s
 
}

Slave:

//Arduino nano RS-485 Modbus Slave

//Knihovny
#include<ModbusRtu.h>       //Library for using Modbus in Arduino

//promenne
Modbus bus;                          //Define Object bus for class modbus
int16_t modbus_array[] = {
  11,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,11,        //
  22,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,22,        //
  33,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,33,        //
  };    //Array initilized with three 0 values

int p = (sizeof(modbus_array)/sizeof(modbus_array[0]));

boolean start = true;
                     
void setup() {
  Serial.begin(9600);
  Serial.println("RS-485 Modbus");     //Welcome Message
  Serial.println("Arduino Slave");
  delay(1000);
 
  //
  bus = Modbus(1,1,4);            //Modbus slave ID as 1 and 1 connected via RS-485 and 4 connected to DE & RE pin of RS-485 Module
  bus.begin(115200);                //Modbus slave baudrate at 9600

  //
  Serial.println("Start");
  Serial.println("Velikost array je: ");
  Serial.println((sizeof(modbus_array)/sizeof(modbus_array[0])));
  Serial.println("Velikost array pro podmínku je: ");
  Serial.println(p);
  delay(1000);
}

void loop() {
   bus.poll(modbus_array,sizeof(modbus_array)/sizeof(modbus_array[0]));       //Used to receive or write value from Master

   /*
   //výpis dat
   for (int i = 0; i < p ; i++ ){       //Výpis dat
      //Serial.println();
      Serial.print("Adresa registru je: ");
      Serial.print(40000+i);
      Serial.println();
      Serial.print("Hodnota je: ");
      Serial.print(modbus_array);
      Serial.println();
   }

   //delay(1000);        //delay 1s
   */
   
}
  node.begin(1, Serial);            //Slave ID as 1
  node.preTransmission(preTransmission);         //Callback for configuring RS-485 Transreceiver correctly
  node.postTransmission(postTransmission);

  //
  Serial.println("Start");

Bad idea. You tell the ModbusMaster library that it can exclusively use the Serial interface for the Modbus traffic and a few lines later you write something to the same interface. The opposite side won't expect other stuff to happen on that line.

Use lower baudrates than 115200 to start. If everything works you may increase the baudrate.