Main Content

Transition Your Code to udpport Interface

The udp function, its object functions, and its properties will be removed. Use udpport instead.

udp Interfaceudpport InterfaceExample
udpudpportConnect to UDP Socket
fwrite and freadwrite and readRead and Write
fprintfwritelineRead Terminated String
Write and Read Back Data
fscanf, fgetl, and fgetsreadlineRead Terminated String
Read and Parse String Data
Write and Read Back Data
flushinput and flushoutputflushFlush Data from Memory
TerminatorconfigureTerminatorSet Terminator
BytesAvailableFcnCount, BytesAvailableFcnMode, BytesAvailableFcn, and BytesAvailableconfigureCallbackSet Up Callback Function
udp Propertiesudpport Properties 

Connect to UDP Socket

These examples show how to connect to a UDP socket and disconnect from it using the recommended functionality.

FunctionalityUse This Instead
u = udp;
fopen(u)
uByte = udpport("byte");
uDatagram = udpport("datagram");
fclose(u)
delete(u)
clear u
clear uByte
clear uDatagram

For more information, see udpport.

Read and Write

These examples use a echo server to show how to perform a binary write and read, and write and read a nonterminated string using the recommended functionality.

FunctionalityUse This Instead
echoudp("on",9090)

% u is a udp object
u.DatagramTerminateMode = "off";
fwrite(u,1:5);
data = fread(u,5)
data =

     1
     2
     3
     4
     5
echoudp("on",9090)

% uByte is a udpport byte object
write(uByte,1:5,"127.0.0.1",9090)
data = read(uByte,5)
data =

     1     2     3     4     5
echoudp("on",9090)

% u is a udp object
u.DatagramTerminateMode = "on";
fwrite(u,1:5);
data = fread(u,1)
data =

     1
     2
     3
     4
     5
echoudp("on",9090)

% uDatagram is a udpport datagram object
write(uDatagram,1:5)
data = read(uDatagram,1)
data = 

  Datagram with properties:

             Data: [1 2 3 4 5]
    SenderAddress: "127.0.0.1"
       SenderPort: 9090
echoudp("on",9090)

% u is a udp object
fwrite(u,"hello","char")
length = 1;
data = fread(u,length,"char")
data =

   104
   101
   108
   108
   111
data = char(data)'
data =

    'hello'
echoudp("on",9090)

% uByte is a udpport byte object
write(uByte,"hello","string","localhost",9090);
length = 5;
data = read(uByte,length,"string")
data =

    "hello"
echoudp("on",9090)

% uDatagram is a udpport datagram object
write(uDatagram,"hello","string","localhost",9090);
length = 1;
data = read(uDatagram,length,"string")
data = 

  Datagram with properties:

             Data: "hello"
    SenderAddress: "127.0.0.1"
       SenderPort: 9090

For more information, see write or read.

Read Terminated String

This example shows how to perform a terminated string write and read using the recommended functionality.

FunctionalityUse This Instead
echoudp("on",9090)

% u is a udp object
u.Terminator = "CR";
fprintf(u,"hello")
data = fscanf(u)
data =

    'hello
     '
echoudp("on",9090)

% uByte is a udpport byte object
configureTerminator(uByte,"CR");
writeline(uByte,"hello","127.0.0.1",9090);
data = readline(uByte)
a = 

    "hello"
echoudp("on",9090)

% u is a udp object
u.Terminator = "CR";
fprintf(u,"hello")
data = fgetl(u)
data =

    'hello'

fgetl reads until the specified terminator is reached and then discards the terminator.

echoudp("on",9090)

% u is a udp object
u.Terminator = "CR";
fprintf(u,"hello")
data = fgets(u)
data =

    'hello
     '

fgets reads until the specified terminator is reached and then returns the terminator.

For more information, see writeline or readline.

Read and Parse String Data

This example shows how to read and parse string data using the recommended functionality.

FunctionalityUse This Instead
% u is a udp object
data = scanstr(u,';')
data =

  3×1 cell array

    {'a'}
    {'b'}
    {'c'}
% uByte is a udpport byte object
data = readline(uByte)
data = 

    "a;b;c"
data = strsplit(data,";")
data = 

  1×3 string array

    "a"    "b"    "c"

For more information, see readline.

Write and Read Back Data

This example shows how to write ASCII terminated data and read ASCII terminated data back using the recommended functionality.

FunctionalityUse This Instead
% u is a udp object
data = query(u,'ctrlcmd')
data =

    'success'
% uByte is a udpport byte object
writeline(uByte,"ctrlcmd")
data = readline(uByte)
data = 

    "success"

For more information, see writeline or readline.

Flush Data from Memory

This example shows how to flush data from the buffer using the recommended functionality.

FunctionalityUse This Instead
% u is a udp object
flushinput(u)
% u is a udpport byte or datagram object
flush(u,"input")
% u is a udp object
flushoutput(u)
% u is a udpport byte or datagram object
flush(u,"output")
% u is a udp object
flushinput(u)
flushoutput(u)
% u is a udpport byte or datagram object
flush(u)

For more information, see flush.

Set Terminator

These examples show how to set the terminator using the recommended functionality.

FunctionalityUse This Instead
% u is a udp object
u.Terminator = "CR/LF";
% u is a udpport byte or datagram object
configureTerminator(u,"CR/LF")
% u is a udp object
u.Terminator = {"CR/LF" [10]};
% u is a udpport byte or datagram object
configureTerminator(u,"CR/LF",10)

For more information, see configureTerminator.

Set Up Callback Function

These examples show how to set up a callback function using the recommended functionality.

FunctionalityUse This Instead
% u is a udp object
u.BytesAvailableFcnCount = 5
u.BytesAvailableFcnMode = "byte"
u.BytesAvailableFcn = @mycallback

function mycallback(src,evt)
   data = fread(src,src.BytesAvailableFcnCount);
   disp(evt)
   disp(evt.Data)
end
    Type: 'BytesAvailable'
    Data: [1×1 struct]

    AbsTime: [2019 12 21 16 35 9.7032]
% uByte is a udpport byte object
configureCallback(uByte,5,@mycallback);

function mycallback(src,evt)
   data = read(src,src.BytesAvailableFcnCount);
   disp(evt)
end
  ByteAvailableInfo with properties:

    BytesAvailableFcnCount: 5
                   AbsTime: 21-Dec-2019 12:23:01
% uDatagram is a udpport datagram object
configureCallback(uDatagram,"datagram",1,@mycallback);

function mycallback(src,evt)
   data = read(src,src.DatagramsAvailableFcnCount);
   disp(evt)
end
  DatagramAvailableInfo with properties:

    DatagramsAvailableFcnCount: 1
                       AbsTime: 21-Dec-2019 12:23:01
% u is a udp object
u.Terminator = "CR"
u.BytesAvailableFcnMode = "terminator"
u.BytesAvailableFcn = @mycallback

function mycallback(src,evt)
   data = fscanf(src);
   disp(evt)
   disp(evt.Data)
end
    Type: 'BytesAvailable'
    Data: [1×1 struct]

    AbsTime: [2019 12 21 16 35 9.7032]
% uByte is a udpport byte object
configureCallback(uByte,"terminator",@mycallback);

function mycallback(src,evt)
   data = readline(src);
   disp(evt)
end
  TerminatorAvailableInfo with properties:

                   AbsTime: 21-Dec-2019 12:23:01

For more information, see configureCallback.

See Also

Related Topics