api / mbed / trunk / Serial

Serial

class Serial : public Stream

A serial port (UART) for communication with other serial devices

Example

// Print "Hello World" to the PC

#include "mbed.h"

Serial pc(USBTX, USBRX);

int main() {
    pc.printf("Hello World\n");
}
SerialA serial port (UART) for communication with other serial devices
Functions
SerialCreate a Serial port, connected to the specified transmit and receive pins
baudSet the baud rate of the serial port
formatSet the transmission format used by the Serial port
putcWrite a character
getcRead a character
printfWrite a formated string
scanfRead a formated string
readableDetermine if there is a character available to read
writeableDetermine if there is space available to write a character
attachAttach a function to call whenever a serial interrupt is generated
attachAttach a member function to call whenever a serial interrupt is generated

Functions

Serial

Serial(PinName tx,  
PinName rx,  
const char *name =  NULL)

Create a Serial port, connected to the specified transmit and receive pins

Variables

txTransmit pin
rxReceive pin

Note: Either tx or rx may be specified as NC if unused

baud

void baud(int baudrate)

Set the baud rate of the serial port

Variables

baudrateThe baudrate of the serial port (default = 9600).

format

void format(int bits =  8,
Parity parity =  Serial::None,
int stop_bits =  1)

Set the transmission format used by the Serial port

Variables

bitsThe number of bits in a word (5-8; default = 8)
parityThe parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
stopThe number of stop bits (1 or 2; default = 1)

putc

int putc(int c)

Write a character

Variables

cThe character to write to the serial port

getc

int getc()

Read a character

Variables

returnsThe character read from the serial port

printf

int printf(const char *format,
 ...)

Write a formated string

Variables

formatA printf-style format string, followed by the variables to use in formating the string.

scanf

int scanf(const char *format,
 ...)

Read a formated string

Variables

formatA scanf-style format string, followed by the pointers to variables to store the results.

readable

int readable()

Determine if there is a character available to read

Variables

returns1 if there is a character available to read, else 0

writeable

int writeable()

Determine if there is space available to write a character

Variables

returns1 if there is space to write a character, else 0

attach

void attach(void (*fptr)(void),  
IrqType type =  RxIrq)

Attach a function to call whenever a serial interrupt is generated

Variables

fptrA pointer to a void function, or 0 to set as none
typeWhich serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)

attach

template<typename T> void attach(*tptr,  
void (T::*mptr)(void),  
IrqType type =  RxIrq)

Attach a member function to call whenever a serial interrupt is generated

Variables

tptrpointer to the object to call the member function on
mptrpointer to the member function to be called
typeWhich serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
class Serial : public Stream
A serial port (UART) for communication with other serial devices
Serial(PinName tx,  
PinName rx,  
const char *name =  NULL)
Create a Serial port, connected to the specified transmit and receive pins
void baud(int baudrate)
Set the baud rate of the serial port
void format(int bits =  8,
Parity parity =  Serial::None,
int stop_bits =  1)
Set the transmission format used by the Serial port
int putc(int c)
Write a character
int getc()
Read a character
int printf(const char *format,
 ...)
Write a formated string
int scanf(const char *format,
 ...)
Read a formated string
int readable()
Determine if there is a character available to read
int writeable()
Determine if there is space available to write a character
void attach(void (*fptr)(void),  
IrqType type =  RxIrq)
Attach a function to call whenever a serial interrupt is generated