Implementation of Serial Communication under Windows CE

zhaozj2021-02-16  54

Abstract: Windows CE is an embedded operating system introduced by Microsoft, which is used in small devices with limited system resources. It uses a modular structure that supports a variety of external interfaces, including the most commonly used RS232 serial interface. This article discusses the programming of the serial interface of the Windows CE system and gives an interface program with the GPS device. Keywords: Windows CE serial communication GPS

1 Introduction to Windows CE

Windows CE is a small, ROM-based, operating system with Win32 subset API. Its advantage is that small size, Win32 API subsets and support capabilities for multi-platform. Programming under Windows CE is that the resources of the Windows CE device are rare, the memory, the display is small, and the interface is relatively small, and the actual situation varies greatly. In addition, Windows CE only supports Unicode, which should be taken particularly at the program. In Windows CE, in addition to some basic Windows universal controls, there are also some specially designed controls, such as CommandBar. WINDOWS CE is small, but its function is much more, memory management, file operation, multi-thread, network function, etc. It is supported, which can be said that it is a small, fifty-capable.

2 Serial communication under Windows CE

The serial port belongs to the stream interface device under Windows CE, which is a conventional I / O driver call for serial device interface and a combination of specific functions associated with communication. Serial devices are considered to open, close, read, read and write serial ports, can be installable. The communication function of Windows CE is the same as that of most of the other Windows communication functions. It is important to note that Windows CE does not support programming directly to serial port registers. Commonly used serial port functions are as follows:

(1) Open and close the serial port

The CreateFile function is used to open the serial port.

