recording modbus data

hey there,
i am trying to communicate with an arduino mega using and rs-485 HAT and connected to the pc using a ttl to usb converter. my motive is to read hex data from generating from my modbus software on the pc and turn on various digital output dpending on the value. the exact guidance i need:
1)the modbus master sotware to use on pc!
2)the code to read the hex data
please its urgent can you help me, its for a project!

are you using a USB-RS485 dongle to connect the RS485 HAT to the PC ?
for PC software have a look at modbus slave simulator
also a web search for pc modbus master slave software gives plenty of links

ok! but how will i read the modbus hex to turn on digital outputs?

Have a look here,this is how I learned it and it very good

Or do a search on google,

What code have you got so far and where are you stuck ?

#include <SimpleModbusSlave.h>

enum
{

AI0, //0
AI1, //1
AI2, //2
AI3, //3
AI4, //4
AW0, //5
AW1, //6
AW2, //7
AW3, //8
AW4, //9
DI0, //10
DI1, //11
DI2, //12
DI3, //13
DI4, //14
DW0, //15
DW1, //16
DW2, //17
DW3, //18
DW4, //19
TOTAL_ERRORS, //20
HOLDING_REGS_SIZE
};

unsigned int holdingRegs[HOLDING_REGS_SIZE];
unsigned int counter;
////////////////////////////////////////////////////////////

//void(* resetFunc) (void) = 0;

void setup()
{
/* parameters(HardwareSerial* SerialPort,
long baudrate,
unsigned char byteFormat,
unsigned char ID,
unsigned char transmit enable pin,
unsigned int holding registers size,
unsigned int* holding register array)
*/

modbus_configure(&Serial, 9600, SERIAL_8N1, 1, 2, HOLDING_REGS_SIZE, holdingRegs);

// modbus_update_comms(baud, byteFormat, id) is not needed but allows for easy update of the
// port variables and slave id dynamically in any function.
// modbus_update_comms(9600, SERIAL_8N1, 1);

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
holdingRegs[AI0] = 0;
delay(100);
}

void loop()
{

// modbus_update() is the only method used in loop(). It returns the total error
// count since the slave started. You don’t have to use it but it’s useful
// for fault finding by the modbus master.
holdingRegs[AI0] = holdingRegs[AI0] + 1; // update data to be read by the master
holdingRegs[AI1] = 1; // update data to be read by the master
holdingRegs[AI2] = 2; // update data to be read by the master
holdingRegs[AI3] = 3; // update data to be read by the master
holdingRegs[AI4] = 4; // update data to be read by the master

holdingRegs[DI0] = holdingRegs[AI0]; // update data to be read by the master
holdingRegs[DI1] = 0; // update data to be read by the master
holdingRegs[DI2] = 0; // update data to be read by the master
holdingRegs[DI3] = 0; // update data to be read by the master
holdingRegs[DI4] = 1; // update data to be read by the master

if (holdingRegs[DW0] == 0){
digitalWrite(LED_BUILTIN, LOW);
holdingRegs[DI1] = 0;
}
else {
digitalWrite(LED_BUILTIN, HIGH);
holdingRegs[DI1] = 1;
}

// holdingRegs[TOTAL_ERRORS] = 1;
// counter = 0;
// while ((holdingRegs[TOTAL_ERRORS] > 0)&&(counter <= 10)) {
// holdingRegs[TOTAL_ERRORS] = modbus_update();
// counter = counter + 1;
// delay(10);
// }
holdingRegs[TOTAL_ERRORS] = modbus_update();

// digitalWrite(LED_BUILTIN, HIGH);
delay(100);

// resetFunc();

}
this is the code and i am kinda stuck as they are showing me an error at the modbus configure() part. saying too many return type functions

use code tags, see forum guide

also post the full error message

also have a look at this post

#include <SimpleModbusSlave.h>


enum
{     
 
  AI0, //0   
  AI1, //1
  AI2, //2
  AI3, //3
  AI4, //4
  AW0, //5   
  AW1, //6
  AW2, //7
  AW3, //8
  AW4, //9
  DI0, //10     
  DI1, //11
  DI2, //12
  DI3, //13
  DI4, //14
  DW0, //15     
  DW1, //16
  DW2, //17
  DW3, //18
  DW4, //19
  TOTAL_ERRORS, //20       
  HOLDING_REGS_SIZE
};


unsigned int holdingRegs[HOLDING_REGS_SIZE];
unsigned int counter;
////////////////////////////////////////////////////////////

//void(* resetFunc) (void) = 0;

void setup()
{
  /* parameters(HardwareSerial* SerialPort,
                long baudrate,
      unsigned char byteFormat,
                unsigned char ID,
                unsigned char transmit enable pin,
                unsigned int holding registers size,
                unsigned int* holding register array)
  */
 
 
   
  modbus_configure(&Serial, 9600, SERIAL_8N1, 1, 2, HOLDING_REGS_SIZE, holdingRegs);

  // modbus_update_comms(baud, byteFormat, id) is not needed but allows for easy update of the
  // port variables and slave id dynamically in any function.
  // modbus_update_comms(9600, SERIAL_8N1, 1);
 
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  holdingRegs[AI0] = 0;
  delay(100);
}

