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

  • Home
  • SEARCH
  • 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 8929955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:46:53+00:00 2026-06-15T08:46:53+00:00

I have a thread that constantly looks for new data and if the data

  • 0

I have a thread that constantly looks for new data and if the data is not already in the serial buffer, ReadFile and GetOverlappedResult seem to tell me there’s data, and that it read it, but not transfer it into my buffer…

func read()
{
    if(state == 0)
    {
        memset(bytes, '\0', sizeof(amount_to_read));

        readreturn = ReadFile(h, bytes, amount_to_read,NULL, osReader);
        if(readreturn <= 0)
        {
            errorcode = GetLastError();
            if(errorcode != ERROR_IO_PENDING)
            {
                SetEAIError(ERROR_INTERNALERROR);
                return -1;
            }
        }
    }

    if (GetOverlappedResult(h, osReader, &dwRead, FALSE) == false)
    {
        errorcode = GetLastError();

        if (errorcode == ERROR_IO_INCOMPLETE || errorcode == 0)
        {
            if(dwRead > 0)
            {
                return 1;
            }
            //timeout
            SetEAIError(ERROR_EAITIMEOUT);
            return -1;
        }
        else
        {
            //other error 
            SetEAIError(ERROR_WIN_ERROR);
            return -1;
        }
    }
    else
    {
        //read succeded, check if we read the amount required
        if(dwRead != amount_to_read)
        {
            if(dwRead == 0)
            {
                //nothing read, treat as timeout
                SetEAIError(ERROR_EAINOREAD);
                return -1;
            }
            else
            {
                //memcpy_s(bytes, sizeof(bytes), readbuf, dwRead);
                SetEAIError(ERROR_PARTIALREAD);
                *_bytesRead = dwRead;
                return -1;
            }
        }
        else
        {
            if(strlen((char*)bytes) == 0)
            {
                //nothing read, treat as timeout
                SetEAIError(ERROR_EAINOREAD);
                return -1;
            }
            //memcpy_s(bytes, sizeof(bytes), readbuf, dwRead);
            *_bytesRead = dwRead;
            return 0;
        }
    }
}

This is what the error codes mean:

  • ERROR_TIMEOUT – switches the state to 1 so that it does not read again, which calls GetOverlappedResult again

  • INTERNALERROR,ERROR_EAINOREAD – it resets state to 0

  • ERROR_PARTIALREAD – starts a new read with new amount of bytes to read

If I swtich GetOverlappedResult to blocking (pass TRUE) it works every time.
If I switch my thread to only read when I know there is data there it works every time.

But if there is not data there, when there is data there it seems to “lose” the data, it my read amount parameter dwRead shows the correct number of bytes read (can see it read with a port monitor) but the bytes are not stored in my char*.

I constantly get ERROR_EAINOREAD

What am I doing wrong?

I do not want to use flags, I want to just use ReadFile and GetOverlappedResult, I should be able to accomplish this with the code I have……. I assume

  • 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-06-15T08:46:53+00:00Added an answer on June 15, 2026 at 8:46 am

    The problem was exactly what was stated the data was getting lost… the REASON it was getting lost is because the bytes parameter passed into the readfile is a local variable in the parents thread. being local it gets re initialized each cycle so after I come into the read again, skip the readfile and go to the overlappedresults, I am now potentially working with a different area of memory

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

Sidebar

Related Questions

I have a background worker thread that is constantly syncing data to/from a remote
I have one thread that is receiving data over a socket like this: while
I have a background thread that is sending data about the application's current status
I have a thread that does the following: 1) Do some work 2) Wait
I have a thread that downloads some images from internet using different proxies. Sometimes
I have a thread that sends GPS coordinates to a database every six seconds
* So I have a Thread that holds a sleep method and I think
I currently have a thread that I created using CreateRemoteThread(). Everything works great. Upon
In the application I'm developing, I have a thread that is running in a
I have a background thread that handles communication with an external service. Each time

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.