I am trying get register value from Modbus slave machine.
I use esp8266 and modbus-esp8266 library with Arduino IDE.
My Slave Machine register address:
40001
40002
40003
40004
40005
40006
40007
40008
40009
40010
40011
40012
40013
40014
40015
40016
I need read 03 READ HOLDIG REGISTER .
I use this example here:
How can i get register value in this procedure
bool cbWrite(Modbus::ResultCode event, uint16_t transactionId, void* data)
This example reads multiple address but i dont know how i can use it
please give me sample for me. how can i move to
to integer variable?
This is example I use:
ModbusRTU ESP8266/ESP32
Read multiple coils from slave device example
(c)2019 Alexander Emelianov ([email protected])
https://github.com/emelianov/modbus-esp8266
modified 13 May 2020
by brainelectronics
This code is licensed under the BSD New License. See LICENSE.txt for more info.
#include <ModbusRTU.h>
#if defined(ESP8266)
#include <SoftwareSerial.h>
// SoftwareSerial S(D1, D2, false, 256);
// receivePin, transmitPin, inverse_logic, bufSize, isrBufSize
// connect RX to D2 (GPIO4, Arduino pin 4), TX to D1 (GPIO5, Arduino pin 4)
SoftwareSerial S(4, 5);
#endif
ModbusRTU mb;
bool cbWrite(Modbus::ResultCode event, uint16_t transactionId, void* data) {
#ifdef ESP8266
Serial.printf_P("Request result: 0x%02X, Mem: %d
", event, ESP.getFreeHeap());
#elif ESP32
Serial.printf_P("Request result: 0x%02X, Mem: %d
", event, ESP.getFreeHeap());
#else
Serial.print("Request result: 0x");
Serial.print(event, HEX);
#endif
return true;
}
void setup() {
Serial.begin(115200);
#if defined(ESP8266)
S.begin(9600, SWSERIAL_8N1);
mb.begin(&S);
#elif defined(ESP32)
Serial1.begin(9600, SERIAL_8N1);
mb.begin(&Serial1);
#else
Serial1.begin(9600, SERIAL_8N1);
mb.begin(&Serial1);
mb.setBaudrate(9600);
#endif
mb.master();
}
bool coils[20];
void loop() {
if (!mb.slave()) {
mb.readCoil(1, 1, coils, 20, cbWrite);
}
mb.task();
yield();
}
Example address is :Link
question from:
https://stackoverflow.com/questions/66050167/esp8266-modbus-example-callback-procedure-get-value 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…