I am trying to write a C ++ MFC application that uses a serial port (e.g. COM8). Every time I try to install DCB, it fails. If someone can point out what I'm doing wrong, I would really appreciate it.
DCB dcb = {0};
dcb.DCBlength = sizeof(DCB);
port.Insert( 0, L"\\\\.\\" );
m_hComm = CreateFile(
port,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL
);
if ( m_hComm == INVALID_HANDLE_VALUE )
{
TRACE(_T("Unable to open COM port."));
ThrowException();
}
if ( !::GetCommState( m_hComm, &dcb ) )
{
TRACE(_T("CSerialPort : Failed to get the comm state - Error: %d"), GetLastError());
ThrowException();
}
dcb.BaudRate = 38400;
dcb.Parity = NOPARITY;
dcb.ByteSize = 8;
dcb.StopBits = 1;
if ( !::SetCommState( m_hComm, &dcb ) )
{
TRACE(_T("CSerialPort : Failed to set the comm state - Error: %d"), GetLastError());
ThrowException();
}
Thank.
Additional information: Generated error code: 87: "The parameter is incorrect." Microsoft is probably the most useful error code. J / c
source
share