Hport = createfile (Text ("COM1:"), Generic_Read | Generic_Write, 0, NULL, OPEN_EXISTING, 0, NULL. Note There is a colon after COM1. The last parameter dwflagsandattribute must be 0 because Windows CE only supports non-overlapping I / O. The third parameter DWSHAREMODE must also be 0, and the communication port cannot be shared like a file. The return value of this function is the handle of the open serial port or INVALID_HANDLE_VALUE.

Close the serial port can call CloseHandle (HPORT).

(2) Configure the serial port

The configuration serial port is mainly used to configure port settings, including baud rates, stop bits, data bit lengths, check digits, flow control, etc., and configure the timeout value.

First open the serial port, get the current open serial configuration with the getcommstate function, then modify the DCB member as needed, and finally set the new serial configuration with the setcommstate function.

DCB portdcb; // Create a DCB variable

Port.dcb.dcblength = sizeof (dcb);

GetcommState (HPORT, & portDCB); // Get the current serial configuration modification DCB member

Portdcb.baudrate = 9600; // baud rate

Portdcb.parity = noparity; // check digit

Portdcb.stopbits = onestopbit; // Stopping

PORTDCB.BYTESIZE = 8;

.

.

.

Setcommstate (HPORT, & portDCB); // Set new serial configuration

For the serial port, you must configure a timeout value, otherwise the program may fall into a loop to wait for the character from the serial port. This will greatly reduce the usage time of the equipment battery, so the timeout value is required to be configured. Another solution is to adopt multi-threaded. Multi-threaded will be described in the next part. Typically, configure the timeout value and configuration serial port. First, use the getcommtimeouts function to get the timeout value of the current serial port. You can then modify the CommTimeout member, and finally set the timeout value with the setcommtimeouts function.

CommTIMEOUTS COMMTIMEOUTS; / / Define CommTIMEOUTS structure

Getcommtimeouts (HPORT, & COMMTIMEOUTS); // Get the current timeout value

/ / Modify members of CommTimeout

CommTIMEOUTS.Readintervaltimeout = Maxdword;

CommTIMEOUTS.ReadtotalTimeoutMultiplier = 0;

CommTIMEOUTS.ReadtotalTimeoutConstant = 0;

CommTIMEOUTS.WRITETOTALTIMEOUTCONSTANT = 1000;

CommTIMEOUTS.WRITETOTIMEOUTMULTIPLIER = 10;

Setcommtimeouts (HPORT, & COMMTIMEOUTS); // Setting the timeout value

(3) Read and write serial port

Use the readfile and writefile function to read and write the serial port.

Int rc;

DWORD CBYTES;

BYTE CH;

Rc = Readfile (Hport, & Ch, 1, & Cbytes, NULL);

The first parameter is the serial handle. The second parameter is the character read back, and the third parameter is the number of characters to read, and the fourth parameter returns the number of characters actually read.

Int rc;

DWORD CBYTES;

BYTE CH = TEXT ("a");

Rc = Writefile (Hport, & Ch, 1, & Cbytes, NULL);

The first parameter is the serial handle. The second parameter is the character to be written. The third parameter is the number of characters to be written, and the fourth parameter returns the number of characters written in characters.

It should be noted that Windows CE does not support overlapping I / O, so if you perform a large number of read and write serial port operations at the main thread, it is possible to get a slow serial port, so multithreading is usually used to read and write serial ports. operating.

(4) Communication event

In the Windows CE programming, in addition to the individual threads can be used to handle read and write serial port operations, methods for utilizing communication events can also be employed. Communication events are notifications to the application when important events have occurred. Use the waitcommmevent function to block the thread until a specific event occurs. The general method is: first specify one or more events to look up with the setcommmevent function, then call the waitcommEvent function and specify the event that causes this function to return. When the waitcommEvent function returns, the loop calls the ReadFile function and reads back all received characters. Finally, call the setcommEvent function again to specify the event you want to find.

3 Multi-threads under Windows CE

Windows CE is a complete multitasking, multi-threaded operating system. Windows CE can also run up to 32 processes. Each process has a primary thread and can have multiple additional threads. The additional thread is limited only by the process of the process address space of the available memory and thread stack. Windows CE is scheduled to be ahead. Threads are run in units of time, usually 25ms. Threads have priorities, all high priority threads will run before the low priority thread. All threads with high priority must be blocked before the thread that can schedule a specific priority. The same priority thread is scheduled by a circulation method. If the high priority thread stops blocking, and the low priority thread is currently running, then the low priority thread will be suspended, and the thread of the high priority is depented. The low priority thread will never seize the high priority thread, and there is also an exception: one is the thread has priority thread_priority_time_critical, it will never be preemptive; the other is a low priority thread with high priority thread Waiting for resources, priority is inverted. In Windows CE, threads can have 8 priorities.

Here is an example of creating a thread and a thread function:

Handle hthread;

DWORD DWTHREADID = 0;

INT nParameter = 5;

Hthread = CreateThread (Null, 0, Thread, NParameter, 0, & DWTHREADID); // Create thread

CloseHandle (hthread); // Close thread

// thread function

DWORD WINAPI THREAD (PVOID PARG)

{

INT nParam = (int) PARG;

.

.

.

Return 0x15;

}

The CreateThread function is not supported under Windows CE in many parameters, so it is set to NULL or 0. The third parameter points to the beginning of the thread function, and the fourth parameter is the only parameter that the CreateTHRead function is transmitted to the thread function. The CreateThread function returns the thread handle. When this handle does not need, call the CloseHandle function to turn off it. The thread function is running until the termination, calls the exitthread function to terminate the execution of the thread.

For multiple threads running in the system, you need to coordinate their activities, which is to achieve synchronization. In Windows CE, the method used is to use synchronous objects. A thread is waiting for a synchronization object, and when notifying the object with a signal, unblocking the thread that is waiting, and schedules the thread. Synchronous objects include events and muters. Here we only introduce events.

Event object is a synchronous object with two states - there is signal and meta signal. The event is automatically set to the signal after being created. The event can be named, which is shared by different processes. Create an event using the following function:

Handle CreateEvent (LPSecurity_attributes lpeventattributes, bool bmanualreset, bool binitialstate, lptstr lpname);

The first parameter of the function should be 0, and the second parameter indicates that the event should be manually reset or automatically reset to no signal. The third parameter indicates that the event is a signal or no signal state, and finally A parameter points to the event name. The named event can be shared by a process, otherwise it is set to NULL. After creating an event, you can use the setEvent function or the PULSEEVENT function notifies the event.

The setEvent function is an automatic reset event, just releases a thread to run; the PulseEvent function is an artificial reset event, releases all threads waiting for that event. Finally, you can break the event object with a CloseHandle function.

The usage of events is usually, and threads use one of the following functions to wait for events: WaitForsingleObject, WaitFormultiPleObjects, MsgwaitFormultiPleObjects or MsgwaitFormultiPleObjectsex. When the thread is blocked by one of these functions, the thread consumes only a small amount of electrical energy and CPU processing capabilities. It should be noted that the application's main thread cannot be blocked by WaitForsingleObject or WaitFormultiPleObjects, otherwise the main thread cannot process the message loop. The usual approach is to use multithreaded, main thread process loops, and additional threads need to block shared resources on events. 4 actual application

In the onboard positioning system, the main computer needs to accept data input of a variety of sensors, which is the most commonly used to GPS data. Usually the communication method of the GPS receiver is a serial RS232 interface, so the function of the GPS module of the navigation program is to receive the data received from the serial port, and then processes.

The program uses multi-threaded, the main thread is responsible for message processing, and there is also a read and write two additional threads, and use an event trigger. The reading thread is responsible for reading the GPS data from the serial port, and the write thread is triggered by the event. A code of the GPS data receiver is given in the network supplement (http://www.dpj.com.cn).

Create an event when the program is initialized, create a write thread and block it. Write thread wait event trigger. After pressing the "Open Serial Port" button, open the serial port, create a read thread, read the GPS data, proceed; press the "Send" button to set the event status, release the blocking write thread, send data.

转载请注明原文地址:https://www.9cbs.com/read-21715.html

New Post(0)