arduino-libraries / ArduinoModbus

244 stars 116 forks source link

Not able to read Holding registers more than 15. RX Buffer issue? #126

Closed codev123 closed 7 months ago

codev123 commented 9 months ago

I am using ArduinoModbus library with RP2040 Pico with Arduino Pico core.

I am able to communicate with Modbus slave and client using this library, the limitation I am facing is that I need to read around 100 holding registers from energy meter slave and process that data to serve it on to Modbus TCP server. but i can only read up to 14 holding registers using Modbus RTU client. please guide me how to fix the issue.

const int numHoldingRegisters = 100;
void readHoldingRegisterValues() {
  Serial.print("Reading Holding Register values ... ");

  // read 10 Input Register values from (slave) id 42, address 0x00
  if (!ModbusRTUClient.requestFrom(42, HOLDING_REGISTERS, 0x00, numHoldingRegisters)) {
    Serial.print("failed! ");
    Serial.println(ModbusRTUClient.lastError());
  } else {
    Serial.println("success");

    while (ModbusRTUClient.available()) {
      Serial.print(ModbusRTUClient.read());
      Serial.print(' ');
    }
    Serial.println();
  }

  // Alternatively, to read a single Holding Register value use:
  // ModbusRTUClient.holdingRegisterRead(...)
}

the error shows:

..............   failed! Connection timed out
pomplesiegel commented 7 months ago

Hi @codev123, thanks for making the issue! What baud rate and microcontroller + additional hardware are you using?

I'm wondering if this is the same behavior as the more generalized issue I'm seeing on my RP2040: https://github.com/arduino-libraries/ArduinoModbus/issues/125

Thanks!

pomplesiegel commented 7 months ago

@codev123, any update / status + additional info? Thanks

codev123 commented 7 months ago

I have increased serial buffer size for uart to 256 bytes and issue is resolved. Thankyou sir

pomplesiegel commented 7 months ago

@codev123, thanks for the update! Can you specify which specific file / line you changed for this? Thank you!

AlexanderTonn commented 7 months ago

Please consider that Modbus RTU requires a termination resistor at the end of the line. i personally don't have problems with the buffer size. I used the Arduino Opta RS485 and i read currently 30 registers without problems.

Probably it can be helpful: I currently writing a class for reading and writing registers of a mppt charger.

codev123 commented 7 months ago

@codev123, thanks for the update! Can you specify which specific file / line you changed for this? Thank you!

I have used serial1 on my pico board, added this line in setup() code:

Serial1.setFIFOSize(256);

And it works fine.

Issue with library is that it doesn't fix modbus response buffer size, it's microcontroller specific.