void loop()
{
 
  // modbus_update() is the only method used in loop(). It returns the total error
  // count since the slave started. You don't have to use it but it's useful
  // for fault finding by the modbus master.
  holdingRegs[AI0] = holdingRegs[AI0] + 1; // update data to be read by the master
  holdingRegs[AI1] = 1; // update data to be read by the master
  holdingRegs[AI2] = 2; // update data to be read by the master
  holdingRegs[AI3] = 3; // update data to be read by the master
  holdingRegs[AI4] = 4; // update data to be read by the master

  holdingRegs[DI0] = holdingRegs[AI0]; // update data to be read by the master
  holdingRegs[DI1] = 0; // update data to be read by the master
  holdingRegs[DI2] = 0; // update data to be read by the master
  holdingRegs[DI3] = 0; // update data to be read by the master
  holdingRegs[DI4] = 1; // update data to be read by the master

  if (holdingRegs[DW0] == 0){
    digitalWrite(LED_BUILTIN, LOW);
    holdingRegs[DI1] = 0;
  } 
  else {
    digitalWrite(LED_BUILTIN, HIGH);
    holdingRegs[DI1] = 1;
  }

//  holdingRegs[TOTAL_ERRORS] = 1;
//  counter = 0;
//  while ((holdingRegs[TOTAL_ERRORS] > 0)&&(counter <= 10)) {
//    holdingRegs[TOTAL_ERRORS] = modbus_update();
//    counter = counter + 1;
//    delay(10);
//  }
holdingRegs[TOTAL_ERRORS] = modbus_update();
 
//  digitalWrite(LED_BUILTIN, HIGH);
  delay(100);

 // resetFunc();
 
 
 
}
[code]
the full error is:
Arduino: 1.8.13 Hourly Build 2020/06/03 04:33 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"





















C:\Users\DELL\AppData\Local\Temp\arduino_modified_sketch_484411\sketch_sep22a.ino: In function 'void setup()':

sketch_sep22a:51:83: error: too many arguments to function 'void modbus_configure(long int, byte, byte, unsigned int, unsigned char)'

   modbus_configure(&Serial, 9600, SERIAL_8N1, 1, 2, HOLDING_REGS_SIZE, holdingRegs);

                                                                                   ^

In file included from C:\Users\DELL\AppData\Local\Temp\arduino_modified_sketch_484411\sketch_sep22a.ino:1:0:

C:\arduino-nightly\libraries\SimpleModbusSlave/SimpleModbusSlave.h:85:6: note: declared here

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

      ^~~~~~~~~~~~~~~~

C:\Users\DELL\AppData\Local\Temp\arduino_modified_sketch_484411\sketch_sep22a.ino: In function 'void loop()':

sketch_sep22a:97:43: error: too few arguments to function 'unsigned int modbus_update(unsigned int*)'

 holdingRegs[TOTAL_ERRORS] = modbus_update();

                                           ^

In file included from C:\Users\DELL\AppData\Local\Temp\arduino_modified_sketch_484411\sketch_sep22a.ino:1:0:

C:\arduino-nightly\libraries\SimpleModbusSlave/SimpleModbusSlave.h:86:14: note: declared here

 unsigned int modbus_update(unsigned int *holdingRegs);

              ^~~~~~~~~~~~~

exit status 1

too many arguments to function 'void modbus_configure(long int, byte, byte, unsigned int, unsigned char)'



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

which modbus library are you using? it looks like the wrong one
you call

 modbus_configure(&Serial, 9600, SERIAL_8N1, 1, 2, HOLDING_REGS_SIZE, holdingRegs);

with 7 parameters but the library you are using expects 6 parameters

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

therefore you get the error message too many arguments to function

should the library you are using be Arduino modbus

its the simple modbus library! i have use3d it in the header file!

#include <SimpleModbusSlave.h>
i have used the library files and installed it!

there appear to be (at least) two libraries with SimpleModbusSlave.h

I think your program is written for one and using the other
try deleting the existing library and loading the other one

there are a thousand soimple modbus libraries available on github, when i searched it on google. as such thre hasnt been any specified library creator has been mentioned for this library. this is the library i have used!

if you look in simplemodbusng/SimpleModbusSlave.h at master · angeloc/simplemodbusng · GitHub the declaration for modbus_configure takes five parateters

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

yet your code calls with seven parameters

 modbus_configure(&Serial, 9600, SERIAL_8N1, 1, 2, HOLDING_REGS_SIZE, holdingRegs);

which appears to match

void modbus_configure(HardwareSerial *SerialPort, long baud, unsigned char byteFormat,
	unsigned char _slaveID, unsigned char _TxEnablePin, unsigned int _holdingRegsSize,
                      unsigned int* _regs);

from Arduino/SimpleModbusSlave.h at master · pepsilla/Arduino · GitHub

how do i copy this library file now?
and do the required! like copying to the libraries folder etc etc!

download the ZIP file then follow importing a ZIP library
you may get a warning that a library of the same name already exists
delete the old one in directry Documents\Arduino\libraries then install the new one

i am pretty sure i checked as there is no zipfile downloading option there. thats how i did the last one! but here no such option ! can you please check and help me!

horace,
i found the solution it works, thank you for your support! peace out!