Menu

SetCommState Fails?

Help
2009-10-01
2013-05-20
  • Tony Glader

    Tony Glader - 2009-10-01

    Hello!

    Com0com serial port baudrate can't be set with SetCommState(). It fails with error 'The Parameter is incorrect'. Why?

    Here is source:

          dcbCommPort.DCBlength = sizeof(DCB);
          GetCommState(hComm, &dcbCommPort);

          dcbCommPort.BaudRate = BAUD_19200;
          dcbCommPort.Parity = NOPARITY;
          dcbCommPort.ByteSize = 8;
          dcbCommPort.StopBits = ONESTOPBIT;
          dcbCommPort.fDtrControl = DTR_CONTROL_DISABLE;
          dcbCommPort.fRtsControl = RTS_CONTROL_DISABLE;
       
       
          if(!SetCommState(hComm, &dcbCommPort))
          {
           void *lpMsgBuf;
           FormatMessage(
                FORMAT_MESSAGE_ALLOCATE_BUFFER |
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                GetLastError(),
                0,
                (LPTSTR) &lpMsgBuf,
                0, NULL );
       
             MessageBox(NULL, (LPTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONINFORMATION);
       
             StatusBox->Lines->Add("Cannot set comm state.");
             CloseHandle(hComm);
             hComm = NULL;
             return;
          }

     
  • Vyacheslav Frolov

    You code works on my PC.

    Anyway

    1. Add `if (hComm == INVALID_HANDLE_VALUE) …`
    2. Add `if (!GetCommState( …`
    3. Replace `BAUD_19200` by `CBR_19200`
    4. Replace `hComm = NULL` by `hComm = INVALID_HANDLE_VALUE`

     
  • Tony Glader

    Tony Glader - 2009-10-01

    Added check of GetCommState and it fails also:

             DCB dcbCommPort;
          void *lpMsgBuf;
       
          StatusBox->Lines->Add("Connecting..");
          if(hComm != NULL)
          {
           StatusBox->Lines->Add("Already connected");
           return;
          }
       
       
       
          hComm = CreateFile("COM19",
                              GENERIC_READ | GENERIC_WRITE,
                              0,
                              0,
                              OPEN_ALWAYS,
                              0,
                              0);
       
          // IF THE PORT CANNOT BE OPENED, BAIL OUT.
       
          if(hComm == INVALID_HANDLE_VALUE)
          {
           StatusBox->Lines->Add("Connection failed!");
           return;
          }
       
          StatusBox->Lines->Add("Connected");
       
       
          GetCommTimeouts(hComm,&ctmoOld);
          ctmoNew.ReadTotalTimeoutConstant = 100;
          ctmoNew.ReadTotalTimeoutMultiplier = 0;
          ctmoNew.WriteTotalTimeoutMultiplier = 0;
          ctmoNew.WriteTotalTimeoutConstant = 0;
          SetCommTimeouts(hComm, &ctmoNew);
       
          dcbCommPort.DCBlength = sizeof(DCB);
       
          if(!GetCommState(hComm, &dcbCommPort))
          {
               FormatMessage(
                FORMAT_MESSAGE_ALLOCATE_BUFFER |
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                GetLastError(),
                0,
                (LPTSTR) &lpMsgBuf,
                0, NULL );
       
             MessageBox(NULL, (LPTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONINFORMATION);
       
             StatusBox->Lines->Add("Cannot get comm state.");
             CloseHandle(hComm);
             hComm = INVALID_HANDLE_VALUE;
             return;
       
          }
       
          dcbCommPort.BaudRate = CBR_19200;
          dcbCommPort.Parity = NOPARITY;
          dcbCommPort.ByteSize = 8;
          dcbCommPort.StopBits = ONESTOPBIT;
          dcbCommPort.fDtrControl = DTR_CONTROL_DISABLE;
          dcbCommPort.fRtsControl = RTS_CONTROL_DISABLE;
       
       
          if(!SetCommState(hComm, &dcbCommPort))
          {
           FormatMessage(
                FORMAT_MESSAGE_ALLOCATE_BUFFER |
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                GetLastError(),
                0,
                (LPTSTR) &lpMsgBuf,
                0, NULL );
       
             MessageBox(NULL, (LPTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONINFORMATION);
       
             StatusBox->Lines->Add("Cannot set comm state.");
             CloseHandle(hComm);
             hComm = INVALID_HANDLE_VALUE;
             return;

     
  • Vyacheslav Frolov

    You have created a regular file COM19 in the working directory.

    Shold be

        hComm = CreateFile("\\\\.\\COM19",
                           GENERIC_READ|GENERIC_WRITE,
                           0,
                           NULL,
                           OPEN_EXISTING,
                           0,
                           NULL);

     
  • Tony Glader

    Tony Glader - 2009-10-01

    Thanks! That was solution :). Now just WriteFile raises exception, but that is another problem not related to com0com, i guess.

     

Log in to post a comment.