Transition Your Code to udpport
Interface
The udp
function, its object functions, and its properties will be
removed. Use udpport
instead.
udp Interface | udpport Interface | Example |
---|---|---|
udp , DatagramTerminateMode , and
fopen | udpport | Connect to UDP Socket |
instrfind and instrfindall | udpportfind | Find Existing UDP Socket Connections |
fwrite | write | Read and Write |
fread | read | |
fprintf | writeline | Read Terminated String |
Write and Read Back Data | ||
fscanf | readline | Read Terminated String |
fgetl | Read and Parse String Data | |
fgets | Write and Read Back Data | |
flushinput and flushoutput | flush | Flush Data from Memory |
Terminator | configureTerminator | Set Terminator |
BytesAvailableFcnCount ,
BytesAvailableFcnMode , and
BytesAvailableFcn | configureCallback | Set Up Callback Function |
DatagramReceivedFcn | ||
BytesAvailable | NumBytesAvailable | |
OutputDatagramPacketSize | OutputDatagramSize | |
ErrorFcn | ErrorOccurredFcn | |
fclose | clear and delete | Disconnect UDP Socket |
Removed Functionality
The binblockread
and binblockwrite
functions
will be removed.
The DatagramAddress
and DatagramPort
properties are the SenderAddress
and SenderPort
properties of the Datagram
structure returned by the
read
function in the updated interface.
The RemoteHost
and RemotePort
properties are
input arguments for the write
and writeline
functions in the updated interface. The LocalPortMode
property is part
of the LocalPort
property in the updated
interface.
The ValuesReceived
and
ValuesSent
properties will be removed. You can calculate the number
of values sent using the NumBytesAvailable
property and the data type
of the data available. For example, if the NumBytesAvailable
is 20
bytes of uint32
data, the number of values sent is five since each
uint32
value is four bytes.
The readasync
and
stopasync
functions and the ReadAsyncMode
and
TransferStatus
properties will be removed. The updated interface
reads data asynchronously.
The BytesToOutput
,
InputBufferSize
, OutputBufferSize
, and
InputDatagramPacketSize
properties will be removed. Buffer sizes and
received datagram sizes are automatically managed and sized as needed.
The OutputEmptyFcn
property will be
removed. You can set callback functions using configureCallback
in the
updated interface, but not for this property.
The RecordDetail
, RecordMode
,
RecordName
, and RecordStatus
properties will be
removed.
The TimerFcn
and TimerPeriod
properties will
be removed. Use timer
instead.
The Name
, Type
,
ObjectVisibility
, Status
, and
Tag
properties will be removed.
Connect to UDP Socket
These examples show how to connect to a UDP socket using the recommended functionality.
Functionality | Use This Instead |
---|---|
u = udp;
u.DatagramTerminateMode = "off";
fopen(u) |
uByte = udpport("byte"); |
u = udp;
u.DatagramTerminateMode = "on";
fopen(u) |
uDatagram = udpport("datagram"); |
The fopen
function is not available in the updated interface. The
object creation function udpport
both creates and connects the
object.
For more information, see udpport
.
Find Existing UDP Socket Connections
instrfind
and instrfindall
will be removed.
Use udpportfind
instead. (since R2024a)
Read and Write
These examples use an echo server to show how to perform a binary write and read, and write and read a nonterminated string using the recommended functionality.
Functionality | Use 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 |
Read Terminated String
This example shows how to perform a terminated string write and read using the recommended functionality.
Functionality | Use 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'
| |
echoudp("on",9090) % u is a udp object u.Terminator = "CR"; fprintf(u,"hello") data = fgets(u) data = 'hello '
|
Read and Parse String Data
This example shows how to read and parse string data using the recommended functionality.
Functionality | Use 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.
Functionality | Use 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" |
Flush Data from Memory
This example shows how to flush data from the buffer using the recommended functionality.
Functionality | Use 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.
Functionality | Use 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.
Functionality | Use 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,"byte",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 |
% u is a udp object u.DatagramTerminateMode = "on"; u.DatagramReceivedFcn = @mycallback; function mycallback(src,evt) data = fread(src,src.BytesAvailableFcnCount); disp(evt) disp(evt.Data) end Type: 'DatagramReceived' Data: [1×1 struct] AbsTime: [2019 12 21 16 35 9.7032] DatagramAddress: '127.0.0.1' DatagramPort: 9090 DatagramLength: 1 |
% 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
.
Disconnect UDP Socket
The fclose
function is not available in the updated interface. To
disconnect a UDP socket, use clear
or delete
instead, depending upon whether you are working in a single workspace
or multiple workspaces. For details, see the following examples on the
udpport
reference page: