Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Program C Ansi Programming Embedded Systems in C and C++ phần 8 ppsx
Nội dung xem thử
Mô tả chi tiết
/**********************************************************************
*
* Method: puts()
*
* Description: Copies the null-terminated string s to the serial
* port and appends a newline character.
*
* Notes: In rare cases, this function may return success though
* the newline was not actually sent.
*
* Returns: The number of characters transmitted successfully.
* Otherwise, -1 is returned to indicate error.
*
**********************************************************************/
int
SerialPort::puts(const char * s)
{
const char * p;
//
// Send each character of the string.
//
for (p = s; *p != '\0'; p++)
{
if (putchar(*p) < 0) break;
}
//
// Add a newline character.
//
putchar('\n');
return ((p - s) + 1);
} /* puts() */
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())
{