This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

dsp.UDPReceiver System object

Receive UDP packets from the network

Description

The dsp.UDPReceiver System object™ receives UDP packets over a UDP network from a remote IP address specified in the RemoteIPAddress property. The object then saves the data to its internal buffer. The amount of data (number of elements) received in each UDP packet can vary. The maximum number of bytes the object can receive without losing data is set by the ReceiveBufferSize property. The MaximumMessageLength property specifies the maximum number of samples each data packet can contain. The LocalIPPort on which the object receives the data is tunable in generated code but not tunable during simulation. For an example, see Tune the UDP Port Number in MATLAB.

To receive UDP packets from the network:

  1. Create the dsp.UDPReceiver object and set its properties.

  2. Call the object with arguments, as if it were a function.

To learn more about how System objects work, see What Are System Objects? (MATLAB).

Creation

Syntax

udpr = dsp.UDPReceiver
udpr = dsp.UDPReceiver(Name,Value)

Description

example

udpr = dsp.UDPReceiver returns a UDP receiver object that receives UDP packets from a specified port.

example

udpr = dsp.UDPReceiver(Name,Value) returns a UDP receiver object with each specified property set to the specified value. Enclose each property name in single quotes.

Properties

expand all

Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and the release function unlocks them.

If a property is tunable, you can change its value at any time.

For more information on changing property values, see System Design in MATLAB Using System Objects (MATLAB).

Port on which to receive the data, specified as a scalar in the range [1 65535]. This property is tunable in generated code but not tunable during simulation.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Address from which to accept data, specified as a character vector or a string scalar containing a valid IP address. Entering a specific IP address blocks UDP packets from other addresses. The default, '0.0.0.0', indicates that the data can be accepted from any remote IP address.

Data Types: char

Size of the internal buffer that receives UDP packets, specified in bytes as a scalar in the range [1 67108864]. If the number of bytes received exceeds this value, the buffer overflows and the contents are truncated.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Maximum length of the output message, specified in samples as a positive scalar in the range [1 65507]. Set this property to a value equal to or greater than the data size of the UDP packet. If you receive more samples than specified in this property, the excess data is truncated.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Data type of the vector elements in the message output, specified as a MATLAB® built-in data type.

Match the data type with the data input used to create the UDP packets.

Data Types: char

Usage

Syntax

dataR = udpr()

Description

example

dataR = udpr() receives one UDP packet from the network.

Output Arguments

expand all

Data received from the network, returned as one packet. The MaximumMessageLength property specifies the maximum number of bytes each data packet can contain. Length of the data received is the number of bytes received from the network.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | logical

Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named obj, use this syntax:

release(obj)

expand all

stepRun System object algorithm
releaseRelease resources and allow changes to System object property values and input characteristics
resetReset internal states of System object

Examples

expand all

Send and receive UDP packets using the dsp.UDPSender and dsp.UDPReceiver System objects. Calculate the number of bytes successfully transmitted.

Note: If you are using R2016a or an earlier release, replace each call to the object with the equivalent step syntax. For example, obj(x) becomes step(obj,x).

Set the RemoteIPPort of UDP sender and the LocalIPPort of the UDP receiver to 31000. Set the length of the data vector to 128 samples, which is less than the value of the MaximumMessageLength property of the receiver. To prevent the loss of packets, call the setup method on the receiver object before the first call to the object algorithm.

udpr = dsp.UDPReceiver('LocalIPPort',31000);
udps = dsp.UDPSender('RemoteIPPort',31000);

setup(udpr); 

bytesSent = 0;
bytesReceived = 0;
dataLength = 128;

In each loop of iteration, send and receive a packet of data. At the end of the loop, use the fprintf function to print the number of bytes sent by the sender and the number of bytes received by the receiver.

for k = 1:20
   dataSent = uint8(255*rand(1,dataLength));
   bytesSent = bytesSent + dataLength;
   udps(dataSent);
   dataReceived = udpr();
   bytesReceived = bytesReceived + length(dataReceived);
end

release(udps);
release(udpr);

fprintf('Bytes sent:     %d\n', bytesSent);
Bytes sent:     2560
fprintf('Bytes received: %d\n', bytesReceived);
Bytes received: 2560

The local IP port number of the dsp.UDPReceiver object and the remote IP port number of the dsp.UDPSender object are tunable in the generated code. Generate a MEX file from the receiver function which contains the algorithm to receive sine wave data over a UDP network. Change the remote IP port number of the UDP receiver without regenerating the MEX file. Verify the number of bytes sent and received over the network.

Note: This example runs only in R2017a or later.

The input to the receiver function is the local IP port number of the dsp.UDPReceiver System object™. The output of this function is the number of bytes received from the UDP network.

type receiver
function [bytesReceived] = receiver(portnumber)

persistent udpRx

if isempty(udpRx) 
    udpRx = dsp.UDPReceiver('MessageDataType','double'); 
end 

udpRx.LocalIPPort = portnumber; 
dataReceived = udpRx();
bytesReceived = length(dataReceived);

The dsp.UDPSender object with remoteIPPort number set to 65000 sends the data over the UDP network. The dsp.UDPReceiver object with LocalIPPort number set to 65000 receives the data from the UDP network. The data is a sine wave containing 250 samples per frame.

portnumber = 65000;
udpSend = dsp.UDPSender('RemoteIPPort',portnumber);
sine = dsp.SineWave('SamplesPerFrame',250);

bytesSent = 0;
bytesReceived = 0;
dataLength = 250;

for i = 1:10
dataSent = sine();
bytesSent = bytesSent + dataLength;
udpSend(dataSent);
bytesReceived = bytesReceived + receiver(portnumber);
end
fprintf('Number of bytes sent: %d', bytesSent);
Number of bytes sent: 2500
fprintf('Number of bytes received: %d', bytesReceived);
Number of bytes received: 2250

The data is sent and received successfully over the UDP network. The initial data is dropped due to overhead.

Generate a MEX file from the receiver.m function.

codegen receiver -args {65000}

Release the sender and change the RemoteIPPort number to 25000. The LocalIPPort number of the receiver continues to be 65000. Since the port numbers are different, the data is not transmitted successfully.

release(udpSend)
portnumberTwo = 25000;
udpSend.RemoteIPPort = portnumberTwo; 
bytesReceived = 0;
bytesSent = 0;
for i = 1:10
dataSent = sine();
bytesSent = bytesSent + dataLength;
udpSend(dataSent);
bytesReceived = bytesReceived + receiver_mex(portnumber);
end
fprintf('Number of bytes sent: %d', bytesSent);
Number of bytes sent: 2500
fprintf('Number of bytes received: %d', bytesReceived);
Number of bytes received: 0

Clear the MEX file and change the local IP port number of the receiver to 25000. Clearing the MEX enables the receiver port number to change without having to regenerate the MEX. The port numbers of the sender and receiver match. Verify if the data is transmitted successfully.

clear mex %#ok
bytesReceived = 0;
bytesSent = 0;
for i = 1:10
dataSent = sine();
bytesSent = bytesSent + dataLength;
udpSend(dataSent);
bytesReceived = bytesReceived + receiver_mex(portnumberTwo);
end
fprintf('Number of bytes sent: %d', bytesSent);
Number of bytes sent: 2500
fprintf('Number of bytes received: %d', bytesReceived);
Number of bytes received: 2250

The data is transmitted successfully over the UDP network. The initial data is dropped due to overhead.

Extended Capabilities

Introduced in R2012a