Documentation

dsp.UDPSender System object

将UDP数据包发送到网络

描述

TheUDPSender对象将UDP数据包发送到网络。

要将UDP数据包发送到网络:

  1. Define and set up your UDP sender. SeeConstruction

  2. Callstep根据属性发送数据包dsp.UDPSender。The behavior ofstepis specific to each object in the toolbox.

不te

Starting in R2016b, instead of using thestep执行由System Object™定义的操作的方法,您可以用参数调用对象,就好像它是函数一样。例如,y = step(obj,x)y = obj(x)perform equivalent operations.

Construction

udps = dsp.udpsender返回UDP发送者对象,udps, that sends UDP packets to a specified port.

udps = dsp.udpsender('财产Name',财产Value,……)返回UDP发送者对象,udps, with each property set to the specified value.

Properties

RemoteIPAddress

远程地址发送数据

指定发送数据的遥控器(即主机)IP地址。默认值为'127.0.0.1',这是本地主机。

RemoteIPPort

远程端口发送数据

Specify the port at the remote IP address to which the data is sent. This property is tunable in generated code but not tunable during simulation. The default is25000

LocalipportSource

来源Localipport财产

指定如何确定主机上的端口为|汽车|财产|. If you specify汽车, the object selects the port dynamically from the available ports. If you specify财产, the object uses the source specified in theLocalipport财产。默认值为汽车

Localipport

Local port from which to send data

Specify the port from which to send data. This property applies when you set theLocalipportSource财产to汽车。默认值为25000

方法

step 将UDP数据包发送到网络
Common to All System Objects
release

允许系统对象属性值更改

例子

expand all

使用该数据包发送和接收UDP数据包dsp.UDPSenderdsp.UDPReceiverSystem objects. Calculate the number of bytes successfully transmitted.

不te: If you are using R2016a or an earlier release, replace each call to the object with the equivalent step syntax. For example,obj(x)变成步骤(obj,x)

Set theRemoteIPPortUDP发件人和LocalipportUDP接收器的31000。将数据向量的长度设置为128samples, which is less than the value of theMaximumMessageLength接收器的属性。为了防止丢失数据包,请致电setupmethod 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;

在每个迭代的循环中,发送并接收一包数据。在循环结束时,请使用fprintffunction to print the number of bytes sent by the sender and the number of bytes received by the receiver.

fork = 1:20 datasent = uint8(255*rand(1,datalength));bytessent = bytessent + datalength;udps(datasent);datareceived = udpr();BytesReceived = BytesReceived + Length(DataReceived);end释放(UDPS);释放(UDPR);fprintf('Bytes sent: %d\n',副词);
字节发送:2560
fprintf('收到的字节:%d \ n', bytesReceived);
Bytes received: 2560

The local IP port number of thedsp.UDPReceiverobject and the remote IP port number of thedsp.UDPSenderobject are tunable in the generated code. Generate a MEX file from thereceiver包含算法以通过UDP网络接收正弦波数据的函数。更改UDP接收器的远程IP端口号,而无需再生MEX文件。验证通过网络发送和接收的字节数。

不te:此示例仅在R2017A或更高版本中运行。

输入到receiver功能是本地IP端口号dsp.UDPReceiver系统对象™。该函数的输出是从UDP网络接收到的字节数。

typereceiver
function [bytesReceived] = receiver(portnumber) persistent udpRx if isempty(udpRx) udpRx = dsp.UDPReceiver('MessageDataType','double'); end udpRx.LocalIPPort = portnumber; dataReceived = udpRx(); bytesReceived = length(dataReceived);

Thedsp.UDPSender对象remoteIPPortnumber set to 65000 sends the data over the UDP network. Thedsp.UDPReceiver对象Localipport设置为65000的数字从UDP网络接收数据。数据是一个正弦波,每帧包含250个样品。

portnumber = 65000;udpsend = dsp.udpsender('RemoteIPPort',portnumber);正弦= DSP.SineWave('SamplesPerFrame',250); bytesSent = 0; bytesReceived = 0; dataLength = 250;fori = 1:10 datasent = sine();bytessent = bytessent + datalength;udpsend(datasent);bytesReceived = bytesReceived + receiver(portnumber);endfprintf('Number of bytes sent: %d',副词);
发送的字节数: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.

receiver.mfunction.

codegenreceiver-args{65000}

                   

Release the sender and change theRemoteIPPort数字到25000。Localipport接收器的数量继续为65000。由于端口号不同,因此数据未成功传输。

release(udpSend) portnumberTwo = 25000; udpSend.RemoteIPPort = portnumberTwo; bytesReceived = 0; bytesSent = 0;fori = 1:10 datasent = sine();bytessent = bytessent + datalength;udpsend(datasent);bytesReceived = bytesReceived + receiver_mex(portnumber);endfprintf('Number of bytes sent: %d',副词);
发送的字节数:2500
fprintf('Number of bytes received: %d', bytesReceived);
Number of bytes received: 0

清除MEX文件,然后将接收器的本地IP端口编号更改为25000。清除MEX使接收器端口号可以更改,而无需再生MEX。发件人和接收器的端口号匹配。验证数据是否成功传输。

清除梅克斯%#okbytesReceived = 0; bytesSent = 0;fori = 1:10 datasent = sine();bytessent = bytessent + datalength;udpsend(datasent);bytesreceived = bytesreceived + receiver_mex(portnumbertwo);endfprintf('Number of bytes sent: %d',副词);
发送的字节数:2500
fprintf('Number of bytes received: %d', bytesReceived);
Number of bytes received: 2250

数据通过UDP网络成功传输。由于开销,初始数据被删除。

Extended Capabilities

See Also

系统对象

在R2012a中引入

这个话题有帮助吗?