Clear Filters
Clear Filters

How to send udp packets to non constant ip address

4 views (last 30 days)
Hallo,
im trying to simulate in Simulink an IP/TCP network. For this I have to send UDP packets to different ip addresses.
The problem is, that I don't know the ip address before the start of the simulation. In other words I can't use const values for ip values.
For example, I have a Simulink "MATLAB Function Block".
Inside this block I create an UDP sender, e.g.:
dsp.UDPSender('RemoteIPAddress', ip , 'RemoteIPPort', 3002);
The variable "ip" cannot be a non-const value, which makes it somewhat useless for me:
Cannot compute constant value for argument #4 for this System object constructor. All arguments to the constructor of this System object must be constants for code generation. One way to supply a constant value is to pass it to the main function using Simulink non-tunable parameters (for MATLAB Function block) or coder.Constant(...) (for MATLAB Coder).
I have also tried several libraries:
  • DSP System Toolbox
  • Simulink Support Package for Raspberry Pi Hardware
  • Instrument Control Toolbox
  • Simulink Real-Time
But even if it is a pure Simulink block, I could not change the IP value to something I specify in a "MATLAB Function Block".
Does anyone know how to reach a UDP sender where the destination IP address is calculated at runtime?

Answers (1)

Arun
Arun on 13 Oct 2023
Hey Kaspar,
I understand that you are facing difficulties in sending UDP packets to the sender from which you received the packet. The IP address of the sender is not available beforehand, and the implementation does not allow variable value for 'RemoteIPAddress'. Also, you are not able to set the variable value of base workspace from MATLAB function workspace.
To answer the second part of the query, it is important to note that the MATLAB Function Block cannot be utilized to modify values within the MATLAB base workspace.
However, since you possess the IP information of the reciever, in order to reach the UDP server, you can broadcast the packet to network using the subnet mask and check if that help you with your implementation. You can use the below logic to get the broadcast address:
remoteIp=bitor(bitand(userIp,userNetworkMask),bitcmp(userNetworkMask)); % get the broadcast address
dsp.UDPSender('RemoteIpAddress', remoteIp, 'remoteIpPort', 3002);
I am further providing reference for UDP broadcast and multicast that might be useful for you.
I Hope this information helps you to send the packet to UDP server.

Community Treasure Hunt

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

Start Hunting!