I've had intermittent errors on modbus_read_registers
I'm reading just one register, and intermittently it returns -1
mb = modbus_new_tcp(ipAddr_.c_str(), port_);
if (mb == NULL){ return false; }
if (0 == modbus_connect(mb)){
modbus_set_response_timeout(mb, 4, 0);
initialized = true;
return true;
}
return false;
const unsigned INPUT_REG = 512;
uint16_t inputs = 0;
int ok = modbus_read_registers(mb, INPUT_REG, 1, &inputs);
if (ok != 1){
std::cout << "Error reading " << std::endl;
}
std::cout << "Read " << inputs << std::endl;
int bitmask_ = 8;
uint16_t bitsToSet = inputs;
// toggle bit
if (bitsToSet& bitmask_){
bitsToSet &= ~bitmask_;
}
else{
bitsToSet |= bitmask_;
}
const unsigned OUTPUT_REG = 512;
ok = modbus_write_registers(mb, OUTPUT_REG, 1, &bitsToSet);
if (ok != 1){
std::cout << "Error writing " << std::endl;
}
I've stepped into the code when this occurs. modbus_read_registers calls read_registers, which calls send_msg, which calls
rc = ctx->backend->send(ctx, msg, msg_length);
This send returns -1
Inside send_msg the error number is 0, "no error"
This happens with several similar, brand new, devices (digital input modules by Wago, Turk, and SMC)
OS= Win7
libmodbus 3.1.2
I can't step into send, so don't know what conditions it would return a -1
Just before this I was also getting an intermittent "bad file descriptor". I had to increase modbus_set_response_timeout to as much as 4 seconds to make this (largely) go away.
So, can anyone show me documentation for modbus_read_registers that indicates what a -1 return value means, with errno= 0 ?
Of course, I'd like to make it go away.