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_hKillInputThreadEven
t.waitDequ
eueNotific
ation(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