Modbus slave with Arduino from https://create.arduino.cc

I was playing with this project from https://create.arduino.cc/projecthub/hwhardsoft/how-to-use-modbus-with-arduino-6f434b. I could not get it working. I am using Nano with Max485. I get an error message on

modbus_configure(9600, 1, 6, TOTAL_REGS_SIZE, 0);

SimpleModbusSlave_012222:44:50: error: too few arguments to function 'void modbus_configure(HardwareSerial*, long int, unsigned char, unsigned char, unsigned char, unsigned int, unsigned int*)'

I am not sure what I am doing wrong. Here is the code I have and how can we fix this?

#include <SimpleModbusSlave.h>

#define  ledPin  7 // onboard led 
#define  buttonPin  12 // push button

//////////////// registers of your slave ///////////////////
enum 
{     
  // just add or remove registers and your good to go...
  // The first register starts at address 0
  ADC0,     
  ADC1,        
  ADC2,
  ADC3,
  ADC4,
  ADC5,  
  LED_STATE,
  BUTTON_STATE,
  TOTAL_ERRORS,
  // leave this one
  TOTAL_REGS_SIZE 
  // total number of registers for function 3 and 16 share the same register array
};

unsigned int holdingRegs[TOTAL_REGS_SIZE]; 

void setup()
{
  modbus_configure(9600, 1, 6, TOTAL_REGS_SIZE, 0);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop()
{
  holdingRegs[TOTAL_ERRORS] = modbus_update(holdingRegs);
  for (byte i = 0; i < 6; i++)
  {
    holdingRegs[i] = analogRead(i);
    delayMicroseconds(50);       
  }
  byte buttonState = digitalRead(buttonPin); // read button states
  holdingRegs[BUTTON_STATE] = buttonState; 
  byte ledState = holdingRegs[LED_STATE]; 
  if (ledState) // set led
  {     
    digitalWrite(ledPin, HIGH);
  }  
  else if (ledState == 0) // reset led
  {
    //digitalWrite(ledPin, LOW);
    holdingRegs[LED_STATE] = 0;
  }
  
}

The function "modbus_configure" expects more arguments than what you are sending in your setup().

This is what you sent, 5 arguments.
modbus_configure(9600, 1, 6, TOTAL_REGS_SIZE, 0);

This is what it expects, 7 arguments and lists what the arguments should be.
'void modbus_configure(HardwareSerial*, long int, unsigned char, unsigned char, unsigned char, unsigned int, unsigned int*)'

Edit:
Looking at the replies from the tutorial, looks like another person had the same issue when compiling. You can check the "SimpleModbusSlave.h" and ".cpp" file and locate the "modbus_configure" function to see if the developer left any notes on the function. I suspect the files you have for the library are not the correct ones.

I checked his GitHub for the project and it only lists the MKR version. The MKR is setup to only need 5 arguments. Below is what he had put for the "SimpleModbsSlaveMKR" This is completely different than what you are seeing for your issue.

void modbus_configure(long baud, byte _slaveID, bool _HalfDuplexEnable, unsigned int _holdingRegsSize, unsigned char _lowLatency);

There are several version/forks of "SimpleModbusSlave" available and it is unclear to me, which version the tutorial uses and which version you have installed on your PC. Obviously the example code and your library don't fit together.

Some time ago I have written my own simple "Server" library (former slave). All examples in folder 02 should work as of today out of the box and can be used with hardware serial and Softserial.

https://werner.rothschopf.net/microcontroller/202112_arduino_modbus_server.htm

Proffit,

Good to know. I am still trying to understand how to use Modbus with Nano. Is there a simple code that I can use for Nano with Generic Modbus software?

Noiasca,

Thank you for the link. Wow, lot of information but I do not understand all of them. You said examples in folder 02. Where is it?

first you have to install the library,
than you will find the examples in the IDE: