The port setting is carried out with the help of the DCB (Device-Control Block) structure.By filling this structure with needed values you can change the connection parameters to those needed at the moment.
To initially create the DCB structure with necessary general settings (baud rates, patity, number of bits, number of stop bits and flow control) is carried out by the BuildCommDCB function.
BOOL BuildCommDCB (LPCTSTR lpDef, LPDCB lpDCB);
This function is very useful as it sets the parameters of DCB structure which can be transmitted to the port just like *mode* command. If you remember MS DOS, null modem cable and connection between two computers: sending a string returns the DCB structure.
String example: baud=1200 parity=N data=8 stop=1
strcpy(buffer,"baud=1200 parity=N data=8 stop=1");
if (dcb.BaudRate == CBR_1200) cout << "Yes " << endl;
else cout << " error config DCB";
In case of successful termination BuildCommDCB() returns any value but zero, otherwize zero. This function replaces only explicit members with some exceptions.
For 9600,n,8,1strings (not ending in x or p characters):
For 9600,n,8,1,x strings (ending in õ character):
For 9600,n,8,1,x strings (ending in p character):
After creating the DCB structure we must write it into the open port, it is done with the help of GetCommState function. When the structure is written into the port it may be needed to change the port parameters. The SetCommState function may be used to evade creating the structure anew .
BOOL GetCommState (HANDLE hFile, LPDCB lpDCB);
BOOL SetCommState (HANDLE hFile, LPDCB lpDCB);
HANDLE hFile, // descriptor of the communications device
LPDCB lpDCB // initial address of the structure
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 7;
dcb.Parity = 2;
dcb.StopBits = 0;
The SetCommState function configures the device according to its specification in the device-control block (a DCB structure). This function initializes the hardware and managing settings parameters again but doesn't clear in and out buffers.
The SetCommState function returns an error if XonChar = XoffChar in the DCB structure.
When using SetCommState to configure a 8250 chip port there are the following limits of ByteSize and StopBits values:
fCoder © 1998 - 2002