Complete asynchronous Implementation of serial port operations

Source: Internet
Author: User

This weekArticle Asynchronous Implementation of serial port operationsSome problems have been found during serial port applications such as RFID, bar code, and printing in the project:

1. Clear useless data in the serial port before using the serial port;

2. Improved methods for reading data from the serial port;

3. Data Parsing

4. Send

5. resource usage

6. logout and resource release

 

1. Clear useless data in the serial port before using the serial port;

One way is to clear useless data before the commhandler thread loops into; one way is to clear useless data when the serial port is opened; because the startup time of the commhandler thread is unknown, clearing in commhandler may cause useless data to be cleared together;

 

2. Improvement in reading data from the serial port;

The previous method was to read data and press it into the queue, and then notify the upper layer of the data. This approach has the drawback that a complete piece of data may be divided into multiple segments; the improvement method is to add a loop in commhandler. After no data is available, it indicates that the upper layer is notified after a complete piece of data is received;

This processing method has limitations and is not suitable for receiving a large amount of continuous data. For my applications, it is very effective to send and add a small amount of reception;

 

Iii. Data Parsing

The data received from the serial port is parsed by the upper layer. different applications and protocols are different. This part of work should be done at the upper layer. For a string of data, parsing is usually used to extract the first and last signs or special characters from the string. It is very suitable to use a queue to handle this situation;

If the protocol to be processed is complicated, you can also separate the data processing and views. In this way, the structure is clear and the data processing part can be directly written into a database. When I am processing the barcode symbol se955 protocol, this is the case;

 

4. Send

Sending time: when sending data through the serial port, select the appropriate time; otherwise, a deadlock may occur;

Change of the sending interface: Change the sending method from send (byte []) to send (byte [], offset, Len). The advantage is that you can send a piece of data in a byte stream;

 

5. Resource occupation

For serial port operations without other data processing, such as bar code and RFID applications, queueeventhandler can be allowed to occupy processor resources continuously, A large amount of data processing needs to be done at the same time, such as printing. queueeventhandler is a waste of resources for loop query;

The solution is to add wait to queueeventhandler, and then use an external call to call notify;

 

6. Exit and release resources

To release resources appropriately, a flag is added to commhandler and queueeventhandler, and a flag is added between commhandler's two wait events to avoid exit, notify is blocked again, and data in the serial port must be cleared for later use. Avoid exceptions during the close of the serial port and between the commhandler and queueeventhandler threads;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.