SetCommState

 

Description

The SetCommState function configures a communications device according to the specifications in a device-control block (a DCB structure). The function reinitializes all hardware and control settings, but it does not empty output or input queues.

Syntax

BOOL SetCommState( HANDLE hFile, LPDCB lpDCB )

Parameters

hFile
handle to the communications device.
lpDCB
Pointer to a DCB structure that contains the configuration information for the specified communications device.

Return

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero.

Examples

EX1

#include <mscomm.h>

BOOL Set_Comm_State()
{
        file ff;

        //Open comm port
        if (!ff.Open("COM1:",  file::modeReadWrite ))
        {
                ASSERT(FALSE);
                return FALSE;
        }

        UINT hCom = ff.GetHandle();

        if (hCom == file::hFileNull ) 
        {
                ASSERT(FALSE);
                return FALSE;
        }

        DCB dcb;

        if (!GetCommState((HANDLE)hCom, &dcb)) 
        {
                ASSERT(FALSE);
                return FALSE;
        }

        //dcb parameters for user
        dcb.BaudRate = CBR_9600;          // set the baud rate
        dcb.ByteSize = 8;               // data size, xmit, and rcv
        dcb.Parity = NOPARITY;            // no parity bit
        dcb.StopBits = ONESTOPBIT;        // one stop bit

        //dcb fixed parameters 
        dcb.fBinary=1; 
        dcb.fParity=0; 
        dcb.fOutxCtsFlow=0; 
        dcb.fOutxDsrFlow=0; 
        dcb.fDtrControl=0; 
        dcb.fDsrSensitivity=0; 

        dcb.fTXContinueOnXoff=0; 
        dcb.fRtsControl=0;

        if (!SetCommState((HANDLE)hCom, &dcb)) 
        {
                ASSERT(FALSE);
                return FALSE;
        }

        //...

        if( !ff.Close() ) // Close() function will happen automatically by the file class destructor.
        {
                ASSERT(FALSE);
                return FALSE;
        }
}

Remark

See Also

file::Open, file::Close, GetCommState

header to Include

mscomm.h

Reference