MATLAB Examples

Communication

These examples will show how to communicate with other hardware when running a MATLAB Function or Simulink Model directly on a desktop computer.

Contents

Introduction

The Instrument Control Toolbox supports many standard communication protocols (i.e. UDP, Serial and TCP/IP). For the full list of supported communication methods, see the following resource:

Supported Hardware and Protocols

Documentation

Here are a few documentation pages for the most common communication methods in robotics.

Documentation for MATLAB:

Documentation for Simulink:

MATLAB Examples

Serial Communication

Here is an example of how to use the Serial method to transmit and receive binary data. I am performing a loopback test by connecting the TX and RX pins together. You will need to do the same in order for this demo to work.

This usb to serial cable was found on Adafruit's website:

USB to TTL Serial Cable - Debug / Console Cable for Raspberry Pi

In the following code, I will use COM Port 6 because the Device Manager on my Windows computer indicates that the USB to Serial cable is connected to this port.

In Linux, use the following command in the terminal to determine which COM port to use:

dmesg | grep tty

% Initialize
s = serial('COM6'); % set up the serial port to use COM Port 6
fopen(s); % open the COM Port

% Serial Transmit and Receive Binary Data
fwrite(s,22:25); % transmit 3 doubles using serial
out = fread(s,4); % receive 3 doubles using serial
display(out) % display the received data

% Cleanup
fclose(s); % close the serial object
delete(s); % delete the serial object
clear('s'); % clear the serial object
out =

    22
    23
    24
    25

UDP Communication

Here is an example of how to use the UDP method to transmit and receive binary data. To perform the loopback test we use the echoudp function.

% Initialize
echoudp('on',4012) % start the echo server to do a loop back test
u = udp('127.0.0.1',4012); % create a UDP object
fopen(u); % Connect the UDP object to the host.

% UDP Transmit and Receive Binary Data
fwrite(u,22:25) % transmit 3 doubles using udp
out = fread(u,4); % receive 3 doubles using udp
display(out)

% Cleanup
echoudp('off') % stop the echo server
fclose(u); % close the UDP object
delete(u);
clear('u');
out =

    22
    23
    24
    25

Common Syntax

Notice how the syntax, "fread" is the same for serial and udp. Serial, udp and tcpip methods all share the same read and write functions i.e. fread, fwrite, fprintf, fgets. The only difference is the object create method i.e. s = serial, u = udp, t = tcpip.

Ascii

To transmit and receive ascii data instead of binary, use the fprintf and fgets/fgetl functions instead. This applies to all communication methods i.e. udp, tcpip, serial.

Initialize

s = serial('COM6');
fopen(s);

% Transmit and Receive ASCII sentences
fprintf(s,'hello, world = %i\n\n hello, world',42); % Transmit ASCII sentence
out = fgets(s); % Receive ASCII sentence
display(out);

% Cleanup
fclose(s); % close the serial object
delete(s); % delete the serial object
clear('s'); % clear the serial object
out =

hello, world = 42


Clearing Communication Objects

If you ever forget to close the serial or udp object before clearing it, use the instrfindall method to find all visible or hidden instrument objects:

objs = instrfindall;
if ~isempty(objs)
    fclose(objs);
    delete(objs);
    clear('objs');
end

Using Timer Objects with Communication

These examples have so far shown what functions to use and how to call them. They have not shown how to transmit and receive on a schedule i.e. transmit every 0.1 seconds and receive every 0.05 seconds. In MATLAB, scheduling can be performed with timer objects.

Open Example

Simulink Examples

As with the MATLAB ICT serial, udp,and tcpip methods, the corresponding Simulink blocks are all called the same. Therefore I will only show the Serial example with binary and ascii.

The Real-Time Synchronization block from the Simulink Desktop Real-Time library was added to all of the models to ensure that the communication is transmitted on a schedule i.e. transmit every 0.1 seconds, receive every 0.05 seconds. The sample time on the receiver side is usually smaller to ensure that the buffer is emptied quicker than it is filled. This matters when the blocks are not in the same model and are not transmitting and receiving at the same exact rates.

Serial Binary

How to send serial binary data:

Open Example


Serial Ascii

How to send serial ascii data. The trick is to use the Simulink Real-Time's ASCII Encode and Decode blocks.

Open Example