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, functionsPrepare 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:
@sendAcknowledgementuFirst.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, functionsPrepare 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:
@readAcknowledgementuSecond.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 uSecondTroubleshooting Tips
If you are not receiving data at the receiving end, follow these steps to diagnose and resolve the issue.
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.
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:
Open the Start Menu. Search for "Windows Defender Firewall" and open it.
On the left panel, click "Allow an app or feature through Windows Defender Firewall."
Click "Change settings" (admin privileges might be required).
At the bottom, click "Allow another app".
Click "Browse" and navigate to:
C:\Program Files\MATLAB\R20xx\bin\matlab.exe. (Replace R20xx with your version, for example, R2024b.)Click Add, and ensure both Private and Public boxes are checked.
Click OK to save the changes.
Allow Specific UDP ports through the firewall. To explicitly allow only the necessary UDP ports (for example, 2020, 3030), follow these steps:
Go to Windows Defender Firewall. Click "Advanced Settings" on the left panel.
In the left sidebar, click "Inbound Rules".
On the right, click "New Rule…".
Choose "Port", then click Next.
Select "UDP" and enter the specific port numbers:2020,3030
Click Next, then select "Allow the connection".
Apply the rule to Domain, Private, and Public profiles.
Name the rule (for example, "MATLAB UDP Ports") and click Finish.
In the left sidebar, click "Outbound Rules" and repeat the same steps.