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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:09:18+00:00 2026-05-13T06:09:18+00:00

I have an application which runs on a controlling hardware connected with different sensors.

  • 0

I have an application which runs on a controlling hardware connected with different sensors. On loading the application, it checks the individual sensors one by one to see whether there is proper communication with the sensor according to predefined protocol or not.

Now, I have implemented the code for checking the individual sensor communication as a singleton thread and following is the run function, it used select system call and pipe for interprocess communication to signal the end of thread.

void SensorClass::run()
{
    mFdWind=mPort->GetFileDescriptor();
    fd_set readfs;
    int max_fd = (mFdWind > gPipeFdWind[0] ? mFdWind : gPipeFdWind[0]) + 1;
    int res;

    mFrameCorrect=false;
    qDebug("BEFORE WHILE"); 
    while(true)
    {
        qDebug("\n IN WHILE LOOP"); 
        usleep(50);
        FD_ZERO(&readfs);
        FD_SET(mFdWind,&readfs);
        FD_SET(gPipeFdWind[0],&readfs);

        res=select(max_fd,&readfs,NULL,NULL,NULL);
        if(res < 0)
            perror("Select Failed");
        else if(res == 0)
            puts("TIMEOUT");
        else
        {
            if(FD_ISSET(mFdWind,&readfs))
            {
                puts("*************** RECEIVED DATA ****************");
                mFrameCorrect=false;
                FlushBuf();
                //int n=mPort->ReadPort(mBuf,100);
                int n=mPort->ReadPort(mBuf,100);

                if(n>0)
                {
                    Count++;


                    QString str((const char*)mBuf);
                    //qDebug("\n %s",qPrintable(str));
                    //See if the Header of the frame is valid
                    if(IsHeaderValid(str))
                    {
                        if( (!IsCommaCountOk(str)) || (!IsChecksumOk(str,mBuf)) ||  (!CalculateCommaIndexes(str))  ) 
                        {

                            qDebug("\n  not ok");
                            mFrameCorrect=false;
                        } //if frame is incorrect
                        else 
                        {
                            qDebug("\n  OK");
                            mFrameCorrect=true;
                        }//if frame is correct(checksum etc are ok)
                    }//else if header is ok
                }//if n > 0
            }//if data received FD_ISSET
            if(FD_ISSET(gPipeFdWind[0],&readfs))
                break;
        }//end nested else res not <= 0
    }//infinite loop  
}

The above thread is run started from the main GUI thread. This runs fine. The problem is I have given an option to the user to retest the subsystem at will. For this I delete the singleton instance using

delete SensorClass::instance();

and then restart the singleton using

SensorClass::instace()->start();

The problem is this time the control comes out of while loop in run() function immedeately upon entering the while loop, my guess is the pipe read has again read from the write pipe which was written to the last time. I have tried to use the fflush() to clear out the I/O but no luck.
My question is

  1. Am I thinking on the right track?

  2. If yes then how do we clear out the pipes?

  3. If not can anyone suggest why is the selective retest not working?

Thanks in advance..

  • 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-13T06:09:18+00:00Added an answer on May 13, 2026 at 6:09 am

    fflush clears the output buffer. If you want to clear the input buffer, you’re going to need to read the data or seek to the end.

    I’m not convinced the “Singleton” pattern is appropriate. There are other ways of ensuring at most one instance for each piece of hardware. What if you later want multiple threads, each working with a different sensor?

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

Sidebar

Related Questions

We have a WinForms application which runs on a touch-screen on a bit of
I have an .Net MVC application which runs fine if I use the build
I have a medium sized application that runs as a .net web-service which I
I have an application which runs using the context menu of windows explorer. I
We have an application which runs in a JRE environment. The application uses some
I have an application which runs on a UINavigationController. Now I would like to
I have an application which runs a tool that requires network connection. Now my
I have a application which runs over browser ...php application... i need to lock
I have an application which has a loop, part of a Scheduler, which runs
I have an application which really should be installed, but does work fine when

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.