Read Data from MATLAB Using UDP Receive Block
This example shows how to use the UDP Receive block to read data packets over the User Datagram Protocol (UDP). In this example, send a sine wave from MATLAB® over UDP and read it using the UDP Receive block.
Configure UDP Receive Block
This model opens a UDP interface to the local address and port specified in the UDP Receive block. The UDP Receive block also listens for data at a specified remote address and port. When it receives data, it displays the data in a scope.
Use the following command to open the model. Then, run the model before moving to the next section.
Send UDP Packets from MATLAB
udpport object to connect to a UDP socket at the specified address and port. The local host and port of the
udpport object must match the values of the remote address and port where the UDP Receive block is listening for data. You can create this object in the same MATLAB session as the model or in a different session on the same computer.
udpObj = udpport(LocalHost="127.0.0.1",LocalPort=9090);
Create a sine wave and send it over UDP using the
udpport object. The destination address and port must match the values of the local address and port in the UDP Receive block of the model. When you are finished, you can close the connection to the UDP socket.
freq = 10; % frequency (Hz) time = (0:1/(freq*1000):1); amp = 1; % amplitude (V) phi = 0; % phase data = amp*sin(2*pi*freq*time+phi); write(udpObj,data,"double","127.0.0.1",9091); clear udpObj;
The sine wave sent over UDP using the
udpport object is received by the UDP Receive block of the model. The sine wave is displayed on the scope.