Closed pllagunos closed 3 weeks ago
Main point is that mb.readHreg()
just sends request and returns. That is query runs async. Response processed bt mb.task()
. mb.slave()
returns true
while request is in-progress.
Simplest way to resolve you problem is to wait the response (or timeout)
// Read PV & SP registers from the slave
if (mb.readHreg(SLAVE, 1, res, 2)) {
while (mb.salve()) { // Wait response received
yield();
mb.task();
}
PV = res[0] / 10.0;
SP = res[1] / 10.0;
Serial.printf("PV: %.2f\n", PV);
Serial.printf("SP: %.2f\n", SP);
}
else {
Serial.println("Failed to read PV and SP");
}
// Read single register from the slave, address 3
uint16_t var;
if (mb.readHreg(SLAVE, 3, &var, 1)) {
while (mb.salve()) { // Wait response received
yield();
mb.task();
}
Serial.printf("var: %d\n", var);
}
else {
Serial.println("Failed to read var");
}
I want to be able to read holding registers in "blocks" (i.e a group of registers) and also as single registers. Here's my attempt at doing so:
Slave code on an ESP32 s3
Master code on a ESP32 devkit
The result is this: 15:35:29.870 -> PV: 6.80 15:35:29.870 -> SP: 1.10 15:35:29.870 -> Failed to read var 15:35:31.348 -> PV: 0.30 15:35:31.348 -> SP: 4.80 15:35:31.348 -> Failed to read var 15:35:32.858 -> PV: 0.60 15:35:32.858 -> SP: 0.10
If I comment the code related to reading PV and SP, var is succesfully read: 15:33:25.531 -> var: 76 15:33:27.043 -> var: 51 15:33:28.555 -> var: 88 15:33:30.033 -> var: 48
I tried adding a delay between the two but it doesn't work.