Non-blocking ReadFile()?

Posted on 2005-05-01
Last Modified: 2012-05-05
Hi guys,

I'm writing an app in C/++ which implements some serial communication over Bluetooth.

Firstly, when a serial connection is established between two devices using (virtual) Com port COM4: I use the CreateFile() function to obtain a handle. I then use the WriteFile() function to write a custom packet of 64 bytes to the com port. This works fine, and the data is received at the other end. The other device then responds with a 64 byte packet. This also works fine BUT only when the 2nd device responds. If the 2nd device does not respond (it may not have the serial app software running) then the ReadFile() function halts the program on the 1st device - there is no data to be read.

What is the easiest way to allow my program to skip ReadFile() if no data is waiting to be read?
Maybe allowing for a slight delay in communication so an active device has time to send data to the ReadFile() function?

I have been looking at a number of ways e.g. ReadFileEx, Overlapping and creating a new threads - but I'm not having much success.

Any help will be gratefully received,

Question by:tr309
    LVL 9

    Accepted Solution


    Author Comment

    Fantastic. I would never have found this. I wish I could give you more points but I need to start answering questions before I can do that.
    LVL 9

    Expert Comment

    no prob. that wasn't much of work for me. experience counts. and yeah, thanks for points.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now