Thread: Win API serial communication - how to check buffer is empty?

  1. #1
    Registered User
    Join Date
    Nov 2012

    Win API serial communication - how to check buffer is empty?

    I am sending a number of msgs from different sources one after the other and it seems like it will start sending the next msg before it finished the 1st one. My guess is that the send buffer is not empty yet. I tried (optimistically?) to check if all the chars were sent in the 2nd line of code, this did not work. What is the right way to do this? Thanks!


  2. #2
    Registered User
    Join Date
    Jun 2005
    Note to mods: this is a windows programming problem, not a C programming problem. Suggest moving to the right forum.

    From the information you've given, there is no obvious problem.

    Have you checked that hSerial was successfully opened?

    Have you ensured that TXBuff0 corresponds to a large enough buffer, and also ensured it is followed by a character with value zero? If not, you could be getting a buffer overrun.

    How are you detecting that it is "sending the next msg before it finished the 1st one"? If that code is expecting to use standard string functions (strlen(), strcpy(), strcat(), etc) that code will need to append a zero character to the end of what it reads (since you are not writing the terminating zero).

    Why aren't you checking the return value of WriteFile()? It returns zero if it fails, and additional information can then be retrieved using the GetLastError() function.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Thanks for the quick comment. I started suspecting it might not finish the previous buffer because when I step through the code in debug it works well and this is only changing the timing of the execution so I figured this got to do with the previous msg not finishing to send. However the while loop should prevent it no? I also checked the # bytes sent variable and all were sent.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Serial Communications in Win32
    Read about EV_TXEMPTY
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial communication
    By cbrandy in forum C Programming
    Replies: 2
    Last Post: 02-24-2012, 07:50 AM
  2. Serial Communication in C++
    By NewGuy100 in forum C++ Programming
    Replies: 8
    Last Post: 04-24-2006, 01:56 PM
  3. Serial Communication
    By Korn1699 in forum C# Programming
    Replies: 0
    Last Post: 11-29-2005, 12:50 PM
  4. Serial Communication Help
    By NewGuy100 in forum C Programming
    Replies: 4
    Last Post: 07-28-2005, 09:15 AM
  5. serial communication
    By hick.hack in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2003, 12:18 PM