Infinite loop in WriteFile for a Serial Port

This is a discussion on Infinite loop in WriteFile for a Serial Port within the Windows Programming forums, part of the Platform Specific Boards category; Hi, guys. Long time no see. It's been like 1 year or so when I last posted here. Well, mainly ...

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    Infinite loop in WriteFile for a Serial Port

    Hi, guys. Long time no see. It's been like 1 year or so when I last posted here. Well, mainly in the C++ forum anyway. BTW, now I've got an issue. It's actually still a C++ one, but IMHO it's Windows related. I wanted to send a string to a COM port using the usual WriteFile command. But I don't know why, it's starting to loop endlessly (IMHO). The code is:
    Code:
    	DWORD len;
    	DWORD dataLen = (DWORD) strlen( p_data );
    
    	std::cout<<"Data to be sent:"<<p_data<<std::endl;
    
            if( WriteFile( m_hComm, p_data, dataLen,
    		&len, NULL ) == 0 )
            {
    
    		MessageBox( NULL, "NO: Failed sending data.", "Debug", MB_OK );
                    return false;
             }
    	MessageBox( NULL, "YES: Data sent.", "Debug", MB_OK );
    When I started the code, it won't even display either one of the message boxes. What's wrong with it? Thanks in advance.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Where's the loop ?
    Maybe call GetLastError and return some additional useful information in your fail messages.
    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.

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by Salem View Post
    Where's the loop ?
    Maybe call GetLastError and return some additional useful information in your fail messages.
    The loop is in the WriteFile itself. So, I guess the word loop is not that accurate. More like it hangs if it reaches the WriteFile part. Anyway, it looks like I managed to solve it myself though. After I set the COMMTIMEOUTS.WriteTotalTimeoutConstant to 2000, it can exit the hangs after 2 secs. Sorry for the trouble guys.
    Last edited by g4j31a5; 07-15-2008 at 12:14 AM.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, if you don't set a timeout, it won't exit WriteFile until the data has been sent. In this case, I expect that you have some sort of flow control problem - perhaps your cable isn't wired up right, or you have one end set for CTS/RTS flow control, and the other end isn't set the same - so the other end never says "Ok, you can go ahead and give me some data now", and thus your WriteFile does exactly what it has been told to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by matsp View Post
    Yes, if you don't set a timeout, it won't exit WriteFile until the data has been sent. In this case, I expect that you have some sort of flow control problem - perhaps your cable isn't wired up right, or you have one end set for CTS/RTS flow control, and the other end isn't set the same - so the other end never says "Ok, you can go ahead and give me some data now", and thus your WriteFile does exactly what it has been told to do.

    --
    Mats
    Yeah, I understand now. Thanks for pointing that out. BTW, a little OOT. I don't know what's wrong with the serial device but it seems like the input / output from / to the device is not always in the correct order. The weird thing is, I'm pretty sure that I've set the configuration just as the middleware programmer has told me (ie. 9600, 8N1, no flow control). Any other idea as of what may cause this issue? Thanks again.
    Last edited by g4j31a5; 07-15-2008 at 04:17 AM.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  6. #6
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Nevermind. What seemed like an incorrect order is actually, IMHO, the difference of unicode mode between the application and the serial. I'll have to work it out myself I guess. Thansk anyway.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Serial port programming in Linux
    By g4j31a5 in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2006, 08:24 PM
  4. Serial Port problems
    By BeBu in forum Linux Programming
    Replies: 1
    Last Post: 04-01-2005, 07:18 AM
  5. Problem with string and serial port
    By collinm in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 09:19 AM

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