In this example, write and read ASCII data with a UDP object.
Use an echo server to experiment with the basic functionality of the UDP objects without connecting to an actual device. An echo server is a service that returns to the sender's address and port, the same bytes it receives from the sender.
Create a byte-type
udpport object. Datagram-type
udpport objects do not support communication with ASCII-terminated
u = udpport
u = UDPPort with properties: IPAddressVersion: "IPV4" LocalHost: "0.0.0.0" LocalPort: 53816 NumBytesAvailable: 0 Show all properties, functions
writeline function to write ASCII data to the server. Write a
string to the echo server.
The function suspends MATLAB® execution until all the data is written or a timeout occurs as specified by
Timeout property of the
Check the default ASCII terminator.
ans = "LF"
writeline function automatically appends the linefeed (LF)
"Request Time" before it is written to the server,
indicating the end of the command.
Confirm the success of the write operation by viewing the
ans = 13
udpport object is connected to an echo server, the data you
write is returned to the object. Read a string of ASCII data. The
readline function reads data until it reaches a terminator, removes the
terminator, and returns the data.
data = readline(u)
data = "Request Time"
When you are finished with the UDP object, clear it and turn off the echo server.
clear u echoudp("off")