modbus-esp8266
modbus-esp8266 copied to clipboard
MODBUS_TCP
Hi,
i am trying to make MODBUS TCP SLAVE code on ESP32 platform. Code is developed in order to have several different RTOS Tasks . Also plan is to have dedicated task only for MODBUS_TCP. Please refer below extraction from compete code :
void setup() {
Serial.begin(9600); // Open serial communications and wait for port to open
delay(500);
Ethernet.init(5); // SS pin
Ethernet.begin(mac, ip, SubnetMask ); // start the Ethernet connection
//Ethernet.begin(mac, ip ); // start the Ethernet connection
delay(500); // give the Ethernet shield a second to initialize
modbus_TCP_init_val();
mb.server(502); // Listen TCP server
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// MODBUS TCP TASK ///////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
xTaskCreatePinnedToCore(
Task_MODBUS_TCP_Service
, "Task_MODBUS_TCP_Service"
, 8192 // Stack size
, NULL // When no parameter is used, simply pass NULL
, 0 // Priority
, &Task_mb_tcp_hdlr // With task handle we will be able to manipulate with this task.
, RUNNING_CORE_1 // Core 1 on which the task will run // pin task to core X
);
delay(500);
}
void loop() {
}
void Task_MODBUS_TCP_Service(void *pvParameters){ // This is a task.
(void) pvParameters;
while(1){ // A Task shall never return or exit.
mb.task();
vTaskDelay(pdMS_TO_TICKS(1));
}
}
During testing code is working only when mb.task(); is in viod loop(); part. When is in located in Task unfortunately is not running.
Kindly give me guidelines how to solve this issue that code will be functional when mb.task(); is located in Task. Thanks.
It's hard to say exactly without real code. In general the Modbus library is not thread safe so it will work unpredictable way if you access mb in separate thread from main loop. So some semaphoring or messaging required.