C# serial communication from entry to proficiency (27) - solving the problem of slow data processing under high-speed communication (within 20ms)

Insert image description here

Preface

When we develop serial communication programs, we sometimes encounter data sent by microcontrollers or sensors at very fast speeds, such as once every 10ms or 20ms, and the amount of data sent each time is relatively large. If we follow the routine In the way of writing, we will find that the received data has not been processed yet, and new data has been sent. This will cause the data processing to lag, and the software will not always process the latest data. This article is to introduce how to solve the problem of data processing under high-speed communication. Slow problem,After reading the content of this article, you will know how to solve any problem of slow data processing under high-speed communication in the future. Whether it is serial communication or socket communication, it will not be a problem. .

1. Common wrong writing methods

We know the reception of serial port data. During high-speed communication, the sender keeps sending data, so we use the passive method of receiving data in development, that is, using the DataReceived event trigger of the serial port to receive data. , the conventional writing method is as follows:
Insert image description here
As can be seen from the above figure, in the conventional writing method, we receive data when the DataReceived event is triggered, and after receiving the data, we then process the data, process the data and then continue Receive data, so the data processing process is single-threaded, as follows:
Receive data-》Process data-》Receive data
Then it is easy to see that processing data It will take up the time to receive data, which will cause the data to be processed too late when the amount of data received is particularly large.

Guess you like

Origin blog.csdn.net/qq_34059233/article/details/134577268
Recommended