Configuring a Serial Port

The most critical phase in serial communications programming is configuring the port settings with the DCB structure. Erroneously initializing the DCB structure is a common problem. When a serial communications function does not produce the expected results, the DCB structure may be in err.

A call to the CreateFile function opens a serial port with default port settings. Usually, the application needs to change the defaults. Use the GetCommState function to retrieve the default settings and use the SetCommState function to set new port settings.

Also, port configuration involves using the COMMTIMEOUTS structure to set the time-out values for read/write operations. When a time-out occurs, the ReadFile or WriteFile function returns the specific number of characters successfully transferred.

    To configure a serial port

  1. Initialize the DCBlength member of the DCB structure to the size of the structure. This initialization is required before passing this member as a variable to any function.
  2. Call the GetCommState function to retrieve the default settings for the port opened with the CreateFile function. To identify the port, specify in the hPort parameter the handle that CreateFile returns.
  3. Modify DCB members as required. The following table shows the DCB structure members most frequently modified.
Member
Description
DCBlength Before calling the GetCommState function, set this member to the length of the DCB structure. Neglecting to do this can cause a failure or return erroneous data.
BaudRate Specifies the device communication rate. Assigns an actual baud rate or an index by specifying a CBR_ constant.
ByteSize Specifies the bits per byte transmitted and received.
Parity Specifies the parity scheme. Do not confuse this member with the fParity member, which turns parity on or off. Because parity is rarely used, this member can usually be NOPARITY.
StopBits Specifies the number of stop bits. ONESTOPBIT is the most common setting.
fOutX and fInX Turns software flow control on and off.
fRtsControl Turns the RTS flow control on and off. RTS_CONTROL_ENABLE turns on the RTS line during the connection. RTS_CONTROL_HANDSHAKE turns on RTS handshaking. RTS_CONTROL_DISABLE turns off the RTS line.
fOutxCtsFlow Turns the CTS flow control on and off. To use RTS/CTS flow control, specify TRUE for this member and RTS_CONTROL_HANDSHAKE for the fRtsControl member.
fOutxDsrFlow Turns the DSR flow control on and off. DSR flow control is rarely used. A typical port configuration is to set this member to FALSE, while enabling the DTR line.
fDtrControl Specifies the DTR flow control. DTR_CONTROL_ENABLE turns on the DTR line during the connection. DTR_CONTROL_HANDSHAKE turns on DTR handshaking. DTR_CONTROL_DISABLE turns off the DTR line.

  1. Call the SetCommState function to set the new port settings.

The following code example shows how to use the GetCommState and SetCommState functions to configure a serial port.

DCB PortDCB;

// Initialize the DCBlength member. 
PortDCB.DCBlength = sizeof (DCB); 

// Get the default port setting information.
GetCommState (hPort, &PortDCB);

// Change the DCB structure settings.
PortDCB.BaudRate = 9600;              // Current baud 
PortDCB.fBinary = TRUE;               // Binary mode; no EOF check 
PortDCB.fParity = TRUE;               // Enable parity checking 
PortDCB.fOutxCtsFlow = FALSE;         // No CTS output flow control 
PortDCB.fOutxDsrFlow = FALSE;         // No DSR output flow control 
PortDCB.fDtrControl = DTR_CONTROL_ENABLE; 
                                      // DTR flow control type 
PortDCB.fDsrSensitivity = FALSE;      // DSR sensitivity 
PortDCB.fTXContinueOnXoff = TRUE;     // XOFF continues Tx 
PortDCB.fOutX = FALSE;                // No XON/XOFF out flow control 
PortDCB.fInX = FALSE;                 // No XON/XOFF in flow control 
PortDCB.fErrorChar = FALSE;           // Disable error replacement 
PortDCB.fNull = FALSE;                // Disable null stripping 
PortDCB.fRtsControl = RTS_CONTROL_ENABLE; 
                                      // RTS flow control 
PortDCB.fAbortOnError = FALSE;        // Do not abort reads/writes on 
                                      // error
PortDCB.ByteSize = 8;                 // Number of bits/byte, 4-8 
PortDCB.Parity = NOPARITY;            // 0-4=no,odd,even,mark,space 
PortDCB.StopBits = ONESTOPBIT;        // 0,1,2 = 1, 1.5, 2 

// Configure the port according to the specifications of the DCB 
// structure.
if (!SetCommState (hPort, &PortDCB))
{
  // Could not create the read thread.
  MessageBox (hMainWnd, TEXT("Unable to configure the serial port"), 
              TEXT("Error"), MB_OK);
  dwError = GetLastError ();
  return FALSE;
}