AN197 Silicon_Laboratories, AN197 Datasheet - Page 2

no-image

AN197

Manufacturer Part Number
AN197
Description
Serial Communications Guide FOR THE Cp210x
Manufacturer
Silicon_Laboratories
Datasheet
AN197
3. Preparing an Open COM Port for Data Transmission
Once a handle is successfully assigned to a COM port several steps must be taken to set it up. The COM port must
first be purged and its initial state should be retrieved. Then the COM port's new settings can be assigned and set
up by a device control block (DCB) structure (more information is provided on the DCB structure in section 3.3. and
at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/dcb_str.asp).
3.1. Purging the COM Port
First the COM port should be purged to clear any existing data going to or from the COM port using the
PurgeComm() function:
PurgeComm(hMasterCOM, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
The first parameter in the PurgeComm() function is a handle to the open COM port that will be purged. The second
parameter contains flags that further describe what actions should be taken. All four flags, PURGE_TXABORT,
PURGE_RXABORT, PURGE_TXCLEAR, and PURGE_RXCLEAR should always be used. The first two flags
terminate overlapped write and read operations, and the last two flags clear the output and input buffers.
If this function returns successfully then a non-zero value is returned. If the function fails, then it returns 0. Upon
return, check the return value; if it is non-zero, continue to set up the COM port (more information on the
PurgeComm() function is located at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/
purgecomm.asp).
3.2. Saving the COM Port's Original State
Since the COM port settings can be modified to meet different needs, it is good practice to obtain the COM port's
current state and store it so that when the COM port is closed, the COM port can be restored back to its original
state. This can be done using the GetCommState() function:
DCB dcbMasterInitState;
GetCommState(hMasterCOM, &dcbMasterInitState);
The first parameter in the GetCommState() function is a handle to the open COM port to obtain settings from. The
second parameter is an address to a DCB structure to store the COM port's settings. This DCB structure should
also be used as the initial state when specifying new settings for the COM port (see section 3.3.).
If this function returns successfully then a non-zero value is returned. If the function fails, then it returns 0. Upon
return, check the return value; if it is non-zero, continue to set up the COM port (more information on the
GetCommState() function is located at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/
getcommstate.asp).
3.3. Setting up a DCB Structure to Set the New COM State
All of a COM port's settings are stored in a DCB structure. In section 3.2. a DCB structure was retrieved that
contained the initial settings of the COM port by using the GetCommState() function. To change a COM port's
settings, a DCB structure must be created and filled out with the desired settings. Then the SetCommState()
function can be used to activate those settings:
DCB dcbMaster = dcbMasterInitState;
dcbMaster.BaudRate= 57600;
dcbMaster.Parity= NOPARITY;
dcbMaster.ByteSize= 8;
dcbMaster.StopBits= ONESTOPBIT;
SetCommState(hMasterCOM, &dcbMaster);
Delay(60);
2
Rev. 0.6

Related parts for AN197