Serial IO

Key Differences

  • You now use SerialUSB instead of Serial for communication with the serial monitor (or any other terminal emulation software).
  • Re-opening the serial monitor doesn’t reset board.
  • If you reset the board, you must re-open the serial monitor.
  • When the Autonomo is running on a battery, plugging in the USB cable will not reset it.
  • Once the sketch is uploaded it will start executing. This means you may miss any initial messages sent to the serial monitor. See below for a solution to this.

Other Information

The SODAQ Autonomo has two hardware serial connections Serial & Serial1 and one virtual serial port SerialUSB which is used for communicating with a connected PC via the native USB port. Serial is available on pins D0 (RX) & D1 (TX). Serial1 is connected to the Bee socket.

The Serial class inherits from the Stream class and the behaviour is similar to other Stream based classes. Most of the functionality involves reading from and writing to the incoming and outgoing stream buffers. Below is a brief description of some of the methods which you might use for this purpose.

Note: Some of the links here reference the documentation of the parent class Stream.

The Serial Connection

To open a serial connection with a serial connected device, a call must be made to Serial.begin(speed). The parameter speed specifies the bits per second or baud rate. This must match the baud rate of the connected device. Some devices like the GPRSbee however,  can auto detect the baud rate. Similarly a call to Serial.end() will close the connection.

Note: Unlike on the SODAQ AVR boards, it is not necessary to setup the SerialUSB connection for use with the serial monitor. It also appears to run a fixed baud rate, ignoring the baud rate selected in the serial monitor (also in other terminal emulation programs).

Reading from the Serial Connection (Input)

Data which is received from the PC or other serial connected device is stored in the incoming stream buffer. Here are some of the methods available for reading from the incoming stream buffer:

  • read(): Reads and returns a single byte (character) from the incoming stream buffer.
  • readBytes(buffer, length): Copies length number of bytes (characters) from the incoming stream buffer into the parameter buffer.
  • readString(): Reads characters from the incoming stream buffer and returns the data as a String.

Each of these methods read varying amounts of data from the incoming stream buffer and either return that data directly, or in the case ofreadBytes() writes the data into the supplied parameter buffer. The data which is read is also removed from the incoming stream buffer.

These methods are only able to return data when the incoming stream buffer is not empty. Calling them when the incoming stream buffer is empty will likely result in a timeout (unless new data is received before the timeout duration has elapsed). A call to the method available() returns the number of bytes (characters) currently stored in the incoming stream buffer and can used to determine if any data has been received. Additionally, the timeout duration can be set with a call to the method setTimeout().

Writing to the Serial Connection (Output)

Sending data to the PC, or other serial connected device, involves writing data to the outgoing stream buffer. Here are several of the methods which can be used for writing to the outgoing stream buffer:

  • print(val, base): Writes the data from val into the outgoing stream buffer. If val is a numerical type, the optional second parameter base can be used to specify the formatting.
  • println(val, base): Functions the same as the print() method but also appends the carriage return (ASCII 13, or ‘\r’) and a newline characters (ASCII 10, or ‘\n’).

Note: On the SAMD platform there seems to be a bug in when trying to write a buffer that is more than 240 characters long which results in some corruption of the data sent. In cases where the buffer is longer than 247 characters, it can result in the board hanging. An example of this can be found here: https://github.com/GabrielNotman/AutonomoTesting/tree/master/Serial%20IO/testBufferSAMD

Waiting for the Serial Monitor

You can make your sketch wait for the serial monitor by adding the following code to the start of your setup() method:

[/vc_column_text]

Wait for SerialUSB

However, this has a draw back that it will stop the sketch from proceeding unless the SerialUSB connection is opened. The following code adds a timeout.

Note: This will proceed after 10 seconds when running on a battery. However, when the USB cable is plugged in, it will wait until you open the serial monitor (or another terminal emulation program).