Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modbus
#1
Hi!

anybody had implement Modbus ?
I'm having hard time to get mine working.
I'm using esp32ModbusRTU library.

here is my code;

esp32ModbusRTU modbus(&Serial1, -1); 


void modbusTask(void *pvParameters) {
  Serial1.begin(4800, SERIAL_8N1, RX, TX); 
  printf("Serial1 initialized for Modbus\n");
  vTaskDelay(pdMS_TO_TICKS(1000)); 

    // callbacks  Modbus
    modbus.onData([](uint8_t serverAddress, esp32Modbus::FunctionCode fc, uint8_t* data, size_t length) {
        printf("Modbus Response Received - Server ID: 0x%02x, Function Code: 0x%02x, Length: %u\n", serverAddress, fc, length);
        for (size_t i = 0; i < length; ++i) {
            printf("%02x ", data[i]);
        }
        printf("\n");
    });

    modbus.onError([](esp32Modbus::Error error) {
        printf("Modbus Error: 0x%02x\n", static_cast<uint8_t>(error));
    });

   
    modbus.begin();
  vTaskDelay(pdMS_TO_TICKS(1000)); 

   
    while (true) {
        modbus.readHoldingRegisters(0x01, 0x0000, 4); 
        //vTaskDelay(pdMS_TO_TICKS(1000)); 
    }
}

extern "C" void app_main(void)
{
  vTaskDelay(6000 / portTICK_PERIOD_MS);
  printf("Hello world!\n");
 
    xTaskCreate(modbusTask, "modbusTask", 8192, NULL, 5, NULL);

    while (true) {
        std::cout << "In the while\n" << std::endl;
        //vTaskDelay(pdMS_TO_TICKS(1000));
    }

}

...
#include <esp32ModbusRTU.h>
#include "driver/uart.h"

// Declaration Modbus
esp32ModbusRTU modbus(&Serial1, 10); // 10 is arbitary GPIO because don't use RTS pin


void modbusTask(void *pvParameters) {
// Configurer la communication UART pour Modbus
Serial1.begin(4800, SERIAL_8N1, RX, TX); // Initialise Serial1
printf("Serial1 initialized for Modbus\n");
vTaskDelay(pdMS_TO_TICKS(1000)); // stabilisation time

// Configurer les callbacks de Modbus
modbus.onData([](uint8_t serverAddress, esp32Modbus::FunctionCode fc, uint8_t* data, size_t length) {

printf("Modbus Response Received - Server ID: 0x%02x, Function Code: 0x%02x, Length: %u\n", serverAddress, fc, length);

if (length % 2 != 0) {
printf("Invalid data length\n");
return;
}

for (size_t i = 0; i < length; i += 2) {
uint16_t value = (data[i] << 8) | data[i + 1];
if(i < 5)
value = value / 10;
printf("Register %zu: %u \n", i / 2, value);
}

printf("\n");
});

modbus.onError([](esp32Modbus::Error error) {
printf("Modbus Error: 0x%02x\n", static_cast<uint8_t>(error));
});

// start Modbus
modbus.begin();
vTaskDelay(pdMS_TO_TICKS(1000)); // wait 1 sec to stabilise

while (true) {
modbus.readHoldingRegisters(0x01, 0x0000, 5); // slave address: 0x01
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

extern "C" void app_main(void)
{
xTaskCreate(modbusTask, "modbusTask", 8192, NULL, 5, NULL);

while (true) {
std::cout << "In the while\n" << std::endl;
vTaskDelay(pdMS_TO_TICKS(2000));
}

}
Reply


Messages In This Thread
Modbus - by charles.garand - 09-04-2024, 10:56 AM

Forum Jump:


Users browsing this thread:
1 Guest(s)