none
Serial Communication Problem using VC6

    Question

  •  

    Hello,

    I have created an ActiveX control (c++) using VC6 for a serial based device. A test application (MFC) that runs this control. The serial port communication is overlapped event based. The very first ReadFile is failing with error code ERROR_FILE_NOT_FOUND  (The system cannot find the file specified) for the debug version only. The release version works fine. If i step through the debug it works then too. It fails only if i run my test application with debug ActiveX control. I have checked over and over again for my variable initializations and played around with the below timing but nothing seems to work. Please let me know if you any suggestions.

    Here's my ReadFile settings

    NewCommTimeOuts.ReadIntervalTimeout           = 200;
    NewCommTimeOuts.ReadTotalTimeoutMultiplier    = 10;
    NewCommTimeOuts.ReadTotalTimeoutConstant      = 0;
    NewCommTimeOuts.WriteTotalTimeoutMultiplier   = 1000;
    NewCommTimeOuts.WriteTotalTimeoutConstant     = 1000;

    // Close If Any Previous Events
    CloseHandle(m_COMOverlapped.hEvent);

    // Set
    memset((void*)&m_COMOverlapped, 0, sizeof(m_COMOverlapped));

    //Create
    m_COMOverlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

    ReadFile(m_hCom, m_byDataBuffer, 1000, &m_dwReadDataLen, &m_COMOverlapped);

     

    Tuesday, June 17, 2008 6:16 PM

Answers

  • There are several things to keep in mind when working with OVERLAPPED.  For one, the ReadFile() call will always return an error when you first call it.  Although it is not ERROR_FILE_NOT_FOUND.  You should also never call ReadFile() again when the previous overlapped read wasn't completed yet.  And you got to keep the read buffer alive while the request is pending, you can't use a buffer on the stack.  And it will work very differently while you are debugging it.  Since you are slowly executing statements one by one, the serial port will have lots of time to complete the request before you check for completion.

    Using somebody else's code might not be a bad idea.  You should be able to google some.  Or use the System::IO::Ports::SerialPort class if managed code is an option.

    Hans Passant.
    • Marked as answer by Yan-Fei Wei Thursday, June 19, 2008 5:26 AM
    Thursday, June 19, 2008 5:15 AM

All replies

  • I'm suspicous about what the ActiveX control does once the data has been read. Do you raise any events? Note that if you do, and your ActiveX control is living in an STA, then you are breaking COM rules.
    Wednesday, June 18, 2008 4:43 PM
  • Thanks for the reply, I am not raising any events. I have tried it with synchronous ReadFile and it works as well too.
    Thursday, June 19, 2008 1:41 AM
  • There are several things to keep in mind when working with OVERLAPPED.  For one, the ReadFile() call will always return an error when you first call it.  Although it is not ERROR_FILE_NOT_FOUND.  You should also never call ReadFile() again when the previous overlapped read wasn't completed yet.  And you got to keep the read buffer alive while the request is pending, you can't use a buffer on the stack.  And it will work very differently while you are debugging it.  Since you are slowly executing statements one by one, the serial port will have lots of time to complete the request before you check for completion.

    Using somebody else's code might not be a bad idea.  You should be able to google some.  Or use the System::IO::Ports::SerialPort class if managed code is an option.

    Hans Passant.
    • Marked as answer by Yan-Fei Wei Thursday, June 19, 2008 5:26 AM
    Thursday, June 19, 2008 5:15 AM
  •  Like i said the very first ReadFile fails and therefore the application exits. Could you please mention what other things are to be kept under consideration beside the one mentioned above while using OVERLAPPED. Maybe that's where i am missing something.

    DCB Setting

    m_dcb.DCBlength = sizeof( DCB );
     m_dcb.BaudRate = 1200;
     m_dcb.ByteSize = 7;
     m_dcb.Parity = EVENPARITY;
     m_dcb.StopBits = ONESTOPBIT;
     m_dcb.fDtrControl = DTR_CONTROL_DISABLE;
     m_dcb.fRtsControl = RTS_CONTROL_DISABLE;
     m_dcb.fBinary = TRUE;
     m_dcb.fParity = TRUE;
     m_dcb.fOutxCtsFlow = FALSE;   
     m_dcb.fOutxDsrFlow = FALSE;
     m_dcb.fDsrSensitivity = FALSE;


    Thanks


    Thursday, June 19, 2008 10:46 PM
  • Like I said, the very first call to ReadFile() fails.  I didn't leave anything out, not sure why you'd assume I did.  Everything is well documented in the MSDN library, take the "overlapped I/O" index entry.
    Hans Passant.
    Thursday, June 19, 2008 11:38 PM