Different methods can be used to design asynchronous serial communication programs in Windows using Visual C ++. One method can use the serial port API functions provided by windows; the other method can directly use the ActiveX control MSComm. ocx provided by Microsoft. Using the MSComm. ocx control for serial port programming is relatively simple. As long as you set and operate the properties, events, and methods of the control, you can complete the simple serial communication function. The direct use of the serial port API functions provided by Windows is relatively flexible. During the experiment, you can program either of them according to your own situation. The following describes how to program the serial port API functions provided by windows.
In Windows, serial port and other communication devices are processed as files. The functions used to open, close, send, and receive a serial port are the same as those used in the operation file. In general, asynchronous serial communication program design using Visual C ++ can be divided into four major stages, they are the opening stage, reading the status value and setting stage, sending and receiving stage of the serial data, and closing stage of the serial port.
(1) Open a serial port
Before you perform all operations on a serial port, open it first. You can use the createfile function to open a serial port. The createfile function returns a handle for subsequent operations related to the serial port. Similar to file operations, when using createfile to open a serial port, you can also specify the serial port as "Read access permission", "write access permission", or "read/write access permission ".
Handle createfile (
Lptstr lpfilename
DWORD dwdesiredaccess
DWORD dwsharedmode
Lpsecurity_attributes lpsecurityattributes
DWORD dwcreationdisposition
DWORD dwflagsandattributes
Handle htemplatefile
);
When the call is successful, createfile returns the handle to open the file, which will be used in subsequent call functions related to the serial port. If the call fails, createfile returns invalid_handle_value.
(2) reading the status of the serial port and setting properties
Once the serial port is enabled, you can set the attributes of the serial port. Because the attributes of the serial port are very complex, it is usually used to read the current status value of the serial port, and then modify it on this basis.
N get the current status of the serial port
In Windows, the getcommstate function is used to obtain the current configuration of the serial port. The getcommstate statement is as follows:
Bool getcommstate (
Handle hfile
Lpdcb
);
The first hfile parameter of the getcommstate function is the handle returned by the createfile function pointing to the opened serial port. The second parameter points to the device control block DCB. DCB is a very important data structure. Almost all the serial port attributes and statuses are stored in the member variables of this structure.
N. Set the serial port
In Windows, the setcommstate function is used to modify the current parameter configuration of a serial port. The setcommstate function declaration is as follows:
Bool setcommstate (
Handle hfile
Lpdcb
);
The first hfile parameter of the getcommstate function is the handle returned by the createfile function pointing to the opened serial port. The second parameter points to the device control block DCB. If the function call is successful, the return value is not 0. If the function call fails, the return value is 0. When the application only needs to modify some configuration values of the serial port, you can use the getcommstate function to obtain the current DCB structure, change the parameters, and then call the setcommstate function to set the modified DCB to configure the serial port.
N allocates the receiving and sending buffer for the serial port
When a serial port is opened, a sending buffer and a receiving buffer can be allocated to the serial port. The setupcomm function can be used to configure the sending and receiving buffers of the serial port. If setupcomm is not called, The system allocates the default sending and receiving buffers for the serial port. However, to ensure that the buffer size is consistent with the actual needs, it is best to call this function for setting. The following is a prototype of the setupcomm function:
Bool setupcomm (
Handle hfile
DWORD dwinqueue
DWORD dwoutqueue
);
The hfile function returns the handle pointing to the opened serial port. The dwinqueue and dwoutqueue parameters respectively specify the size of the receiving buffer and sending buffer recommended by the application.
N clear the receiving and sending Buffer
Before all the data sending and receiving operations on the serial port, it is best to use the purgecomm function to clear the data in the sending and receiving buffers of the serial port. The purgecomm function is prototype as follows:
Bool purgecomm (
Handle hfile
DWORD dwflages
);
The hfile parameter is the handle that is returned by the createfile function pointing to the opened serial port. The dwflags parameter indicates the action to be executed. If dwflags is purge_txclear, the system is notified to clear the sending buffer; If dwflags is purge_rxclear, the system is notified to clear the receiving buffer; if you need to clear all the sending and receiving buffers, you can set dwflags to purge_txclear | purge_rxclear. If the purgecomm function is successfully called, the returned value is not 0. If the function fails to be called, the returned value is 0.