Recently changed pages
mbed interface mbed interface
tag interface, mbed
Homepage Homepage
Compiler Tour Compiler Tour
Media Media
Serial Serial
Terminals Terminals
SerialPC SerialPC
From the mbed microcontroller Handbook.

Serial

/media/uploads/mbedofficial/serial_interfaces.png

Serial is a generic protocol used by computers and electronic modules to send and receive control information and data. The Serial link has two unidirection channels, one for sending and one for receiving. The link is asynchronous, and so both ends of the serial link must be configured to use the same settings.

One of the Serial connections goes via the mbed USB port, allowing you to easily communicate with your host PC.

Hello World!

Print to the PC, then pass back characters (slightly modified!)

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx

int main() {
    pc.printf("Hello World!");
    while(1) {
        pc.putc(pc.getc() + 1);
    }
}

API

API summary

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
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

Details

The Serial Interface can be used on mbed pins p9/p10, p13/p14, p28/p27 and USBTX/USBRX

Note that USBTX/USBRX are not DIP pins, they represent the pins that route to the interface USB Serial port.

Serial channels have a number of configurable parameters:

The default settings for the mbed microcontroller are described as 9600 8N1, and this is common notation for Serial port settings.

See Also

Reference

Examples

Write a message to a device at a 19200 baud

#include "mbed.h"

Serial device(p9, p10);  // tx, rx

int main() {
    device.baud(19200);
    device.printf("Hello World\n");
}

Provide a serial pass-through between the PC and an external UART

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx
Serial device(p9, p10);  // tx, rx

int main() {
    while(1) {
        if(pc.readable()) {
            device.putc(pc.getc());
        }
        if(device.readable()) {
            pc.putc(device.getc());
        }
    }
}



calendar Page history
Last modified 28 Jul 2010, by user avatar Simon Ford   tag No tags | 6 replies     Share: Digg Tweet This

6 comments on Serial:

28 Jul 2010

Missing a closing > on <</code>

28 Jul 2010

Thanks! Now fixed...

28 Jul 2010

could someone give an example on using scanf() to receive a string, its not working quite well for me!!

29 Jul 2010

int main() { char string[50]; String 'variable' (strings in C are arrays of chars) Serial pc(USBTX, USBRX); pc.printf("Hello\n"); while (1) { pc.scanf("%s", &string); scanf" the placeholder is '%s' for strings. scanf must be given a pointer where to store the data. The &string represents the address of string. pc.printf("%s", string); } }

Seems to work. Remember that the scanf takes enter or space as the delimiter. Also, make sure you don't try to scan strings longer than the char array or nasty things can happen. And remember the string terminator '\0' that gets placed in the array at the end of the sting. Make sure there is room for it..

3 weeks, 6 days ago

The list of supported baud rates is:

110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600

(from [[http://mbed.org/forum/mbed/topic/893/?page=1#comment-4520|this forum post)

3 weeks, 5 days ago

That list of supported baud rates is only for the USB serial port. The other serial ports can use non-standard baud rates, using the fractional baud rate generation capabilities of the LPC1768/LPC2368.

Please login to post comments.