Serial port ReadFile problem

This is a discussion on Serial port ReadFile problem within the Windows Programming forums, part of the Platform Specific Boards category; I'm having a problem with the serial port code. I just do: opencomm(); send(); closecomm(); and the ClearCommError() (inside recv()) ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    Serial port ReadFile problem

    I'm having a problem with the serial port code.

    I just do:
    opencomm();
    send();
    closecomm();

    and the ClearCommError() (inside recv()) returns in
    comstat.cbInQue the same amount that was sent.

    So, if sizeof (sendbuff) is 100,
    I get 100 in comstat.cbInQue.

    After reading one byte with ReadFile,
    comstat.cbInQue decrements.
    (after subsequent ClearCommError() of course)

    The values read are not the ones that were written.
    There is no device connected to the port.

    The same code works in another app,
    so I'm puzzled why it doesn't work here.

    Aleksa


    Code:
    static HANDLE hComm; 
    
    static void opencomm (void) 
    { 
        static COMMTIMEOUTS timeouts = {0,0,0,0,0}; 
    
        static DCB dcb = { 
            sizeof (DCB),   //   DCBlength 
            115200, // * BaudRate 
            1,      //   fBinary 
            0,      // * fParity 
            0,      //   fOutxCtsFlow 
            0,      //   fOutxDsrFlow 
            0,      //   fDtrControl 
            0,      //   fDsrSensitivity 
            1,      //   fTXContinueOnXoff 
            0,      //   fOutX 
            0,      //   fInX 
            0,      //   fErrorChar 
            0,      //   fNull 
            0,      //   fRtsControl 
            0,      //   fAbortOnError 
            0,      //   fDummy2 
            0,      //   wReserved 
            8*k,    //   XonLim 
            2*k,    //   XoffLim 
            8,      // * ByteSize 
            0,      // * Parity 
            0,      // * StopBits 
            0,      //   XonChar 
            0,      //   XoffChar 
            0,      //   ErrorChar 
            0,      //   EofChar 
            0,      //   EvtChar 
            0       //   wReserved1 
        }; 
    
            hComm = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); 
            if (hComm != INVALID_HANDLE_VALUE) { 
                SetupComm(hComm, 16*k, 16*k); 
                SetCommState(hComm, &dcb); 
                SetCommTimeouts(hComm, &timeouts); 
            } 
    } 
    
    static void closecomm (void) 
    { 
            CloseHandle(hComm); 
    } 
    
    static BYTE recv (void) 
    { 
        BYTE text; 
        DWORD temp; 
    
        COMSTAT comstat; 
    
            while (1) { 
                ClearCommError(hComm, &temp, &comstat); 
                if (comstat.cbInQue != 0) break; 
    
                Sleep(1); 
            } 
    
            ReadFile(hComm, &text, 1, &temp, NULL); 
    
            return text; 
    } 
    
    static void send (void) 
    { 
        DWORD temp; 
    
            // send to other comp 
            WriteFile(hComm, sendbuff, sizeof (sendbuff), &temp, NULL); 
    
            // check other comp done 
            if (recv() != 0xAA) { 
                Beep(1000, 100); 
                quit(); // comm error 
            } 
    }

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Any idea what could have happen to my system?
    I've tried two older programs that have the same code and they both don't work anymore.

    BTW, the first 10 bytes I receive are FE FE 33 FE FF FF FF FF 9F FE
    does that mean something to someone?

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    It was the cable. Not properly shielded and too long.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Well there is such a thing as
    - checking for errors
    - reading the manual, say -> SetCommState function (Windows)
    The SetCommState function fails if the XonChar member of the DCB structure is equal to the XoffChar member.
    This is exactly what you have in YOUR DCB.

    I suggest you use GetCommState() to get a valid state, then make the changes you need.

    And I want to see plenty of GetLastError calls in your next reply.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Thanks, I'll change XoffChar to, say, 1.
    BTW, at least on XP SP3, SetCommState works even when XonChar == XoffChar.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial Port problem in C
    By andson870420 in forum C Programming
    Replies: 1
    Last Post: 12-19-2011, 10:31 PM
  2. Problem with Serial I/O to RS-232 Port
    By rpinsky in forum C Programming
    Replies: 17
    Last Post: 10-04-2010, 10:20 AM
  3. Serial Port - Problem with binary frames
    By estratos in forum Linux Programming
    Replies: 2
    Last Post: 01-18-2006, 05:22 AM
  4. Problem with string and serial port
    By collinm in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 09:19 AM
  5. Weired serial port problem
    By SuperNewbie in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2003, 04:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21