Page 177 - ARM Based Microcontroller Projects Using MBED
P. 177
8.8 PC SERIAL INTERFACE 163
TABLE 8.2 Serial Communication Functions
Function Description
pc.printf() Write a formatted string
pc.putc() Write a character
pc.getc() Read a character
pc.scanf() Read a formatted string
pc.readable() Determine if there is something to read
pc.writeable() Determine if it is possible to write
pc.baud() Set the speed (baud rate) of communication
pc.format() Set other communication parameters
8.8.2 Mbed Serial Port Functions and Display Control Characters
Mbed supports a number of functions and display control characters that can be useful in
PC-based serial display programs. Table 8.2 presents the functions that can be used in serial
communication.
printf is probably one of the most commonly used functions as it is used to display text or
numeric data on the PC screen. putc and getc are usually used in pairs to write and read char-
acters from the keyboard, respectively. For example, we can use the getc and putc function
combinations as follows to read and echo characters on the PC screen:
MyPC.putc(MyPC.getc());
scanf is another useful function that can be used to read formatted strings from
the keyboard. Functions readable and writeable determineifitispossibletoread
or write, respectively. baud is used to set the communication speed (default 9600).
Finally, format sets the other communication parameters such as the data width,
parity, etc.
In addition to the serial communication function, we can use the following cursor control
characters when sending data to the screen:
\n Generate newline (at the same column position)
\r Generate carriage return (beginning of the current line)
\t Generate tab
\b Generate backspace