Skip to content

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

Open
@arimukhlas

Description

@arimukhlas

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions