MATLAB Answers

0

How can I receive UDP broadcast data in MATLAB using Instrument Control Toolbox?

My device sends UDP broadcast data to all computers on my local area network.
How can I receive this data in MATLAB?

Tags

No tags entered yet.

2 Answers

Answer by MathWorks Support Team on 18 Jan 2017
 Accepted Answer

You can receive UDP broadcast data using the UDP functionality in Instrument Control Toolbox.
 
If you are only receiving UDP packets, and not sending out any UDP packets, the only relevant properties of the UDP object are LocalHost and LocalPort; the RemoteHost and RemotePort are not relevant in this case.
 
LocalHost is the host name or IP address of the Ethernet adapter you are receiving the UDP packets on, and the LocalPort is the port number you are receiving the UDP packets on.
Note: If you don't specify LocalHost (or if you set it to an empty string), you will receive UDP packets on all available Ethernet interfaces at the specified LocalPort.
 
% Listen on all available Ethernet interfaces at local port 8000.
% Specify a LocalHost (host name or IP address) if known
u = udp('', 'LocalHost', '', 'LocalPort', 8000);
fopen(u);
% Receive a single UDP packet
packetData = fread(u);
% Clean up
fclose(u);
delete(u);
clear u
 

  1 Comment

Could you please give an explanation for each input argument to the udp function? I'm not sure what the first blank entry is for. I'm guessing we don't need to specify an IP for the LocalHost since it defaults to 127.0.0.1?

Sign in to comment.


Answer by Vinod
on 12 Jan 2017

Here's a file exchange example that shows you how you can broadcast and receive messages over UDP.

  0 Comments

Sign in to comment.