Modbus RTU RS485 requestFrom problem

Hy!
I want to use modbus communication protocol over RS485. I have one master device and multiple slave devices. First I want to test the protocol with one slave. The slave device have an VEML6030 light sensor and send the LUX data to master and the master display on the Serial monitor. The master device hardware serial used to RS485 and the software seriel to serial monitor.

Master code:

#include <ArduinoRS485.h> 
#include <ArduinoModbus.h>
#include <SoftwareSerial.h>

#define RE 6
#define DE 7
#define LB_ADDRESS  0x01
#define LT_ADDRESS  0x02
#define RB_ADDRESS  0x03
#define RT_ADDRESS  0x04

int LB_VALUE=0;
int LT_VALUE=0;
int RB_VALUE=0;
int RT_VALUE=0;

SoftwareSerial mySerial(4, 5);

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);

  mySerial.println("Master device initializing...");
  
  RS485.setPins(1,DE,RE);

  if (!ModbusRTUClient.begin(9600)) {
    Serial.println("Failed to start Modbus RTU master!");
    while (1);
  }else{
    mySerial.println("Modbus RTU master started");
  }
  

}

void loop() {
 
 //ModbusRTUClient.holdingRegisterWrite(4,0x00, 11);
 //RT_VALUE = ModbusRTUClient.holdingRegisterRead(4, 0x00);
 //mySerial.println(RT_VALUE);
  if (!ModbusRTUClient.requestFrom(42, INPUT_REGISTERS, 0x00, 1)) {
    mySerial.println("failed! ");

  } else {
    mySerial.println(ModbusRTUClient.inputRegisterRead(42,0x00)); 
  }
 

}

Slave code:

#include <VEML6030.h>
#include <ArduinoRS485.h> 
#include <ArduinoModbus.h>

#define BOARD_ADDRESS 0x04

#define RE 5
#define DE 6
#define ADDRESS 4
#define INT 2

VEML6030 ALS;



float LUX_VALUE;



void setup() {
  pinMode(ADDRESS, OUTPUT);
  pinMode(INT, INPUT);
  digitalWrite(ADDRESS, LOW);
 
  
  Serial.begin(9600);  
  RS485.setPins(1,DE,RE);
  while (!Serial);
   if (!ModbusRTUServer.begin(42, 9600)) {
    Serial.println("Failed to start Modbus RTU Slave!");
    while (1);
  }
  
  ALS.begin();  //Begin the UV module
 /* LUX_VALUE = GetLightValue();
  delay(200);
  ALS.Shutdown();
  ALS.IntOn();
  ALS.SetWindow((LUX_VALUE + 500),(LUX_VALUE / 10));*/
  delay(500);
  ALS.PowerOn();

  delay(100);
  
 // attachInterrupt(digitalPinToInterrupt(INT), LightInterrupt, FALLING);

 ModbusRTUServer.configureHoldingRegisters(0x00, 1);
 ModbusRTUServer.configureInputRegisters(0x00, 1);

 

}

void loop() {
/*ModbusRTUServer.poll();
ModbusRTUServer.holdingRegisterWrite(0x00, 11);*/
ModbusRTUServer.poll();
ModbusRTUServer.inputRegisterWrite(0x00,(int)GetLightValue());
//delay(500);
  
 
}


float GetLightValue(void){
  delay(1000);
  LUX_VALUE = ALS.GetLux();
  return LUX_VALUE;
}

void LightInterrupt(){

}

I've tried it in many ways, but it dosent work, I can't read the light value on the master.

What is the problem?

P.S.: I tested the ArduinoModbus libary example (ModebusRTUClientToggle & ModbusRTUServerLed) and its work great.

P.S.: I tested the ArduinoModbus libary example (ModebusRTUClientToggle & ModbusRTUServerLed) and its work great.

If these examples work unmodified you must have an MKR hardware with the appropriate shield. In this case your modifications in the sketch to adapt to other hardware:

