Get the UDP sender adress and port in Simulink

10 次查看(过去 30 天)
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

回答(1 个)

Animesh
Animesh 2023-3-7
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 个评论
Tim Remmert
Tim Remmert 2023-3-8
The "UDP Receive" block just returns the message it's self as and uint8 vector. Are there other "UDP Receive" blocks I dont have access to? I'm using "DSP System Toolbox / UDP Receive"

请先登录,再进行评论。

标签

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by