A communications resource is configured when it is opened by CreateFile. However, these settings may not conform to the requirements of your application. To determine the initial DCB configuration, call GetCommState . This fills in a serial port DCB with the current configuration parameters. Your application can then adjust the DCB values that are important to it, and reconfigure the device by specifying the modified DCB in a call to SetCommState.
SetCommState provides a basic interface that uses a serial port DCB to specify the desired configuration. Among the parameters specified by the DCB are baud rate, number of data bits, and number of stop bits. Some of the other DCB fields enable parity checking, flow control, and specify special characters. If you are only interested in setting a few fields of the DCB, it is a good idea to just modify a DCB that has been filled in by a call to GetCommState . This ensures that the other fields of the DCB have appropriate values. For example, a common error occurs when a device is configured with a DCB in which the XonChar is equal to the XoffChar. Note that some fields of the DCB structure have been changed from previous versions of Windows. In particular, the flags for controlling RTS and DTR flow control have been changed. Refer to the DCB reference page for more information.
Another way to fill in a DCB is with the BuildCommDCB call. BuildCommDCB uses a string with the same format as the DOS MODE command line to specify settings for baud rate, parity scheme, number of stopbits, and number of character bits. The remaining fields of the DCB are filled in according to the current settings for that device (or the defaults if the device has not been opened previously), except that Xon/Xoff and hardware flow control are always disabled.
GetCommProperties can be called to get information from a provider about the configuration settings that are supported by that provider. This information can then be used in the calls that set configuration.
The following code fragment opens a handle to “COM1” and fills in a DCB with the current configuration. The DCB is then modified and used to reconfigure the device:
HANDLE hCom;
DWORD dwError;
DCB dcb;
BOOL bSuccess;
hCom = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened with exclusive access
NULL, // no security attrs
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE) {
dwError= GetLastError();
// handle error
}
// omit call to SetupComm to use default queue sizes
// get the current configuration
bSuccess = GetCommState(hCom, &dcb);
if (!bSuccess) {
/* handle error *
}
// fill in DCB: baud=9600, 8 data bits, no parity, 1 stop bit
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
bSuccess = SetCommState(hCom, &dcb);
if (!bSuccess) {
/* handle error */
}
Note that when using SetCommState to reconfigure a device, the device's state is changed but the transmit and receive queues are not flushed and pending read and write operations are not aborted.