Main Content

Communicate Between Two MATLAB Sessions Using User Datagram Protocol

This example shows how to send data over the User Datagram Protcol (UDP) between two MATLAB® sessions on the same computer using the udpport function.

First MATLAB session

Create udpport Instance

Create a udpport instance and bind to port 2020.

uFirst = udpport("LocalPort",2020)
uFirst = 
  UDPPort with properties:

     IPAddressVersion: "IPV4"
            LocalHost: "0.0.0.0"
            LocalPort: 2020
    NumBytesAvailable: 0

  Show all properties, functions

Prepare Callback Function

Configure the callback function to read data received using the configureCallback function. This callback function will trigger whenever a terminator is received. The sendAcknowledgement callback function sends an acknowledgement string back to the udpport instance in the second MATLAB session when it receives data.

Set the Terminator property to "CR/LF", to match that of the other udpport instance.

configureTerminator(uFirst, "CR/LF");

The Terminator property should now be set to "CR/LF".

uFirst.Terminator
ans = 
"CR/LF"

Set up the callback function sendAcknowledgement to trigger when the assigned terminator is received.

configureCallback(uFirst,"terminator",@sendAcknowledgement);

The sendAcknowledgement.m callback function is given below.

function sendAcknowledgement(u, ~)
% Read the data received from the other udpport instance. readline removes
% the terminator from the data read.
data = readline(u);

% Prepare the acknowledgement string.
data = "COMMAND RECEIVED - """ + data +  """. SENDING ACKNOWLEDGEMENT.";

% Send the acknowledgement string, followed by the Terminator "CR/LF", to the
% udpport instance bound to port 3030 in the first MATLAB instance.
writeline(u, data, "127.0.0.1", 3030);
end

The BytesAvailableFcn property should be now set to the callback function, and the BytesAvailableFcnMode set to "terminator".

uFirst.BytesAvailableFcn
ans = function_handle with value:
    @sendAcknowledgement

uFirst.BytesAvailableFcnMode
ans = 
"terminator"

Second MATLAB session

Create udpport Instance

Create a udpport instance and bind to port 3030.

uSecond = udpport("LocalPort", 3030)
uSecond = 
  UDPPort with properties:

     IPAddressVersion: "IPV4"
            LocalHost: "0.0.0.0"
            LocalPort: 3030
    NumBytesAvailable: 0

  Show all properties, functions

Prepare Callback Function

Configure the callback function readAcknowledgement to read data received using the configureCallback function. This callback function is triggered when a terminator is received. The terminator value used for this example is "CR/LF".

Set the Terminator property to "CR/LF" using the configureTerminator function.

configureTerminator(uSecond,"CR/LF");

The Terminator property should now be set to "CR/LF".

uSecond.Terminator
ans = 
"CR/LF"

Set up the callback function readAcknowledgement to trigger when the assigned terminator is received.

configureCallback(uSecond,"terminator",@readAcknowledgement);

The readAcknowledgement.m callback function is given below.

function readAcknowledgement(u, ~)
% Read the acknowledgement data. readline removes the Terminator from the
% data read.
data = readline(u);

% Display the acknowledgement string read.
disp(data);
end

The BytesAvailableFcn property should be now set to the callback function, and the BytesAvailableFcnMode set to "terminator".

uSecond.BytesAvailableFcn
ans = function_handle with value:
    @readAcknowledgement

uSecond.BytesAvailableFcnMode
ans = 
"terminator"

Send Command to First MATLAB Session

Send a "START" command to the udpport instance uFirst on the first MATLAB session using writeline. uFirst is bound to port 2020. writeline automatically appends the "START" command with the Terminator "CR/LF".

disp("Sending Command - START");
Sending Command - START
writeline(uSecond, "START", "127.0.0.1", 2020);

Send another command to the same destination address and destination port. The "STOP" command is automatically appended with the Terminator "CR/LF".

disp("Sending Command - STOP");
Sending Command - STOP
writeline(uSecond, "STOP");

Clear udpport

Pause before clearing the object for the responses to come back from the first MATLAB session.

pause(0.3);

Configure the callbacks to be off.

configureCallback(uSecond, "off");

The BytesAvailableFcn property should be now set to an empty function_handle, and the BytesAvailableFcnMode set to "off".

uSecond.BytesAvailableFcn
ans =

  0×0 empty function_handle array
uSecond.BytesAvailableFcnMode
ans = 
"off"

Clear udpport instance.

clear uSecond