The application of multithreaded serial communication technology in GPS navigation
Abstract: GPS navigation system needs to obtain the positioning data from the serial port of the receiver in real time. While monitoring the serial port in real time, it can also carry out some other operations in the foreground. This problem can be well solved by using the thinking and method of serial communication programming based on multithreading. This paper introduces the basic concept of multithreading and serial port communication programming technology, analyzes the functions and requirements of GPS navigation system, emphatically expounds the serial port communication method based on multithreading CSerialPort class to obtain positioning data, and gives the use method of CSerialPort class with a sales revenue of 15billion yuan. After debugging, the program runs smoothly
Keywords: multithreading; Serial communication; CSerialPort; GPS navigation
0 introduction
gps (Global Position System) is a positioning, timing and speed measurement system with global and all-weather advantages. Users use GPS receivers to receive the signals transmitted by satellites, so as to obtain the geodetic coordinates, elevation, time and other information of the current position, so as to achieve the purpose of positioning, navigation or elevation measurement. Satellite navigation and positioning technology is widely used in marine survey, marine engineering, marine development and military operations. Its excellent characteristics of high precision, fast and convenient, all-weather and so on make it more and more popular. In GPS navigation, it is necessary to collect GPS data complying with NMEA0183 protocol in real time. After processing the data, the user's location, time, speed and other information are stored in the database through ODBC interface, so as to provide basis for real-time display of target location on electronic map in the future. In order to avoid thread blocking caused by waiting for serial port I/0 operation, the program is required to carry out data extraction, storage, display and other operations in the foreground while monitoring the serial port in real time. In order to solve the real-time and multi task processing and avoid a task occupying CPU for a long time, multi-threaded programming is an ideal choice
1 overview of multithreading
1.1 basic concepts
a process is an execution instance of a program on a computer. A thread is an execution branch in a program. Multithreading means that multiple tasks can be executed simultaneously in the same program. Each process has at least one main execution thread, which does not need to be actively created by the user, but is automatically created by the system. Users create other threads in the application as needed, and multiple threads run in the same process concurrently
all threads in a process are in the virtual address space of the process and use these virtual address spaces, global variables and system resources together
1.2 vc
vc++ environment support for multithreading technology in visual c++6.0, MFC class library provides support for multithreaded programming, which makes multithreaded programming more convenient. There are two kinds of threads in MFC, which are called worker threads and user interface threads respectively. The main difference between the two is that worker threads do not have message loops, while user interface threads have their own message queues and message loops. Worker threads are usually used to perform background computing and maintenance tasks. User interface threads are generally used to process user input independent of other threads, and respond to events and messages generated by users and systems
1.3 thread creation, suspension, recovery, termination
in MFC, the global function afxbeginthread() is generally used to create and initialize the operation of a thread. This function has two overloaded forms, which are used to create worker threads and user interface threads respectively. Sus pendthread() and resumethread() are used to suspend the specified thread and resume the thread suspended with suspendthread() respectively. Exit thread (DWORD dwexitc0de) is used for a thread to terminate its own execution
1.4 thread synchronization
threads often access some resources at the same time, so it is inevitable to access shared resources and cause conflicts. In order to solve this resource conflict problem, the concept of thread synchronization must be introduced. The Win32 API provides a variety of synchronization control objects to solve shared resource access conflicts, including using critical areas, using mutually exclusive objects, using semaphores, and using events
2 serial communication programming
at present, when programming under windows, there are three common methods of serial communication: using msc0mm communication control; Programming with Windows API; Use some serial communication classes provided by the third party to write
(1) MSComm control
using MSComm control will make programming fast and simple. However, due to a large number of packaging, the controllability and flexibility of programming are reduced. Therefore, many complex processing needs to be done in multithreaded and multi serial programming
(2) windows API
when using Windows API to write serial port programs, especially complex multithreaded serial port programs, it requires higher programming ability of programmers. In addition to the need for programmers to master and use many API functions and be able to write many underlying code, they must also be familiar with the programming method of threads
(3) third party serial port communication class
when using the serial port communication class of the third party for serial port programming, it can not only make the programming efficiency high, the program controllability strong, but also simpler than window API programming. Among them, the serial port communication class of the most used third party is cserial port. Based on multithreading, it is a packaged class of Win32 API, which encapsulates the Win32 API class dealing with serial ports. With this class, it is easy to operate serial ports and realize multithreaded serial port communication. The program written can run well under Windows 98/NT/2000/XP operating system
comparing the three serial port communication methods, we can find that using the third-party serial port communication class CSerialPort is a better choice to realize multithreaded serial port programming under windows
3 multi thread programming technology is that there is no new break in the application of GPS data acquisition system
3.1 function analysis of GPS navigation system
gps navigation gives the user's location in real time through GPS positioning technology, which requires real-time receiving of positioning data from the serial port of GPS receiver, and data storage, display, etc. while monitoring the serial port in real time, Using multithreaded serial communication technology will solve this problem well. Through the analysis of GPS navigation system, the program is divided into the following threads:
main thread: responsible for handling the message processing of the user interface, and scheduling other threads to process data according to the predefined process
serial port monitoring thread: monitor the serial port, collect data and save the data to a buffer
inbound thread: read data from the buffer, process it accordingly, and store the processed data into the database
display thread: display the user's real-time position on the electronic map through the map matching algorithm
gps navigation system block diagram is shown in Figure 1
3.2 specific implementation
the system first explains the thread in the corresponding header file, and then adds people to create program code during program initialization, so that after creation, the thread can execute concurrently with the main thread. The main thread, storage thread and display thread are the same as the general programming processing, so the following focuses on the serial port monitoring thread
CSerialPort class based on Multithreading programming is used for serial port operation, and its workflow is as follows: first set the serial port parameters, and then start the serial port monitoring worker thread. After the serial port monitoring worker thread monitors the data flow, control events or other serial port events received by the serial port, it notifies the main program in the form of a message and triggers the message processing function to process data, which is for receiving data; Sending data can be sent directly to the serial port. The application flow is shown in Figure 2
the programming steps are as follows:
(1) establish a program
establish a MFC application cserial porttest based on a single document, and other steps remain in the default state
(2) add class files
copy the serialport.h and serialport.cpp class files to the project folder, and people's lives will be better with project ad. the D to project files command adds the above two files to the project, and adds include automatic shutdown to any module that wants to call this class; De serialport.h file
define the object of serial port class in the view class header file: cserialportm_ Port。
(3) manually add the serial port message response function oncommunica tion (wParam ch, lParam port)
first add the serial port character to cserialporttestview.h to receive the message WM_ COMM_ Rxchar (there is a character in the serial port receiving buffer) response function declaration, that is:
after the main thread initializes the serial port, create the commthread function to enter the dead loop, and the thread has been monitoring the serial port event. When the serial port reading event occurs, read the data received by the serial port, and send a custom message WM to the main thread_ Comm rxchar, notify the main thread to process data in the corresponding message response function. When receiving the write serial port command from the main thread, write the data in the cache to the serial port
(5) perform data processing in the oncommunication() function. Whenever there is a character in the serial port receiving buffer, a WM comm rxchar message is generated, triggering the oncommunica() function. At this time, the corresponding data processing can be carried out in the function to extract the key positioning data such as time, latitude and longitude, speed, and then save these data to the database
4 conclusion
serial communication is widely used in the field of communication. CSerialPort, a third-party serial VI communication class based on multithreading, solves the problems of thread blockage caused by the long-time CPU occupation of the serial port. The programming is simple, convenient and portable, and can be used for other types of serial port communication problems. The program is compiled by Microsoft Visual c++6.0 and runs under Windows XP
LINK
Copyright © 2011 JIN SHI