Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Sauermann J.Realtime operating systems.Concepts and implementation of microkernels for embedded systems.1997.pdf
Скачиваний:
29
Добавлен:
23.08.2013
Размер:
1.32 Mб
Скачать

3. Kernel Implementation

69

 

 

{

SerialOut so(ch);

for (unsigned int i = 0; msg.Body[i]; i++) so.Print(ch,"%c ",p[i]);

}

}

}

Now there is only one SerialOut object instead of one for each message character which causes an entire message to be printed. Thus the static Print() is typically used when the item to be printed can be expressed by a single format string, while the non-static Print() is used otherwise.

3.7.4 Public SerialIn Member Functions

The simplest public member function of the SerialIn class is Getc() which returns the next character received on a channel. If no characters are available, then the task calling Getc() is blocked until the next character is received. In contrast to the SerialOut class, Getc() returns useful results only for interrupt driven I/O and indicates EOF (-1) otherwise. Getc() returns int rather than char in order to distinguish the EOF condition from the regular char 0xFF (i.e. -1).

1 /* SerialIn.cc */

...

34int SerialIn::Getc()

35{

36unsigned char cc;

38switch(channel)

39{

40

case SERIAL_0:

inbuf_0.Get(cc);

return

cc;

41

case SERIAL_1:

inbuf_1.Get(cc);

return

cc;

42

default:

return -1;

 

 

43}

44}

If it is not desired to block the task, Pollc() can be used instead. Pollc() returns EOF when Putc() would block the task.

1 /* SerialIn.cc */

...

46int SerialIn::Pollc()

47{

48unsigned char cc;

49

50switch(channel)

51{

52case SERIAL_0: return inbuf_0.PolledGet(cc) ? -1 : cc;

53case SERIAL_1: return inbuf_1.PolledGet(cc) ? -1 : cc;

54

default:

return -1;

55 }

70

3.7 Serial Input and Output

 

 

56 }

Often one wants to receive characters up to, but not including a terminating character; e.g. if decimal numbers of unknown length are entered. UNIX has a unputc() function which undoes the last putc(). We have not adopted this scheme, but instead provide a function Peekc() which works like Pollc(), but does not remove the character from the receive queue. Both the unputc() approach and the Peekc() approach have their advantages and disadvantages, and one can easily implement unputc() in the SerialIn class.

1 /* SerialIn.cc */

...

58int SerialIn::Peekc()

59{

60unsigned char cc;

61

62switch(channel)

63{

64case SERIAL_0: return inbuf_0.Peek(cc) ? -1 : cc;

65case SERIAL_1: return inbuf_1.Peek(cc) ? -1 : cc;

66

default:

return -1;

67}

68}

GetDec() and GetHex() are based on the Pollc() and Peekc() functions and collect decimal (’0’..’9’) or hexadecimal (’0’..’9’,’A’..’F’ and ’a’..’f’) sequences

of characters, and return the resulting integer value. These functions do not necessarily belong to an operating system, but are provided since they are commonly required.

For serial output, characters can never get lost, since tasks performing output would block before the transmit buffer overflows. For serial input however, the receive buffer may overflow, e.g. if no task is performing Getc() for some time. The function getOverflowCounter() returns the number of characters lost due to buffer overflow, and 0 for polled or dummy serial input where this condition can not be easily detected.