Closed noir235 closed 5 years ago
Hello,
I suggest to write something like below code. From one hand it allows to see request result (timeout or any kind of modbus error - see Modbus.h)
#include <ModbusRTU.h>
uint32_t res = 0;
bool flag = false;
ModbusRTU mb;
bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) {
Serial.printf("Result code: %02X\n", event);
flag = true;
return true;
}
void setup() {
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N2, 16, 17);
mb.begin(&Serial2);
mb.master();
}
void loop() {
mb.readHreg(0, 0, (uint16_t*)&res, 2, cb);
while (!flag) {
delay(100);
mb.task();
}
res = (res>>16) | (res<<16);
Serial.println(res);
delay(10000);
}
From other hand mb.task() in your code called to early to get any responce from slave. You may try at least
mb.readHreg(0, 0, (uint16_t*)&res, 2, nullptr);
delay(100);
mb.task();
Hello togehter,
First of all, I'm a newbie in programming ESP32 with Arduino IDE. My target is to read the air flow from the a sensor with the modbus protocol.
I found out the following details for Modbus-configuration:
Transmission speed: 9600 BAUD Start bit: 1 Data bits: 8 Stop bit: 2 parity: no adress: 247 (no LED reaction on sensor fo communcation, I used 0) Function: 03 (read holding register) register: 0x0000 - 0x0001 value-format: float32
I use this code with Arduino IDE on M5Stack-Core with PLC-module:
include
uint32_t res = 0; ModbusRTU mb;
void setup() { Serial.begin(115200); Serial2.begin(9600, SERIAL_8N2, 16, 17); mb.begin(&Serial2); mb.master(); }
void loop() { mb.readHreg(0, 0, (uint16_t*)&res, 2, nullptr); mb.task(); delay(100); res = (res>>16) | (res<<16);
Serial.println(res); }
Can you help me with that issue?