Look RS232 > RS232C Hardware  
fCoder SIA
fCoder SIA Look RS232 Download Order Support Cookie Policy
Look RS232
Download
Order
Support
Testimonials
FAQ
Online help
Modem Programming
Serial port
RS232 Hardware
Programming
  Work with DCB

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.

Function syntax:

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

Function example:

DCB dcb;

ZeroMemory(&dcb,sizeof(DCB));

char buffer[100];

strcpy(buffer,"baud=1200 parity=N data=8 stop=1");

if (BuildCommDCB((char*)&buffer,&dcb))

{

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):

  • fInX, fOutX,fOutXDsrFlow, fOutXCtsFlow are set to FALSE
  • fDtrControl is set to DTR_CONTROL_ENABLE
  • fRtsControl is set to RTS_CONTROL_ENABLE

For 9600,n,8,1,x strings (ending in � character):

  • fInX, fOutX are set to TRUE
  • fOutXDsrFlow,fOutXCtsFlow are set to FALSE
  • fDtrControl is set to DTR_CONTROL_ENABLE
  • fRtsControl is set to RTS_CONTROL_ENABLE

For 9600,n,8,1,x strings (ending in p character):

  • fInX, fOutX are set to FALSE
  • fOutXDsrFlow,fOutXCtsFlow are set to TRUE
  • fDtrControl is set to DTR_CONTROL_HANDSHAKE
  • fRtsControl is set to RTS_CONTROL_HANDSHAKE

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 .

Function syntax:

BOOL GetCommState (HANDLE hFile, LPDCB lpDCB);

BOOL SetCommState (HANDLE hFile, LPDCB lpDCB);

Parameters specifications:

HANDLE hFile, // descriptor of the communications device

LPDCB lpDCB // initial address of the structure

Functions example:

if(GetCommState(hCom, &dcb))

{

dcb.BaudRate = CBR_9600;

dcb.ByteSize = 7;

dcb.Parity = 2;

dcb.StopBits = 0;

SetCommState(hCom, &dcb);

}

Note

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:

  • number of bits can be from 5 to 8 bits;
  • it's forbidden to use 5 bits together with 2 stop bits;
  • it's forbidden to use 6, 7, or 8 bits together with 1.5 stop bits.
 
  Contact us
© fCoder SIA