Error code from ArduinoModbus library

Hey everybody!

I’m trying to read out a conductivity sensor through a MKR-Board with the RS485 shield attached.
I’m using RTU-Modbus and the ArduinoModbus library.
The function “lastError()” returns “Connection timed out” error.
What does this mean? How can I fix it?

Thanks for the help!

#include <ArduinoModbus.h>

float temperature;
float humidity;

void setup()
{
  Serial.begin(9600);
  while (!Serial);

  Serial.println("Modbus conductivity Sensor");
  // start the Modbus RTU client
  if (!ModbusRTUClient.begin(38400))
  {
    Serial.println("Failed to start Modbus RTU Client!");
    while (1);
  }
}

void loop()
{
  // send a Holding registers read request to (slave) id 247, for 2 registers
  if (!ModbusRTUClient.requestFrom(247, HOLDING_REGISTERS, 0x00, 45))
  {
    Serial.print("failed to read registers! ");
    Serial.println(ModbusRTUClient.lastError());
  }
  else
  {
    // If the request succeeds, the sensor sends the readings, that are
    // stored in the holding registers. The read() method can be used to
    // get the raw temperature and the humidity values.
    short conductivity = ModbusRTUClient.read();

    // To get the temperature in Celsius and the humidity reading as
    // a percentage, divide the raw value by 10.0.
    Serial.println(conductivity);
  }
  delay(5000);
}

It sounds like the other device has not responded in time. Did you have a look at the code for ArduinoModbus to see under what circumstances this error is raised?

The function "lastError()" returns "Connection timed out" error. What does this mean? How can I fix it?

That usually means there's no connection to the slave device. That can have multiple reasons, some of them in hardware (we have no clue about your wiring or the about the slave device) or in software (did you check the serial parameters? Parity is a good candidate, official Modbus standard has a parity bit).

Did you have a look at the code for ArduinoModbus to see under what circumstances this error is raised?

I checked the documentation for the library here: Arduino - ModbusClientlastError
There’s no documentation where it explains which error is returned under which circumstances.

Here’s the wiring diagram I’m using. The sensor is a conductivity sensor.

sensor.png

mbe4ever:
I checked the documentation for the library here: Arduino - ModbusClientlastError
There’s no documentation where it explains which error is returned under which circumstances.

Here’s the wiring diagram I’m using. The sensor is a conductivity sensor.

faced this problem!Thanks for the solution!

There's no documentation where it explains which error is returned under which circumstances.

No, but you get the complete source of the library so you could check in what situation you get which error code.

The sensor is a conductivity sensor.

How about a link to it's manual?