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 7504759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:40:42+00:00 2026-05-29T21:40:42+00:00

I am reading a Bluetooth COM port, getting data from a microcontroller. After initializing

  • 0

I am reading a Bluetooth COM port, getting data from a microcontroller.
After initializing the port, I am using this to read the port:

while(1)
if(WaitCommEvent(com_hnd, &dwEventMask, NULL))

This works fine, but if I close the COM port, the program will wait forever in this loop, without detecting the fact that the COM port is gone (I have an else for that if(), where I check for errors).

How can I check and see if it still open?

  • 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-29T21:40:47+00:00Added an answer on May 29, 2026 at 9:40 pm

    As already suggested in comments, you will probably want some external way to stop the waiting, like an event + WaitForMultipleObjects(). You can also wait for the actual COM port handle obtained from CreateFile.

    Do you actually need to use WaitCommEvent? As far as I know, you should only need that function if you are using handshaking signals and other such oddities. You can use ReadFile if you are only interested in the data. In my experience, that function is more reliable.

    I have worked with similar COM port devices and the most robust way to spot that they have been disconnected seems to be checking the result of ReadFile:

    BOOL result;
    
    result = ReadFile(...);
    
    if(result == FALSE)
    {
      DWORD last_error = GetLastError();
      if(last_error == ERROR_OPERATION_ABORTED)  // disconnected ?
      {
        close();                                 // close the port
      }
    }
    

    I’m of course not sure whether or not this will work in your specific case, but I’m using this method in some production code that has ended up with multiple crappy USB-to-serial adapters.

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

Sidebar

Related Questions

I was reading this http://developer.android.com/guide/topics/wireless/bluetooth.html#QueryingPairedDevices which is allot of help on how to pair,connect
I have a BluetoothSocket that I am reading data from, using a separate Thread
I have a thread reading data from a bluetooth stream that sends the data
My App recieves a neverending datastream from a bluetooth device. I am reading this
After reading this post and some derivative publications (ddotdash.com) I wonder whether it is
Reading from http://developer.android.com/guide/topics/wireless/bluetooth.html , The current Android Bluetooth API's require devices to be paired
Reading code from other posts, I'm seeing something like this. struct Foo { Foo()
Reading this question I found this as (note the quotation marks) code to solve
Reading over the responses to this question Disadvantages of Test Driven Development? I got
Reading through this question on multi-threaded javascript, I was wondering if there would be

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.