Main Content

Send and Receive Data of Variable Lengths Using Arduino Serial Communication Blocks

This example shows how to use the Serial Transmit and Serial Receive blocks from the Simulink® Support Package for Arduino® Hardware to transmit and receive data of variable lengths on the Arduino serial port using a USB to serial converter.

Prerequisites

Required Hardware

  • Any Arduino hardware board

  • USB to serial device, for example, FTDI USB to serial device

  • Connecting wires

Install USB to Serial Device Driver

Follow these instructions to install a USB to serial FTDI driver.

  • Connect your system to the Internet, and then connect an FTDI device. If your system is connected to the Internet, the FTDI driver installs automatically once the FTDI device is plugged in.

  • Follow the installation guides to manually install the FTDI drivers.

Connect Arduino and USB to Serial Adaptor

To align the TX and RX pins correctly, as shown in the diagram, connect the TX on the USB to serial device to RX on the Arduino board and RX on the USB to serial device to TX on the Arduino board. If these are misaligned, the USB to serial device can go undiscovered.

View Arduino COM Ports on Host

To view the list of the available Arduino COM ports on your host computer, go to Start > Control Panel > Device Manager > Ports (COM & LPT). Enter this COM port value in the Port parameter in the block masks for the blocks used in this Simulink model. The COM ports shown in this figure is for illustration purpose and might vary for your host computer.

Configure Simulink Model and Calibrate Parameters

Open the arduino_serial_variable_length Simulink model.

This model transmits and receives data of variable length on the Arduino serial port. The Arduino serial port transmits a string of variable length when,

  • The header and terminator that encloses the data matches with the one you configure in the Serial Receive block.

  • The data enclosed in the message matches with the one you configure in the Simulink model.

For example, the Simulink model transmits the data 'Turning ON' to the Arduino serial port when the data received on the serial port is 'STONOFF' where, ST is the header, 'ON' is the data message, and 'OFF' is the terminator. Observe that the data 'ON' is two bytes while, the data 'OFF' is three bytes.

Arduino hardware is connected to the FTDI chip allows serial data transfers over USB with the host computer.

In the Configuration Parameters dialog box of the arduino_serial_variable_length Simulink model, go to Hardware Implementation > Target hardware resources > Serial port properties and set the Serial 3 baud rate to 9600.

Configure these parameters in the Serial Receive Block Parameters dialog box to receive data of variable length with a header and terminator.

  1. Set Data length option to Variable length.

  2. Set Header to ST.

  3. Set Terminator to EN.

Build and Deploy Simulink Model on Arduino Hardware

On the Hardware tab of the arduino_serial_variable_length Simulink model, in the Deploy section, click Build, Deploy & Start.

You can use the serial terminal on your host computer to view the output data. When logging in to the terminal,

  • Specify the COM port number that corresponds to your Arduino serial connection. For more information, see Configure Host and Bootloader COM Port Manually.

  • Specify the baud rate to the same value as the baud rate set in the Configuration Parameters dialog box. In this example, the baud rate is 9600.

After you open the serial terminal, send two bytes of data 'ON' with a header 'ST' and terminator 'EN' in this format: STONEN and observe the output. The string matches with the properties set in the 'If Action Subsystem' and the output 'Turning ON' is displayed on the terminal.

Serial Terminal Output

Similarly, send three bytes of data 'OFF' in this format: STOFFEN and observe that the output 'Turning OFF' is displayed on the terminal.

Serial Terminal Output

You can also create a serialport to view the output in your MATLAB® session.

On the MATLAB Command Window, run this command to create a serialport object.

serialobj = serialport('COM3',9600,'Timeout',2)
serialobj =
Serialport with properties:
               Port: "COM3"
           BaudRate: 9600
                Tag: ""
  NumBytesAvailable: 0
Show all properties, functions

Write data ON to serial port.

serialobj.write('STONEN','uint8')

Read data from serial port.

serialobj.readline
ans =
  "Turning on"

Similarly, write data OFF to serial port.

serialobj.write('STOFFEN','uint8')

Read data from serial port.

serialobj.readline
ans =
  "Turning Off"

See Also

Send and Receive Data Between Arduino and Host Using Serial Communication