Like Tree1Likes
  • 1 Post By grumpy

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

This is a discussion on Win API serial communication - how to check buffer is empty? within the Windows Programming forums, part of the Platform Specific Boards category; I am sending a number of msgs from different sources one after the other and it seems like it will ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    6

    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!

    Code:
    WriteFile(hSerial,TXBuff0,strlen(TXBuff0),&dwBytesSent,NULL);
    while(dwBytesSent!=strlen(TXBuff0)){};

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,200
    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.
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 06: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, 11:50 AM
  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, 11:18 AM

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