Get the UDP sender adress and port in Simulink
Show older comments
I get a UDP message from an unkown IP-Adresse and unkown port to a known local port. I want to read that message, create an answer and reply to the same IP-Adresse and port I got the message from. In MATLAB, using the udpport, I get the message and the sender address / port but using udp recieve in Simulink just provides the message / data. Is there any other method I can use?
Thanks for your help
Answers (1)
Animesh
on 7 Mar 2023
Hello,
Here's how you can modify your Simulink model to achieve this:
- Drag and drop the "UDP Receive" block from the Simulink Library Browser into your model.
- Double-click on the "UDP Receive" block to open its parameters dialog box.
- Set the "Local IP port" parameter to the port number that your Simulink model will use to receive UDP packets.
- Click the "Apply" button to apply the changes.
- Connect the output of the "UDP Receive" block to a MATLAB Function block.
In the MATLAB Function block, write code to extract the received data and the remote sender's IP address and port number from the input signal. Here's an example code snippet that you can use:
function output = udp_receiver(input)
% Extract received data and remote sender's IP address and port number
data = input(1).Data;
ip_address = input(1).RemoteAddress;
port = input(1).RemotePort;
% Your code to process the received data and generate a reply message
% Create a UDP packet to send the reply message to the remote sender
packet = struct('Data', reply_data, 'RemoteAddress', ip_address, 'RemotePort', port);
% Output the packet to the next block
output = packet;
end
- Connect the output of the MATLAB Function block to the input of a "UDP Send" block.
1 Comment
Tim Remmert
on 8 Mar 2023
Categories
Find more on Modeling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!