
136 Chapter 9: Putting It All Together
The receive method getchar is similar to putchar. It starts by checking if the
receive buffer is empty. If so, an error code is returned. Otherwise, one byte of
data is removed from the receive buffer and returned to the caller. The gets
method calls getchar repeatedly until either a newline character is found or there
is no more data available at the serial port. It then returns whatever string was
found up to that point. The code for both of these methods follows:
/**********************************************************************
*
* Method: getchar()
*
* Description: Read one character from the serial port.
*
* Notes:
*
* Returns: The next character found on this input stream.
* -1 is returned in the case of an error.
*
**********************************************************************/
int
SerialPort::getchar(void)
{
int c;
if (pRxQueue->isEmpty())
{
return (-1); // There is no input data available.
}
int rxStalled = pRxQueue->isFull();
//
// Read the next byte out of the receive FIFO.
//
c = pRxQueue->remove();
//
// If the receive engine is stalled, restart it.
//
if (rxStalled)
{
scc.rxStart(channel);
}
return (c);
} /* getchar() */