c++ - Set DCB Fails When Attempting to Configure COM Port -
i'm trying write c++ mfc application uses serial port (e.g. com8). every time try set dcb fails. if can point out i'm doing wrong, i'd appreciate it.
dcb dcb = {0}; dcb.dcblength = sizeof(dcb); port.insert( 0, l"\\\\.\\" ); m_hcomm = createfile( port, // virtual com port generic_read | generic_write, // access: read , write 0, // share: no sharing null, // security: none open_existing, // com port exists. file_flag_overlapped, // asynchronous i/o. null // no template file com port. ); if ( m_hcomm == invalid_handle_value ) { trace(_t("unable open com port.")); throwexception(); } if ( !::getcommstate( m_hcomm, &dcb ) ) { trace(_t("cserialport : failed comm state - error: %d"), getlasterror()); throwexception(); } dcb.baudrate = 38400; // setup baud rate. dcb.parity = noparity; // setup parity. dcb.bytesize = 8; // setup data bits. dcb.stopbits = 1; // setup stop bits. if ( !::setcommstate( m_hcomm, &dcb ) ) // <- fails here. { trace(_t("cserialport : failed set comm state - error: %d"), getlasterror()); throwexception(); }
thanks.
additional info: generated error code 87: "the parameter incorrect." microsoft's most useful error-code. j/k
my money on this:
dcb.stopbits = 1;
the msdn docs says stopbits:
the number of stop bits used. member can 1 of following values.
onestopbit 0 1 stop bit. one5stopbits 1 1.5 stop bits. twostopbits 2 2 stop bits.
so, you're asking 1.5 stop bits, such horribly archaic thing can't remember comes from. teleprinters, possibly.
i'd guess chances of driver/hardware supporting mode slim, hence error.
so, change dcb.stopbits = onestopbit;
Comments
Post a Comment