Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 693921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:48:13+00:00 2026-05-14T02:48:13+00:00

I’m facing a particular issue that regards serial communication under win32. I’m communicating with

  • 0

I’m facing a particular issue that regards serial communication under win32.
I’m communicating with a device can only accept frames when it is not already communicating. So I must find a valid frame and then inmediatelly send my request.

I developed a class named Serial that handles basic operations on serial port (open, close, read, write) and then a Thread calls inside a loop read and write functions.

Thread loop

//Device is an object of class Serial
while( device->isOpen() && !terminate )
{
    unsigned int readed = 0;
    unsigned long error = ERROR_SUCCESS;
    unsigned char* data = device->read( &readed, &error );

    if( error==ERROR_SUCCESS )
    {
        //If data received, deliver to upper level
        if( readed>0 )
        {
            QByteArray output( (const char*)data, (signed int)readed );
            emit dataArrived( output, readed );
        }
    }
    else
    {
         //unrelated stuff
    }

    //Here I manage the writting issue
    //Only when nothing is received, and Upper layer wants to send a frame
    //(Upper layer only will mark as something to send when it detects a valid frame)
    if( readed==0 )
    {
         out_lock.lock();
         //If something to send...
         if( something_to_send > 0 )
         {
            if( device->write( output_buffer, output_size, &error ) )
            { //things...
            }
         }
     }
}

The Thread basically keeps reading, and when nothing is received, sees if somebody has signaled to send a frame (this means that a valid frame is just received).
When this happens, it writes the frame through serial port.

Here comes my problem.
Inside the Serial::read() function:
I use the overlapped way of reading:

::ClearCommError( handle, &dwErrors, &stat);
if( stat.cbInQue )
{
    //If there's something to read, read it, please note the bytes to read parameter, here 1.
    bool ok = ::ReadFile( handle, buffer_in, 1, &bytes_read, &ov_reader );
    if( !ok )
    {
         DWORD _error = ::GetLastError();
         if( _error == ERROR_IO_PENDING )
         {
             DWORD result = ::WaitForMultipleObjects( 2, waiters, FALSE,INFINITE );
             switch( result )
             {     //Eventshutdown
                   case WAIT_OBJECT_0:  /*code omitted*/break;
                   case WAIT_OBJECT_0+1:   ok = ::GetOverlappedResult( handle, &ov_reader, &bytes_read, true );
                                           //check ok value omitted
                                           break;
             } 
         } 
    }
}

if( bytes_read>0 )
{
   *size = bytes_read;
}

Here starts my problem.
When device sends me small frames (around 30 bytes) everything works fine, but when larger frames are sent, the code is not able to find any free time between frames causing the thread to never be able send any frame because readed is never 0.

If I increase the number of bytes to read inside the read() function, lose the ability to detect when the device “listens”:
bool ok = ::ReadFile(handle, buffer_in, 50, &bytes_read, &ov_reader );
This happens because my app can receive the end of a frame together with the start of the next one. This behaviour is very common.

In the other hand, if I change the INFINITE argument by a valid timeout in the WaitForMultipleObjects function, I lose data.

So my question basically is… what I’m doing wrong? Why when reading 1 byte each time I don’t find any free time to send my own frames?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:48:14+00:00Added an answer on May 14, 2026 at 2:48 am

    I’m not sure if this will help or not, but since you already have a good idea of how many bytes are in the serial device’s input queue (stat.cbInQue) maybe it would help to read in that many bytes instead of just 1 byte or an arbitrary number of bytes (like 50):

    bool ok = ::ReadFile( handle, buffer_in, stat.cbInQue, &bytes_read, &ov_reader );
    

    Of course, you’d need to make sure that the buffer_in had the capacity for that number of bytes, so there might be some other logic you’d have to add to make sure there’s no buffer overruns.

    Also, because the serial driver and ReadFile() APIs depend heavily on buffering for handling received characters, you might be able to get more precise indications of when characters have been received (and not received) using the
    WaitCommEvent() and SetCommMask() APIs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.