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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:50:37+00:00 2026-06-09T05:50:37+00:00

I am interfacing with a device, and need a way to constantly check if

  • 0

I am interfacing with a device, and need a way to constantly check if any bytes are available to be read by the serialport, and if so I need to read them and perform different functions. I can’t create a data_received event for a number of reasons due to the project I am working on. As of now I have an infinite for loop running in a new thread, but it is causing the computer to slow way down.

Does anyone have any other ideas? Any help is appreciated. Below is the for loop code I am currently using.

void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        object[] parameters = (object[])e.Argument;
        SerialPort port = (SerialPort)parameters[0];
        agentRadio agentR = (agentRadio)parameters[1];

        for (; ; )
        {
            if (port.IsOpen)
            {
                try
                {
                    if (port.BytesToRead > 0)
                    {
                        byte value = (byte)port.ReadByte();

                        if (value == Global.OFF_CHAR)
                        {
                            port.Close();
                            Global.isConnected = false;
                            Form form = new Form();
                            form.deviceDisconnect(agentR);

                            MessageBox.Show("Your device has disconnected on its own, due to inactivity or because a key was switched off.", "Device Disconnect", MessageBoxButton.OK, MessageBoxImage.Information);

                            break;
                        }
                        else if (value == Global.WARNING_CHAR[0])
                        {
                            if (Global.activeClick)
                            {
                                port.BaseStream.Write(Global.POLL_CHAR, 0, Global.POLL_CHAR.Length);
                            }
                        }
                    }
                }
                catch {  }
            }
            else
            {
                break;
            }
        }
    }
  • 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-09T05:50:38+00:00Added an answer on June 9, 2026 at 5:50 am

    Get rid of the horrible ‘if (port.BytesToRead > 0)’ polling loop! Why do you do that – you do nothing else if the check retuns false, so just let the read block in the normal way.

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

Sidebar

Related Questions

In a pure C++ world we can generate interfacing or glue code between different
I am interfacing an embedded device with a camera module that returns a single
in my project I need to work with device drivers, but have a hard
I am interfacing with an external device which is sending data in hex format.
Does anyone know of any design patterns for interfacing with relational databases? For instance,
If we check the official documentation we can find various ways of interfacing Erlang
I'm interfacing with a payment gateway and not having any luck with Net::SSLeay and
Any idea on interfacing with AutoCAD through a JAVA program. I am looking for
I am interfacing with a server that requires that data sent to it is
I'm interfacing to a WCF web service that exposes its method using SOAP, using

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.