%% Second udpport instance
% This creates a second udpport instance in a new MATLAB session. Open and run this file on a new MATLAB instance.
%% Create udpport instance for second MATLAB session
% Create a udpport instance, binding to port 2020.

u = udpport("LocalPort",2020);

%
%% Prepare callback function to read data from the first MATLAB instance.
% 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 first MATLAB instance when it receives data.
% Set the Terminator property to "CR/LF", to match that of the the other udpport instance.

configureTerminator(u, "CR/LF")

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

u.Terminator

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

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

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

u.BytesAvailableFcn
u.BytesAvailableFcnMode

% Copyright 2020 The MathWorks, Inc.