Main Content

Communicate with UDP on Different Computers

This example shows how to send data via UDP between two MATLAB® sessions on different computer using the udpport function.

First MATLAB Session on Receiving Computer

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
                  Tag: ""
    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 acknowledgment 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");

Verify the Terminator property value.

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 callback function is shown here.

Note

In the following code, replace "172.19.119.24" with the IP address of the target (receiving) computer.

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 acknowledgment string.
data = "COMMAND RECEIVED - """ + data +  """. SENDING ACKNOWLEDGEMENT.";

% Send the acknowledgment string, followed by the Terminator "CR/LF", to the
% udpport instance bound to port 3030 in the first MATLAB instance.
% Replace "172.19.119.24" with the IP address of the target computer(receiver's computer).
writeline(u, data, "172.19.119.24", 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 on Transmitting Computer

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
                  Tag: ""
    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");

Verify the Terminator property value.

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 callback function is shown here.

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

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

Verify the values of the BytesAvailableFcn and BytesAvailableFcnMode properties.

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".

Note

Replace "172.19.119.24" with the IP address of the target (receiving) computer.

% Replace "172.19.119.24" with the IP address of the target (receiving) computer.
writeline(uSecond, "START", "172.19.119.24", 2020);

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

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");

Verify the values of the BytesAvailableFcn and BytesAvailableFcnMode properties.

uSecond.BytesAvailableFcn
ans =

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

Clear the udpport instance.

clear uSecond

Troubleshooting Tips

If you are not receiving data at the receiving end, follow these steps to diagnose and resolve the issue.

  1. Verify Network Connection. Ensure both the sender and receiver computers are connected to the same local network (for example, the same Wi-Fi® or Ethernet network). Cross-network or VPN configurations might prevent communication.

  2. Allow MATLAB through the Windows® firewall. If UDP packets are arriving in Wireshark but not in MATLAB, the firewall might be blocking MATLAB from receiving data. To allow MATLAB through the firewall:

    1. Open the Start Menu. Search for "Windows Defender Firewall" and open it.

    2. On the left panel, click "Allow an app or feature through Windows Defender Firewall."

    3. Click "Change settings" (admin privileges might be required).

    4. At the bottom, click "Allow another app".

    5. Click "Browse" and navigate to: C:\Program Files\MATLAB\R20xx\bin\matlab.exe. (Replace R20xx with your version, for example, R2024b.)

    6. Click Add, and ensure both Private and Public boxes are checked.

    7. Click OK to save the changes.

  3. Allow Specific UDP ports through the firewall. To explicitly allow only the necessary UDP ports (for example, 2020, 3030), follow these steps:

    1. Go to Windows Defender Firewall. Click "Advanced Settings" on the left panel.

    2. In the left sidebar, click "Inbound Rules".

    3. On the right, click "New Rule…".

    4. Choose "Port", then click Next.

    5. Select "UDP" and enter the specific port numbers:2020,3030

    6. Click Next, then select "Allow the connection".

    7. Apply the rule to Domain, Private, and Public profiles.

    8. Name the rule (for example, "MATLAB UDP Ports") and click Finish.

    9. In the left sidebar, click "Outbound Rules" and repeat the same steps.