Advertisement

02.01.2008 at 07:46AM PST, ID: 23129816
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

loosing bytes when reading from a linux serial port ttyS0

Asked by cenetadmin in Linux Programming, C++ Programming Language

Tags: ,

Basically we have a read thread that does the following:

    for ( bool done = false ; !done ; ) {
        //
        // Under normal conditions this loop should have a read action
        // in progress at all times. The only time this won't be true
        // is when there is no room in the RX queue. We have a member
        // in class LinuxPort that defines whether or not a read is
        // presently active. This section of code just makes sure that
        // if no read is currently in progress, we do our best to get
        // one started.
        //
        int bytes_to_read = 0;
        unsigned char read_buffer[ 256 ];
        int iBytesRead;
        if ( !m_bInputThreadReading )
            {
            bytes_to_read = m_RxQueue.SpaceFree();
            if ( bytes_to_read > 256 )
                bytes_to_read = 256;
            //
            // If there is room to add new bytes to the RX queue, and
            // we currently aren't reading anything, we kick off the
            // read right here with a call to ReadFile(). There are two
            // possible things that can then happen. If there isn't any
            // data in the buffer, ReadFile() can return immediately
            // with the actual input in progress but not complete. If
            // there was enough data in the input stream already to
            // fulfill the read, it might return with data present.
            //
            if ( bytes_to_read > 0 )
                  {
//                        fcntl(m_hPort, F_SETFL, FNDELAY); // don't block serial read
                        iBytesRead = read( m_hPort, read_buffer, bytes_to_read );
                        if(iBytesRead < 0)
                        {
                              // The only acceptable error condition is the I/O
                              // pending error, which isn't really an error, it
                              // just means the read has been deferred and will
                              // be performed using overlapped I/O.
                              if(errno == EAGAIN)
                              {
                                    printf("SERIAL EAGAIN ERROR \n");
                              }
                              else
                              {
                                    printf("SERIAL read error %d %s\n",errno, strerror(errno));
                                    m_bInputThreadReading = true;
                              }
                        }
                        else
                        {    
                              // If we reach this point, ReadFile() returned
                              // immediately, presumably because it was able
                              // to fill the I/O request. I put all of the bytes
                              // just read into the RX queue, then call the
                              // notification routine that should alert the caller
                              // to the fact that some data has arrive.
                              if ( iBytesRead )
                              {
                                    m_RxQueue.Insert( (char*)read_buffer, iBytesRead );
                                    RxNotify( iBytesRead );
                              }
                        }
            }
        }
        //
        // We've completed the preliminary part of the loop and we are
        // now read to wait for something to happen. Note that it is
        // possible that either the call to ReadFile() or
        // WaitCommEvent() returned immediately, in which case we aren't
        // actively waiting for data of that event type. If that's true,
        // we have to go back through the loop and try to set up the
        // ReadFile() or WaitCommEvent() again. That's what this first
        // conditional statement is checking for. It would be a simpler
        // statement, but we have to take into account the possibility
        // that we aren't reading because there is no room in the
        // RX queue, in which case we can wait right away.
        if ( m_bInputThreadReading || bytes_to_read == 0 )
            {
                  AutoPtr<Notification> pNf(m_hKillInputThreadEvent.waitDequeueNotification(1));
                  if (pNf)
                  {
                        done = true;
                        break;
                  }
        }
    }

When I debug on Linux, basically there should be 10 byte record coming from the serial device.  First 8 bytes comes through.  When I look at bye 7, I expect to see a 4, instead I see another value.  After another read, I look at the first byte of that read, and it is fine.  What is causing the last value to be bad?

Any assistance would be appreciated.  I am new to linux, but have programmed in windows.Start Free Trial
 
Loading Advertisement...
 
[+][-]02.01.2008 at 08:02AM PST, ID: 20797673

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.01.2008 at 08:02AM PST, ID: 20797677

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.01.2008 at 08:04AM PST, ID: 20797690

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.01.2008 at 08:11AM PST, ID: 20797764

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.01.2008 at 08:43AM PST, ID: 20798124

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.01.2008 at 09:04AM PST, ID: 20798364

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.01.2008 at 09:15AM PST, ID: 20798493

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.01.2008 at 09:29AM PST, ID: 20798618

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.01.2008 at 09:37AM PST, ID: 20798706

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.01.2008 at 10:49AM PST, ID: 20799348

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.01.2008 at 12:49PM PST, ID: 20800544

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.01.2008 at 02:01PM PST, ID: 20801174

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.04.2008 at 07:26AM PST, ID: 20815188

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.04.2008 at 01:08PM PST, ID: 20818021

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Linux Programming, C++ Programming Language
Tags: c++, linux
Sign Up Now!
Solution Provided By: duncan_roe
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628