


How to ensure oncurrent TCP connection for ESPased programmable controller
Oct 19, 2024 pm 02:07 PMWhen designing the software system architecture of the simple programmable controller, I have the following main considerations:
(1) need to support TCP server as well as TCP client multiplexed concurrently, TCP server has three cases of connection, the browser downloads the built-in web page and the communication after that, for the communication connection of the third-party software, for the connection of Modbus-TCP protocol;
The TCP client is used to connect to the TCP server on the cloud server to realize remote access. Considering the resource situation of ESP8266, the concurrency is designed according to the specification that the TCP server supports four TCP client connection while one TCP client connects to the cloud server;
(2) It is necessary to control the flow of up to 5-way TCP/IP data communication and allocate enough time slices for PLC-related task scheduling to ensure reliable scheduling of PLC functions and real-time response;
3) The main tasks are the LWIP task and the main task, the priority of the LWIP task is lower than that of the main task, and at the same time ensure that the main task does not have a long time-consuming or blocking task, and the main task is scheduled in 10ms tick, and it actively enters the blocking state after the execution of the main task, releasing the CPU for scheduling other tasks;
Combining these, I designed the nearby software architecture;
When TCP server or TCP client receives data, the task of LWIP executes the callback function for data reception;
In the callback function, the program just stores the received data into the receive buffer, but does not process the data;
There are five receive buffer, each used for five concurrent connections; when deposited into these buffer, the TCP packet unpacking and sticky packet situation is handled at the same time, HTTP protocol data is unpacked through the end of the rnrn characters;
When new data is received, the task of LWIP wakes up the main task to process it by signaling Semaphore.
Each time the main task detect status of these 5 buffers, and if there is received data, it processes the relevant data and generates a response back to the sender;
In order to ensure that all 5 connections have the same chance to be processed, the main task takes turns to prioritize the state of these 5 caches.
For example, if the scheduler determines the cache for connection 0 first, then the next scheduler determines the cache for connection 1.
This can be achieved with a simple code;
signed short http_get_recvs(U32 *msg, U32 *addr, U16 *port){
U8 i;
signed short res = -1;
for(i = 0; i< TCPSERVER_CLIENT_NUM; i )
{
if(tcpclient_curquery_client >= TCPSERVER_CLIENT_NUM){ tcpclient_curquery_client = 0; } if(tcpclient_info[tcpclient_curquery_client].used == TRUE){ if(tcpclient_info[tcpclient_curquery_client].received){ *msg = (U32)tcpclient_info[tcpclient_curquery_client].recvbuff; *addr = tcpclient_info[tcpclient_curquery_client].ipaddr; *port = tcpclient_info[tcpclient_curquery_client].port; res = (signed short)tcpclient_curquery_client; break; } } tcpclient_curquery_client++; } return(res);}
The above is the detailed content of How to ensure oncurrent TCP connection for ESPased programmable controller. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Yes, function overloading is a polymorphic form in C, specifically compile-time polymorphism. 1. Function overload allows multiple functions with the same name but different parameter lists. 2. The compiler decides which function to call at compile time based on the provided parameters. 3. Unlike runtime polymorphism, function overloading has no extra overhead at runtime, and is simple to implement but less flexible.

C has two main polymorphic types: compile-time polymorphism and run-time polymorphism. 1. Compilation-time polymorphism is implemented through function overloading and templates, providing high efficiency but may lead to code bloating. 2. Runtime polymorphism is implemented through virtual functions and inheritance, providing flexibility but performance overhead.

Yes, polymorphisms in C are very useful. 1) It provides flexibility to allow easy addition of new types; 2) promotes code reuse and reduces duplication; 3) simplifies maintenance, making the code easier to expand and adapt to changes. Despite performance and memory management challenges, its advantages are particularly significant in complex systems.

C destructorscanleadtoseveralcommonerrors.Toavoidthem:1)Preventdoubledeletionbysettingpointerstonullptrorusingsmartpointers.2)Handleexceptionsindestructorsbycatchingandloggingthem.3)Usevirtualdestructorsinbaseclassesforproperpolymorphicdestruction.4

People who study Python transfer to C The most direct confusion is: Why can't you write like Python? Because C, although the syntax is more complex, provides underlying control capabilities and performance advantages. 1. In terms of syntax structure, C uses curly braces {} instead of indentation to organize code blocks, and variable types must be explicitly declared; 2. In terms of type system and memory management, C does not have an automatic garbage collection mechanism, and needs to manually manage memory and pay attention to releasing resources. RAII technology can assist resource management; 3. In functions and class definitions, C needs to explicitly access modifiers, constructors and destructors, and supports advanced functions such as operator overloading; 4. In terms of standard libraries, STL provides powerful containers and algorithms, but needs to adapt to generic programming ideas; 5

Polymorphisms in C are divided into runtime polymorphisms and compile-time polymorphisms. 1. Runtime polymorphism is implemented through virtual functions, allowing the correct method to be called dynamically at runtime. 2. Compilation-time polymorphism is implemented through function overloading and templates, providing higher performance and flexibility.

C polymorphismincludescompile-time,runtime,andtemplatepolymorphism.1)Compile-timepolymorphismusesfunctionandoperatoroverloadingforefficiency.2)Runtimepolymorphismemploysvirtualfunctionsforflexibility.3)Templatepolymorphismenablesgenericprogrammingfo

C polymorphismisuniqueduetoitscombinationofcompile-timeandruntimepolymorphism,allowingforbothefficiencyandflexibility.Toharnessitspowerstylishly:1)Usesmartpointerslikestd::unique_ptrformemorymanagement,2)Ensurebaseclasseshavevirtualdestructors,3)Emp
