Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SerialPort : DCB settings ?

Status
Not open for further replies.

youssef

Programmer
Mar 13, 2001
96
BE
Hi,

I use CreateFile, ReadFile, WriteFile, COMMSTAT, CLEARCOMMERROR for using the SerialPort in RS422.

I would like some help for settings the DCB.
I use my little program for communicate with a device.
In my settings , I would like :

- 38400 Bauds,
- 8 Bits,
- 1 Stop Bit,
- EVEN Parity,
- Binary Mode,
- Set RTS and DTR Enable,
- HandShaking comRTS.

If anyone can check my code for correct it because it doesn't working with my interface in RS422. But if I use this with hyperterminal in RS232 NullModem cable all are ok.

Best regards



// Open SerialPort (nComport = the port number )
//----------------------------------------------
//
// HANDLE hPort;

DWORD dwError; // dwThreadID;
DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
CString strComPort;
strComPort.Format("COM%d", nComport);


// Open the serial port.
BETACART_hPort = CreateFile (strComPort, // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read/write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute
// to copy

// If it fails to open the port, return FALSE.
if ( BETACART_hPort == INVALID_HANDLE_VALUE )
{
// Could not open the port.
MessageBox (TEXT("Le Port RS sélectionné n'existe pas"),
TEXT("Error"), MB_OK);
dwError = GetLastError ();
}

PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
GetCommState (BETACART_hPort, &PortDCB);

// Change the DCB structure settings.
PortDCB.BaudRate = 38400; // 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_DISABLE; //DTR_CONTROL_HANDSHAKE; //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_HANDSHAKE; //RTS_CONTROL_ENABLE;
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error.
PortDCB.ByteSize = 8; // Number of bits/bytes, 4-8
PortDCB.Parity = EVENPARITY; //ODDPARITY; // 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 (BETACART_hPort, &PortDCB))
{
// Could not configure the serial port.
MessageBox (TEXT("Unable to configure the serial port"),
TEXT("Error"), MB_OK);
//dwError = GetLastError ();
}
// Direct the port to perform extended functions SETDTR and SETRTS.
// SETDTR: Sends the DTR (data-terminal-ready) signal.
// SETRTS: Sends the RTS (request-to-send) signal.
EscapeCommFunction (BETACART_hPort, SETDTR);
EscapeCommFunction (BETACART_hPort, SETRTS);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top