In the past, the dos and bios interrupts were used to provide users with the communication capability of serial interfaces. In Windows, C ++ development tools neither provide dedicated serial communication control methods like DOS and bios, nor allow users to directly control serial port interruptions.
To ensure resource sharing, the Windows system takes over all kinds of hardware resources. Using interrupt to control the port will disrupt the system's multi-task performance and affect the system's stability. However, windows also provides powerful API functions that allow users to indirectly control serial communication.
1. APIs for implementing serial communication
API functions not only provide methods to open and read/write communication ports, but also provide a wide range of functions to support various operations on serial communication. Common functions and functions are shown in Table 5-1.
Table 5-1 Common Serial Communication API functions and their functions
Function Name
Createfile open serial port
Getcommstate detection serial port settings
Setcommstate
Buildercommdcb uses the value in the string to fill the device control block.
Getcommtimeouts detection communication timeout settings
Setcommtimeouts sets the communication timeout Parameter
Setcommmask: Set monitored events
Waitcommevent waits for a monitored event
Waitformultipleobjects waits for the results of multiple Monitored Objects
Writefile sends data
Readfile receives data
Getoverlappedresult returns the final overlapping (asynchronous) Operation Result
Purgecomm clears the serial port buffer and exits all related operations
Clearcommerror updates the serial port status struct and clears all serial port hardware errors.
Closehandle
2. Open the serial port
The createfile function is originally used to open a file, but it can also be used to open a communication port. Like other objects in the system, the communication port is identified by a handle. The createfile function returns the communication port handle to be operated. The call method is as follows:
Handle createfile (
Lptstr lpfilename, // pointer to the file name string
DWORD dwdesireaccess, // Operation Mode
DWORD dw1_mode, // Sharing Mode
Lpsecur99vy_attributes lpsecurityattributes,
// Pointer to the Security Attribute
DWORD dwcreationdistribution, // File Creation Method
DWORD dwflagsandattributes // file attributes
Handle htemplatefile) // template file handle
Lpfilename: point to a null-terminated string that specifies the name of the file, pipe, source, disk device, or console to be created, opened, or truncated. When you use createfile to open the serial port, this parameter can be set to "COM1" to specify Serial Port 1, "com2" to specify Serial Port 2, and so on.
Dwdesireaccess: specifies the type of access to the file. This parameter can be generic_read (specifying read access to the file) or eneric_wr99ve (specifying write access to the file) one or both of the two values are. Use eneric_read | generic_wr99ve to specify that the serial port can be read and written;
Dww.mode: specifies how the file can be shared. Because the serial port does not support any sharing mode, dww.mode must be set to 0;
Lpsecurityattributes defines security attributes, which are generally not used and can be set to null. This parameter is ignored in win 9X;
Dwcreationdistribution defines the file creation method. The serial port must be set to open_existing to open an existing file;
Dwflagsandattributes specifies the attributes and flag of the file. In this program, file_flag_overlapped indicates the asynchronous communication mode;
Htemplatefile points to the handle of a template file. The serial port does not have a template. It is set to null. In Windows 9x, this parameter must be null.
The function call for enabling Serial Port 1 in asynchronous read/write mode is as follows:
M_hcomm = createfile ("COM1", // open Serial 1
Generic_read | generic_wr99ve, // read/write Method
0, // cannot be shared
NULL, // No security structure
OPEN_EXISTING, // open an existing device
FILE_FLAG_OVERLAPPED, // asynchronous mode
0); // no template
When the serial port is successfully opened, its handle is returned; otherwise, INVALID_HANDLE_VALUE (0 XFFFFFFFF) is returned ).
3. Serial Port Settings
When you open the serial port for the first time, the serial port is set to the system default value. The GetCommState and SetCommState functions can be used to retrieve and set the DCB (Device Control Block) structure of port settings, in this structure, the BaudRate, ByteSize, StopBits, and Parity fields contain information such as the serial port baud rate, number of data digits, stop bit, and Parity control. In the program, you can first use GetCommState to retrieve the current port settings, modify some of the fields, and then use SetCommState to set the port. This eliminates the need to construct a complete DCB structure.
The following describes several major functions and struct:
(1) GetCommState
BOOL GetCommState (hCommDev, lpdcb );
The hCommDev parameter identifies the communication device and uses the handle returned by CreateFile. Lpdcb is a pointer to the DCB structure. After the function is called, the current serial port configuration information will be saved in this structure. If the function is successful, the return value is TRUE; otherwise, the return value is FALSE. The SetCommState usage is similar to that of GetCommState. The DCB structure is defined as follows (only the main items are described ):
Typedef struct _ DCB {
......
DWORD BardRate; // The baud rate.
BYTE ByteSize; // number of data bits
BYTE Parity; // whether the Parity bit exists
BYTE StopBits; // Number of Stop bits
......
} DCB;
(2) SetCommTimeouts
BOOL SetCommTimeouts (hCommDev, lpctmo );
The Lpctmo point to the COMMTIMEOUTS structure containing the new timeout parameter. The COMMTIMEOUTS structure is defined as follows:
Typedef struct _ COMMTIMEOUTS {
DWORD ReadIntervalTimeout;
DWORD ReadTotalTimeoutMultiplier;
DWORD ReadTotalTimeoutconstant;
DWORD WriteTotalTimeoutMultiplier;
DWORD WriteTotalTimeoutconstant;
} COMMTIMEOUTS, LPCOMMTIMEOUTS;
Readintervaltimeout: specifies the maximum time between two characters on the communication line in milliseconds. During the readfile operation, the calculation time starts when the first character is received. If the interval between any two characters exceeds the maximum value, the readfile operation is complete and data buffering is returned. The value 0 indicates no interval. If the member is maxdword and readtotaltimeoutconstant and readtotaltimeou
This article from: Take the Wind Original Program (http://www.qqcf.com) detailed source reference: http://study.qqcf.com/web/214/23259.htm