Application Introduction of VC Multithread Serial Communication Programming Technology in GPS Navigation

GPS (GlobalPositioningSystem) is also called global positioning system. GPS positioning is a positioning method that determines the user's position by measuring the distance between the user and the satellite, and is widely used in countries around the world. Throughout the application of GPS, it is always closely connected with communication technology. From satellite ephemeris broadcasting to differential information transmission, and real-time reading of data from GPS receivers are all related to communication. The serial communication technology is used to read data from the GPS receiver in real time. The serial communication technology is generally used in real-time processing. GPS navigation is to give the user's location in real time through GPS positioning technology, which requires obtaining positioning data in real time, and now generally uses asynchronous serial communication method. The GPS receiver and computer communicate in a serial manner. This communication method uses fewer data lines, but its transmission speed is slower than parallel. Using multi-threading technology, that is, setting up a secondary thread for reading data, this can avoid the main program deadlock when reading and writing is blocked.

2 Basic Principles of Serial Communication The essential function of the serial port is to act as a code converter between the serial devices of the CPU. When data is sent from the CPU through the serial port, the byte data is converted into serial bits. When receiving data, serial bits are converted into byte data. In the Windows environment (Windows NT, windows 98, Windows 2000), the serial port is part of the system resources. Application level uses serial port for communication. Before using it, you must submit a resource request to the operating system (open the serial port). After the communication is completed, you must release the resource (close the serial port).

3 The implementation process of reading data using multi-thread serial port programming 32-bit serial port communication can be achieved in two ways: using ActiveX control and AH communication function. Here mainly introduces the use of API communication function, because the use of API communication function is more complicated but more flexible than the control.

Local variables can also be defined as class variables in their own defined classes, and are referenced by class objects.

2002.6 / Global Positioning System 21 can choose to allow reading and writing // sharing mode, this item must be NULL, // pointer to security attribute OPENEXISTING, // set the generation method FILEFLAGOVERLAPPED, // I am going to use asynchronous communication, I must set NULL like this ); // The template file handle must be NULL. The function above is to open the COM2 serial port with asynchronous communication and return the serial port handle. If you want to open COM1, replace COM2 with COM1.

After opening the serial port, you also need to check whether the serial port is successfully opened. If the opening is successful, set the serial port, otherwise exit.

The following parameter settings are to reset the original parameters of the serial port. deb.BaudRate = 9600; // The baud rate of the read data can be changed, such as 4800.

// Parity check control, where the parity is invalid, the above parameters should be selected according to the communication protocol.

Set up the receiver Set up the receiver, that is, write commands to the serial port to set the output data format, positioning mode, data transmission rate, output message type and other parameters. Different receiver models have different commands. You must read the receiver's manual carefully. The function of writing data to the serial port will be introduced later.

Start a helper thread to handle the serial port event hCommWatchThread = CreateThread NULL, // Security attribute, // Initialize the size of the thread stack, the default is the same size as the main thread. The function starts a helper thread and executes Fil-eReadThreacK To allow the reading of data in the background, FileReadThreadO must be defined as a global function.

The real data reading function is realized by the FileReadThread () function. The following is the implementation code for reading the serial port data using the overlapping method.

The data character array osReader = 0; // read the serial port overlapping structure to read the serial port IpStat; // serial port device status information // create the event handle osReader. FALSE in the heavy event structure, // delay reading data // overlapping read data 7 Write data to the serial port in the main thread to send the command The following code is to send data to the serial port, sending data is similar to receiving data.

// Set the last returned error code illegal serial port handle error // Set the last returned error code invalid data // If the data buffer pointer is empty if (pBuf― = NULL) // Set the last returned error The data sent by the parameter with invalid code and the result returned by the overlap event overlap structure IpStat; // Serial device status information // Create event handle osWrite in the event structure. // Create heavy event handle without success // Send data to the serial port /// Send in an overlapping manner // Get the result of overlapping reading serial port data caseWAITOBJECT0; // Clear the error occurred in the serial port 8 Close the serial port and perform this operation before the end of the thread application, can be executed in the OnDestroy () function of the View class

(Continued from page 53) (Continued from page 23) 4 Conclusion The above is the process and code of serial communication using multi-thread programming technology, which can realize real-time communication between the GPS receiver and the computer, so as to realize the navigation function. All serial communication problems can use the above method.

As a switch supplier,most of our products compliance with TUV,CE approval which based on EN 61058-1;2002+A2,the current rating from 10amp to 16amp,Ranging from 3 to 6 poles,with many choices of  functions.Including series KR1,KR2 and KRX.Different size of the panel cut-out,kinds of functions for different application.

For example,the following item KRX meet TUV/CE requirment at 2A 250VAC:

LINE CORD SWITCH


TUV Approved Switch

TUV Approved Switch,Professional TUV Approved Switch,safety TUV Approved Switch

Ningbo Kara Electronic Co.,Ltd. , https://www.kara-switch.com

This entry was posted in on