How to use modbus rtu to communicate esp8266 with PLC FX3u (slave)?

Hello everyone,
I have some issues when using esp8266 (nodemcu) reads data from D registers of PLC FX3u (slave) via modbus rtu (485) and create a web server. I have searched for it many days and I don't know how to do it.

Have anyone ever done this example? Can you help me?
Thank you!

Here is my code, I write similar to a source below

uint16_t au16data[16];
uint8_t u8state;
uint8_t u8query;
Modbus master(0,SerialPLC,13);
modbus_t telegram[6];
unsigned long u32wait;
uint16_t inputs[10];
uint16_t outputs[10];
uint16_t registers[20];

void initModbus()
{
    // telegram 0: read registers d10-15
    telegram[0].u8id = 1; // slave address
    telegram[0].u8fct = 3; // function code (this one is registers read)
    telegram[0].u16RegAdd = 10; // start address in slave
    telegram[0].u16CoilsNo = 5; // number of elements (coils or registers) to read
    telegram[0].au16reg = registers;

    // telegram 1: read registers d23
    telegram[1].u8id = 1;
    telegram[1].u8fct = 3;
    telegram[1].u16RegAdd = 23;
    telegram[1].u16CoilsNo = 1;
    telegram[1].au16reg = registers+5;

    // telegram 2: read registers d28-29
    telegram[2].u8id = 1;
    telegram[2].u8fct = 3;
    telegram[2].u16RegAdd = 28;
    telegram[2].u16CoilsNo = 2;
    telegram[2].au16reg = registers+6;

    // telegram 3: read registers d38-39
    telegram[3].u8id = 1;
    telegram[3].u8fct = 3;
    telegram[3].u16RegAdd = 38;
    telegram[3].u16CoilsNo = 2;
    telegram[3].au16reg = registers+8;

    // telegram 4: read registers d48
    telegram[4].u8id = 1;
    telegram[4].u8fct = 3;
    telegram[4].u16RegAdd = 48;
    telegram[4].u16CoilsNo = 1;
    telegram[4].au16reg = registers+10;

    // telegram 5: read registers d20-22
    telegram[5].u8id = 1;
    telegram[5].u8fct = 3;
    telegram[5].u16RegAdd = 20;
    telegram[5].u16CoilsNo = 3;
    telegram[5].au16reg = registers+11;

    SerialPLC.begin(19200);
    master.start();
    master.setTimeOut(5000);
    u32wait = millis() + 1000;
    u8state = u8query = 0;
}

void initWebServer()
{
    server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
    { 
        request->send(200, "text/html", index_html); 
    });

    server.on("/read", HTTP_GET, [](AsyncWebServerRequest *request)
    {
        String s = "{\"D10\":" + String(registers[0]) + ",\"D11\":" + String((registers[0]&2)>>1) + ",\"D12\":" + String((registers[0])>>2) + ",\"D13\":" + String(registers[3]) + ",\"D14\":" + String((registers[0]&16)>>4) + ",\"D28\":" + String(registers[6]) + "}" ;
        request->send_P(200, "text/plain", s.c_str()); 
    });

    server.on("/D10", HTTP_GET, [](AsyncWebServerRequest *request)
    {
        registers[0] = !registers[0];
        request->send_P(200, "text/plain", "OK"); 
    });

    server.on("/D11", HTTP_GET, [](AsyncWebServerRequest *request)
    {
        registers[1] = !registers[1];
        request->send_P(200, "text/plain", "OK"); 
    });
    server.begin();
}

source example: https://www.coder96.com/2022/06/web-server-plc-fx3u.html

The posted code is incomplete. Experience shows that the error is in that part of the code people are hiding from us.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.