Serial

Serial is a generic protocol that computers and electronic modules use 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 uses the mbed USB port, allowing you to easily communicate with your host PC.

API

API summary

 
 /* mbed Microcontroller Library
  * Copyright (c) 2006-2013 ARM Limited
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 #ifndef MBED_SERIAL_H
 #define MBED_SERIAL_H
 
 #include "platform/platform.h"
 
 #if defined (DEVICE_SERIAL) || defined(DOXYGEN_ONLY)
 
 #include "Stream.h"
 #include "SerialBase.h"
 #include "PlatformMutex.h"
 #include "serial_api.h"
 
 namespace mbed {
 /** \addtogroup drivers */
 
 /** A serial port (UART) for communication with other serial devices
  *
  * Can be used for Full Duplex communication, or Simplex by specifying
  * one pin as NC (Not Connected)
  *
  * @note Synchronization level: Thread safe
  *
  * Example:
  * @code
  * // Print "Hello World" to the PC
  *
  * #include "mbed.h"
  *
  * Serial pc(USBTX, USBRX);
  *
  * int main() {
  * pc.printf("Hello World\n");
  * }
  * @endcode
  * @ingroup drivers
  */
 class Serial : public SerialBase, public Stream {
 
 public:
 #if DEVICE_SERIAL_ASYNCH
  using SerialBase::read;
  using SerialBase::write;
 #endif
 
  /** Create a Serial port, connected to the specified transmit and receive pins
  *
  * @param tx Transmit pin
  * @param rx Receive pin
  * @param name The name of the stream associated with this serial port (optional)
  * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
  *
  * @note
  * Either tx or rx may be specified as NC if unused
  */
  Serial(PinName tx, PinName rx, const char *name=NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
 
 
  /** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud
  *
  * @param tx Transmit pin
  * @param rx Receive pin
  * @param baud The baud rate of the serial port
  *
  * @note
  * Either tx or rx may be specified as NC if unused
  */
  Serial(PinName tx, PinName rx, int baud);
 
  /* Stream gives us a FileHandle with non-functional poll()/readable()/writable. Pass through
  * the calls from the SerialBase instead for backwards compatibility. This problem is
  * part of why Stream and Serial should be deprecated.
  */
  bool readable()
  {
  }
  bool writable()
  {
  }
  bool writeable()
  {
  }
 
 protected:
  virtual int _getc();
  virtual int _putc(int c);
  virtual void lock();
  virtual void unlock();
 
  PlatformMutex _mutex;
 };
 
 } // namespace mbed
 
 #endif
 
 #endif
Definition: Stream.h:39
Definition: PlatformMutex.h:28
Serial(PinName tx, PinName rx, const char *name=NULL, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
virtual void unlock()
virtual void lock()
Definition: Serial.h:52
void baud(int baudrate)
Definition: SerialBase.h:41
Definition: AnalogIn.h:27

Note: On a windows machine, you will need to install a USB serial driver. See Windows serial configuration.

Serial channels have a number of configurable parameters:

  • Baud Rate - There are a number of standard baud rates ranging from a few hundred bits per second, to megabits per second. The default setting for a serial connection on the mbed microcontroller is 9600 baud.
  • Data length - Data transferred can be either 7 or 8 bits long. The default setting for a serial connection on the mbed microcontroller is 8 bits.
  • Parity - You can add an optional parity bit. The parity bit will be automatically set to make the number of 1s in the data either odd or even. Parity settings are Odd, Even or None. The default setting for a serial connection on the mbed microcontroller is None.
  • Stop Bits - After data and parity bits have been transmitted, one or two stop bits are inserted to “frame” the data. The default setting for a serial connection on the mbed microcontroller is one stop bit.

The default settings for the mbed microcontroller are described as 9600-8-N-1, a common notation for serial port settings.

Hello World!

 


/* mbed Example Program
 * Copyright (c) 2006-2014 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "mbed.h"
 
Serial pc(USBTX, USBRX); // tx, rx
 
int main() {
    pc.printf("Hello World!\n\r");
    while(1) {
        pc.putc(pc.getc() + 1); // echo input back to terminal
    }
}

Examples

Example one

Write a message to a device at a baud rate of 19200.

 


#include "mbed.h"

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

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

Example two

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

 


#include "mbed.h"
//K64F
Serial pc(USBTX, USBRX); // tx, rx
Serial device(MBED_CONF_APP_UART1_TX, MBED_CONF_APP_UART1_RX);  // tx, rx

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

Example three

Attach a function to call during the generation of serial interrupts. This function defaults to interrupt on an RX pin.

 


#include "mbed.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);

Serial pc(USBTX, USBRX);

void callback_ex() {
    // Note: you need to actually read from the serial to clear the RX interrupt
    printf("%c\n", pc.getc());
    led2 = !led2;
}

int main() {
    pc.attach(&callback_ex);
    
    while (1) {
        led1 = !led1;
        wait(0.5);
    }
}