RS485.setPins(1,DE,RE);

is not necessary and makes the sketch fail.

I've tried it in many ways, but it dosent work, I can't read the light value on the master.

What output do you see on the serial monitor? In case you get a "failed!" you should print out the string returned by ModbusRTUClient.lastError().

pylon:
If these examples work unmodified you must have an MKR hardware with the appropriate shield. In this case your modifications in the sketch to adapt to other hardware:

RS485.setPins(1,DE,RE);

is not necessary and makes the sketch fail.

What output do you see on the serial monitor? In case you get a "failed!" you should print out the string returned by ModbusRTUClient.lastError().

Hy!
When I dont set the DE and RE pin then it doesn't work because by default the libary use digital pin 2.
On my custom board I'm not using this pin.

The string returned by ModbusRTUClient.lastError() is: Time out. But when I use this function the dynamic memory will be low, the usage is mor than 80%.

I came to the following statement:
When I start to initialize VEML6030 the modbus stop working.
After I remove

 ALS.begin(); 
 ALS.PowerOn();

from slave code its work.

I think the ArduinoModbus and Wire libary not compatibile?
Any suggestion?

On my custom board I'm not using this pin.

That's the first time you mention that you use a custom board. Why didn't you post the schematics of that board?

Please add a wiring diagram of the setup!

The string returned by ModbusRTUClient.lastError() is: Time out. But when I use this function the dynamic memory will be low, the usage is mor than 80%.

The library you're using is designed to be used on ARM based Arduinos with much more RAM than the AVR type Arduino have. Do you use an ARM based Arduino?

I think the ArduinoModbus and Wire libary not compatibile?

I don't think so. My guess is a RAM problem.

pylon:
That's the first time you mention that you use a custom board. Why didn't you post the schematics of that board?

Please add a wiring diagram of the setup!

The library you're using is designed to be used on ARM based Arduinos with much more RAM than the AVR type Arduino have. Do you use an ARM based Arduino?

I don't think so. My guess is a RAM problem.

I use Atmega328 based board. When I use the VEML6030 libary and Modbus libary without ModbusRTUClient.lastError() function the memory usage is 17%. I think it is not a memory problem.
I attached the master and slave device schematic.

When I use the VEML6030 libary and Modbus libary without ModbusRTUClient.lastError() function the memory usage is 17%

Is the IDE telling you that? The IDE just calculates how much global variables are using. The ArduinoModbus library dynamically allocates much memory so the IDE won't tell you that value.

What reason do you have to strictly use that library and no other?

pylon:
Is the IDE telling you that? The IDE just calculates how much global variables are using. The ArduinoModbus library dynamically allocates much memory so the IDE won't tell you that value.

What reason do you have to strictly use that library and no other?

Yes the IDE telling this usage.
I searching an packet type commonucation libary over RS485.
I don't necessarily insist to Modbus, but I thought this was the most obvious.
Thank you for your answer, I think then Atmega328 is too little for this,

I don't necessarily insist to Modbus, but I thought this was the most obvious.

I wrote nothing against Modbus (I'm using it a lot in my own projects) but the library you've chosen is made for ARM based Arduinos. There are other libraries for Modbus that doesn't "waste" memory like the library you've used. I use a modified version of this library, here's the link to my fork if you're interested (branch "homebus").

Thank you for your answer, I think then Atmega328 is too little for this,

I don't think so, my whole home is running on about 60 ATmega328 connected by Modbus RTU (over RS-485).

pylon:
I wrote nothing against Modbus (I'm using it a lot in my own projects) but the library you've chosen is made for ARM based Arduinos. There are other libraries for Modbus that doesn't "waste" memory like the library you've used. I use a modified version of this library, here's the link to my fork if you're interested (branch "homebus").

I don't think so, my whole home is running on about 60 ATmega328 connected by Modbus RTU (over RS-485).

Thanks, I try it!