The control is easy to use, but it must be used in the dialog box. In some applications that require communication in the thread, the use of the control is too slow. An API is an extremely important component included in windows. Windows 32-bit APIs are mainly a series of complex functions and Message sets. It can be seen as an open universal function enhancement interface provided by windows for various development systems running under it.
The communication programCreatefileSpecify the serial device and related operation properties, and then return a handle, which will be used for subsequent communication operations and throughout the communication process. After the serial port is opened, its properties are set to the default value.Getcommstate (hcomm, & DCB)Read the DCB settings of the control block of the current serial port device.Setcommstate (hcomm, & DCB)Write it. ApplicationReadfile ()AndWritefile ()The two API functions implement serial port read/write operations. For Asynchronous Communication, the last parameter of the two functions is a non-null pointer pointing to the overlapped structure. When the return value of the read/write function is false, call the getlasterror () function. The returned value is error_io_pending, indicating that the I/O operation is suspended, that is, the operation is transferred to the background for further execution. In this case, you can useWaitforsingleobject ()To wait for the end signal and set the maximum wait time, for example:
Bool breadstatus; Breadstatus = readfile (m_hidcomdev, buffer, Dwbytesread, & dwbytesread, & m_overlappedread ); If (! Breadstatus ){ If (getlasterror () = error_io_pending ){ Waitforsingleobject (m_overlappedread.hevent, 1000 ); Return (INT) dwbytesread );} Return (0 );} Return (INT) dwbytesread ); |
Implement serial communication with multiple threads
In Windows, the preemptive scheduler allocates CPU time between active threads. in windows, there are two different types of threads, one isUser Interface thread(User Interface thread), which contains a message loop or a message pump for processing received messages. The other isWorker threadThere is no message loop. The thread used to execute background tasks and monitor serial events is the working thread.
The multi-threaded program is written in the port configuration. The connection part is the same as that of a single thread. After the port configuration is complete, it is most important to create a synchronization object between multiple threads according to the actual situation, such as traffic signals, critical zones, and events.
After everything is ready, start the working thread. The program is as follows:
Cwinthrea commthread = Afxbeginthread (commwatchthread, // thread function name (Lpvoid) m_pttyinfo, // passed Parameter Thread_priority_above_normal, // Set the thread priority (Uint) 0, // maximum stack size (DWORD) create_suincluded, // create a flag (Lpsecurity_attributes) null ); If (waitcommevent (pttyinfo-> idcomdev, & dwevtmask, null )) { If (dwevtmask & pttyinfo-> dwevtmask) = pttyinfo-> dwevtmask) { Waitforsingleobject (pttyinfo-> hpostevent, 0 xffffffff ); Resetevent (pttyinfo-> hpostevent ); // Set the synchronization event object to a non-signal state. : Postmessage (csampleview, id_com1_data, 0, 0); // send notification message }} Begin_message_map (csampleview, cview) // {Afx_msg_map (csampleview) On_message (id_com1_data, onprocesscom1data) On_message (id_com2_data, onprocesscom2data) ..... //} Afx_msg_map End_message_map () |
Multi-threaded implementation can make the ports independent and accurately implement serial communication, so that serial communication has more flexibility and rigor, and fully utilizes the CPU time. However, in a real-time monitoring system, how to coordinate multiple threads and how to synchronize threads is difficult for multi-threaded serial communication programs.
The above content is taken from the VC-based serial communication technology application example.
The following is an excerpt from the development of large capacity storage devices in the power quality monitoring system.
Application Design
Applications are user applications running on the host. They are compiled using VC ++ to control USB devices and use USB to obtain power data stored in flash, analyze the data and draw a waveform. The application now calls the Win 32 FunctionCreatefile()Get the handle to access the device driver. Then, the application uses the Win32 function.Devicelocontrol()Submit the I/O control code and set the I/O buffer for the device handle returned by the createfile () function.
For each EZ-USB device that connects to the host, the EZ-USB GPD creates a connector for it, such as ezusb-I, where I increments from 0. CallCreatefile()The function is actually to get the handle of the target device generated by the device driver. It uses the device's link character as the function parameter. Applications Use Win32 FunctionsDevicelocontrol()Send a request to the device driver.
If the program has only one thread, data transmission takes most of the program's time during the program running process, causing no response to the command input at this time. Therefore, set data transmission to a separate thread to make the Command respond in a timely manner.