modbus-esp8266 icon indicating copy to clipboard operation
modbus-esp8266 copied to clipboard

mb.readHreg function sometimes does not get a value from the slave device

Open arimukhlas opened this issue 11 months ago • 1 comments

Hi @emelianov thanks for sharing great modbus library

mb.readHreg function sometimes does not get a value from the slave device, How can I make it so that when it fails to get a value, it will call the function again several times, to ensure that the function always gets a value?

this is some of my sketch

void loop() {
  if ((unsigned long)(millis() - previousMillisSendData) >= 5000) {   
    reg32bit(); 
    previousMillisSendData = millis();
}

bool cbWriteTotalizer(Modbus::ResultCode event, uint16_t transactionId, void* data) {
  if (event != Modbus::EX_SUCCESS) {
    Serial.print("Fail Totz : 0x");
    Serial.println(event, HEX);
    buffTotalizer = 0;
    Serial.println(buffTotalizer);
  }
  else  {
    buffTotalizer = reunite32(regBuff3[0],regBuff3[1]);
    Serial.println(buffTotalizer);
  }
  return true;
}

void reg32bit(){
  uint16_t regBuff[2];
  if (!mb.slave()) {
    mb.readHreg(1, 3017, regBuff, 2, cbWriteTotalizer);
    while(mb.slave()) {
      mb.task();
      delay(20);
    }
    regBuff3[0] = regBuff[0];
    regBuff3[1] = regBuff[1];
  }
}

uint32_t reunite32 (uint16_t i, uint16_t j){
  uint32_t m = (uint32_t)(i & 0xFFFF)<<16|j;
  return m;
}

and this is the result image

give me some hint to get rid of the error reading that appears uncertainly, thanks

arimukhlas avatar Nov 25 '24 09:11 arimukhlas