Linux Read serial data __linux

Source: Internet
Author: User
First, the preface

The recent project needs to read the serial data in the Linux system, because the use of the Ubantu system, the development tool is QT Creator, so first on the internet Baidu a bit, found a few blogs, roughly the same content. Due to the use of QT4, it does not have its own serial port class (QT5 has its own serial, can be used directly), the powerful QT is its open source and Cross-platform, for QT4 in Linux, the serial port program, the Internet has a third-party library, that is, Qextseriaport class, Single most bloggers write about the use of Qextseriaport classes are for the Windows platform, since QT is cross-platform, then Qextseriaport must be supporting the Linux platform, on the Internet to find a bit, Find a blog for linux application Qextseriaport, based on Linux (Fedora 17) of the QT serial communication example, this article is mainly aimed at this article, according to the experiment in the process of some of the problems arising from the summary, Further detailed explanation uses the Qextseriaport class in the Linux system, carries on the serial port data reads.


Ii. Background to the problem

roughly: There is a sending end periodically to the local serial port to send data, the machine needs to read the serial port data, analysis, get the information they want. Actual description: Because it is to do intelligent driving, need to read the speed of vehicles, through the OBD equipment read and analyze the speed of vehicles, and then sent to the PC via Bluetooth serial port (also through a Bluetooth module to receive), the PC end of the serial port to receive serial data and analysis.

In the course of the above, there is a problem to note: The sender is the periodic non-stop sending data, that is, the PC serial port has been data in, the most common is the transmitter end frequency than the receiver, that is, ready to read the serial data can always read the data, but also caused another problem, That is how to deal with the receiver side of the serial buffer, because every time you read the operation must be the current serial port buffer to read all the data, can not read only a part, or the sender has been data come in, and you do not read every time, you will cause a buffer overflow.


third, what is the essence of the serial port

Any contact with Linux system knowledge or understand the principles of computer systems, then you must pass a sentence " under any operating system, everything is a file ," Linux system is the most perfect implementation of this sentence, that is, under the Linux system, you can completely take everything as a file , that is, all things can be "open--set-read-write-closed" mode , the same, in the Linux serial port can also be seen as a file (in fact, any operating system can be so thought), you have to read and write serial data, you must first open, set the serial port, Then read and write, read and write, you must close the serial port.


four, Ubantu qt serial data reading program

First of all, you can first look at the Linux (Fedora 17) of the QT serial communication instances, know a general flow, and according to the requirements of download Qextseriaport source code, copy the corresponding files to their engineering directory, you can according to the code he said to write, In fact, this is just a simple demo, you can write a slightly more complex.

After reading his code, perhaps you already understand roughly, his operation process is completely according to "open-set-read-write-close" operation mode to operate the serial port, its "open and setup" process is in the MainWindow constructor, "read" Operation is actually a function readall (), in order to achieve timed reading, need to connect with a qtimer, in fact, you can not use the timer (in his description is said to use the timer), the function of what the timer. is not timed to trigger its associated function. The essence is that you have to call the ReadAll () function on a regular basis, and you can control the timing by yourself, that is, you can call the ReadAll () function periodically in your main function, and the most common, if you want to keep the degree, The ReadAll () function is called in the while (1) loop, and the "close" operation is called in the MainWindow destructor.

Because my QT project is relatively large, it's not just one or two compiler modules, so I'm calling the ReadAll () function, and I'm not associating with Qtimer because I have a master program (in fact, the Mian function), so the ReadAll () function called in the main control program, To realize the periodic reading of serial data, I read the vehicle speed file as follows:

ReadCarSpeed.h

#ifndef readcarspeed_h
#define Readcarspeed_h

void opencom ();   Open, set serial port

void Readspeed ();     Read, parse data, shutdown is also called
#endif//Readcarspeed_h when exiting the interface
ReadCarSpeed.cpp

#include "ReadCarSpeed.h" #include "posix_qextserialport.h" extern int carspeed;

Posix_qextserialport *mycom; void opencom () {//defines the serial port object and passes the arguments to initialize it in the constructor mycom = new Posix_qextserialport ("/dev/ttyusb1", Qextserialbase::P oll

    ing);     Mycom->open (Qiodevice::readwrite);     To read and write to open the serial port/set serial port Parameters mycom->setbaudrate (BAUD9600);     Baud rate setting, we set to 9600 mycom->setdatabits (data_8);     Data bit setting, we set to 8 bit data bit mycom->setparity (Par_none);     Parity setting, we are set to Mycom->setstopbits (Stop_1) without verification;     Stop bit setting, we set to 1 bit stop bit mycom->setflowcontrol (Flow_off);     Data flow control settings, we are set to no data flow control mycom->settimeout (50);     Delay setting, set to delay 20ms} void Readspeed () {Qbytearray temp = Mycom->readall ();      Reads all data from the serial buffer to the TEMP//Temporary variable temp//temporary variables to integer variable carspeed qbytearray c = "km/h"; The data sent by the sender is "nkm/h", where n indicates the actual speed, because the transmitter end frequency is faster than the receiver, so each call ReadAll () read the data is more than one consecutive "nkm/h" string, like "nkm/hnkm/hnkm/hnkm/hnkm/ hnkm/hnkm/h "//As I need to be the latest vehicle speed, so only need to read the string in the last speed word subcode strings, using QbytearrAY member function LastIndexOf can be implemented, its return value is the last km/h position, this is a C + + string search knowledge point//BO friends can read C++primer (Fifth edition P325) int index = Temp.lastinde     XOf (c);     Returns the last C-match position Qbytearray speedstr = Temp.mid (index-1,1);    Get speed Information carspeed = Speedstr.toint (); Convert to integral type, assign to global variable carspeed (is a global variable)}
(1) Open the serial port:

In the ReadCarSpeed.cpp file, the opencom () function is responsible for opening and setting the serial port, there is a bit to be verbose : mycom->settimeout (50); Delay setting function, I set this here 50ms, For this delay, my understanding is that the local serial port reception frequency, note that the reception frequency is different from the frequency of your reading, where the reception frequency is the local serial port buffer reception data delay, that is, no 50ms has a data into the receiving buffer queue (to understand that the data buffer is actually a queue, FIFO), and the frequency you read is the frequency with which you call the ReadAll () function, such as my reading frequency is the frequency of the master program (about 500ms), so read approximately 10 speed information at a time. (2) Data reading and parsing:

The Readspeed () function first calls the ReadAll () function to read all the data in the buffer, and then parses the data, which says that each read operation reads approximately 10 speed information (or more), and I need the latest speed information. And the latest speed information in the buffer queue is the last one in the queue, so only the last speed in the Qbytearray is extracted, which is achieved by character search (matching) in C + +, and can be accessed in C++primer (Fifth edition P325). Here Qbytearray is a byte array that can store any data and use it to view the QT Assistant document.

In short, in my application, the first is to call the Opencom function, open and set the serial port, and then periodically call the Readspeed function (the essence is the periodic call ReadAll function) can read the latest speed information.






Related Article

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.