RS-485 MODBUS Serial Communication with Arduino as Master

Submitted by Khairul on

Hi,

im khairul from malaysia.

i try 1 of the project provided which is RS-485 MODBUS Serial Communication with Arduino as Master.

im using arduino UNO and ModbusMaster.h library

i having problem with the code while compiling.

node.begin(1,Serial); ---> this line keep getting error no matching function for call to 'ModbusMaster::begin(int, HardwareSerial&)'

also for this line

node.preTransmission(preTransmission);  ---> the error 'class ModbusMaster' has no member named 'preTransmission'

 

here is my full code:

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

#define MAX485_DE      3
#define MAX485_RE_NEG  2

ModbusMaster node;                    //object node for class ModbusMaster

int countvalue = 0;
float pressurevalue = 0;

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()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  
  //pinMode(4,INPUT);
  //pinMode(5,INPUT);
  
  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);
}

void loop()
{
  countvalue = analogRead(A1);
  pressurevalue = map(countvalue, 3, 608, 0, 101.5);
  float voltage = countvalue * (3.0 / 608.0);
  int countvalue1 = countvalue *1.6825657;
  
  node.writeSingleRegister(0x40000,pressurevalue);  //Writes value to 0x40000 holding register
  node.writeSingleRegister(0x40001,voltage);
  node.writeSingleRegister(0x40002,countvalue1);
  
}

 

anyone can help me?

Hello Mr.Khairul,

This error is due to that the library doesn't match.

Download the ModbusMaster.h library zip from this link https://github.com/4-20ma/ModbusMaster. (The Link in the Tutorial is wrong! I apologise for the mistake )

Make sure you have included the ModbusMaster.h library in your sketch. (Tools->Include library->Add .zip library).

After that compile again! You will compile sucessfully :)

 

 

  Joined August 28, 2018      1
Tuesday at 12:17 PM

Thank you for the both projects : 

https://circuitdigest.com/microcontroller-projects/rs485-modbus-serial-…

https://circuitdigest.com/microcontroller-projects/rs-485-modbus-serial…

I have a question please : I want to use the arduino as master like your project, but i want to read data from the PC too, I mean two way communication.

Do you have an arduino code that provide this? Thank you 

  Joined December 13, 2021      2
Monday at 02:28 PM

Hi 

 

I am trying to communicate MODBUS with RS232.

what is the circcuit and is there any changes require in program.

#include <ModbusMaster.h>               //Library for using ModbusMaster
#include <LiquidCrystal.h>              //Library for using LCD display

#define MAX485_DE      3
#define MAX485_RE_NEG  2

ModbusMaster node;                    //object node for class ModbusMaster

LiquidCrystal lcd(8,9,10,11,12,13);  //Object lcd for class Liquidcrystal with LCD pins (RS, E, D4, D5, D6, D7) that are connected with Arduino UNO.

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()
{
  lcd.begin(16,2);
  lcd.print("CIRCUIT DIGEST");
  delay(3000);
  lcd.clear();
  lcd.print("Arduino");
  lcd.setCursor(0,1);
  lcd.print("Modbus Master");
  delay(3000);
  lcd.clear();
  
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  
  pinMode(4,INPUT);
  pinMode(5,INPUT);
  
  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);
}

void loop()
{
  
  float value = 10;
  //analogRead(A0);

  
  Serial.write("hello");
  node.writeSingleRegister(0x40000,value);        //Writes value to 0x40000 holding register
  
  lcd.setCursor(0,0);
  lcd.print("POT Val :");
  lcd.print(value);
  int a= digitalRead(4);                           //Reads state of push button 
  int b= digitalRead(5);

  if (a == 1)
  {
    node.writeSingleRegister(0x40001,1);               //Writes 1 to 0x40001 holding register
    lcd.setCursor(0,1);
    lcd.print("S1: 1");
  }
  else
  {
    node.writeSingleRegister(0x40001,0);              //Writes 0 to 0x40001 holding register
    lcd.setCursor(0,1);
    lcd.print("S1: 0");
  }
  if (b == 1)
  {
    node.writeSingleRegister(0x40002,1);              //Writes 1 to 0x40002 holding register
    lcd.setCursor(8,1);
    lcd.print("S2: 1");
  }
  else
  {
    node.writeSingleRegister(0x40002,0);                //Writes 0 to 0x40002 holding register
    lcd.setCursor(8,1);
    lcd.print("S2: 0");
  }
 
}

  Joined March 27, 2020      1
Friday at 03:13 PM