none
Unable to configure the serial port RRS feed

  • Question

  • Hi,

    my programm open a serial port on Windows CE 6.0

    I have some problem when i configure the port: (on windows 8 it work correclty)

    code:

    hPort= CreateFile(/*lpszPortName*/_T("\\.\COM2")/*CString3*/,            GENERIC_READ|GENERIC_WRITE, 
                                          0, 
                                          0, 
                                          OPEN_ALWAYS /* CREATE_NEW */   /*OPEN_EXISTING*/,
                                          FILE_ATTRIBUTE_NORMAL /*| FILE_FLAG_OVERLAPPED*/,
                                          0);
    
      // If it fails to open the port, return FALSE.
      if ( hPort == INVALID_HANDLE_VALUE ) 
      {
        // Could not open the port.
    	 
       //   MessageBox ( TEXT("Unable to open the port"), TEXT("Error"), MB_OK);
       AfxMessageBox (TEXT("Unable to open the port"),  MB_OK,0);
     
    	dwError = GetLastError ();
        return FALSE;
      }
     
      PortDCB.DCBlength = sizeof (DCB);     
    
      // Get the default port setting information.
      GetCommState (hPort, &PortDCB);
    
      // Change the DCB structure settings.
      PortDCB.BaudRate =19200 ;//115200;         // Current baud 
      PortDCB.fBinary = TRUE;               // Binary mode; no EOF check 
     
      PortDCB.fParity = FALSE;//TRUE;               // Enable parity checking. 
      PortDCB.fOutxCtsFlow = FALSE;         // No CTS output flow control 
      PortDCB.fOutxDsrFlow = FALSE;         // No DSR output flow control 
      PortDCB.fDtrControl =DTR_CONTROL_ENABLE;// DTR_CONTROL_DISABLE;//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_ENABLE;// DTR_CONTROL_DISABLE;//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 = NOPARITY;            // 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 (hPort, &PortDCB))
      {
        // Could not configure the serial port.
         AfxMessageBox (TEXT("Unable to configure the serial port"), 
                    MB_OK,0);
    	  dwError = GetLastError ();
       return FALSE;
      }


    when i open the port i recevive this error:

    Unable to configure the serial port

    Some tips for me?

    thanks

    Tuesday, November 27, 2012 1:52 PM

All replies

  • Take a look at:  http://geekswithblogs.net/BruceEitman/archive/2008/06/23/windows-ce-device-driver-index-greater-than-9.aspx

    Basically, you need to use "COM2:" or "$device\\COM2"


    Bruce Eitman (eMVP)
    Senior Engineer
    Bruce.Eitman AT Eurotech DOT com
    My BLOG http://geekswithblogs.net/bruceeitman

    Eurotech Inc.
    www.Eurotech.com

    Tuesday, November 27, 2012 2:24 PM
    Moderator
  • thanks it work!
    Tuesday, November 27, 2012 2:38 PM
  • Bruce it's right of course: I was wondering why the error you report is "Unable to configure the serial port" rather than "Unable to open the port"

    Luca Calligaris lucaDOTcalligarisATeurotechDOTcom www.eurotech.com Check my blog: http://lcalligaris.wordpress.com

    Tuesday, November 27, 2012 2:42 PM
  • OPEN_ALWAYS is the cause there. If the named "file" doesn't exist, it's created. Ports/drivers should always be opened with OPEN_EXISTING. If it doesn't exist you want the Open to fail!

    Paul T.

    Tuesday, November 27, 2012 3:37 PM
  • Yep, Paul it's absolutely right. And his explains also why CreateFile does not fail and SetCommState does of course. I looked at the flags but I didn't notice...

    Luca Calligaris lucaDOTcalligarisATeurotechDOTcom www.eurotech.com Check my blog: http://lcalligaris.wordpress.com


    Tuesday, November 27, 2012 3:47 PM