Main Content

Write and Read Binary Data over UDP

In this example, write and read binary data with a UDP object.

Write and Read Binary Data with Byte-Type UDP Port

Configure and Connect to the Server

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.

echoudp("on",4040)

Create a byte-type udpport object.

u = udpport
u = 

  UDPPort with properties:

     IPAddressVersion: "IPV4"
            LocalHost: "0.0.0.0"
            LocalPort: 51595
    NumBytesAvailable: 0

  Show all properties, functions

Write Binary Data

Use the write function to write the values 1:10 to the server.

write(u,1:10,"localhost",4040)

The function suspends MATLAB® execution until all the data is written or a timeout occurs as specified by the Timeout property of the udpport object.

By default, the write function writes binary data as uint8 data. For more information about specifying other data types, see write.

Read Binary Data

Confirm the success of the write operation by viewing the NumBytesAvailable property.

u.NumBytesAvailable
ans =

    10

Since each uint8 data is one byte and ten values were written, a total of ten bytes are available to be read from the object.

Since the udpport object is connected to an echo server, the data you write is returned to the object. Read the first five values of data as uint8 data.

data1 = read(u,5,"uint8")
data1 =

     1     2     3     4     5

After you read data, MATLAB removes it from the buffer. You can use the same command to read the next five bytes of data as uint8 from the buffer.

data2 = read(u,5,"uint8")
data2 =

     6     7     8     9    10

Clean Up

When you are finished with the UDP object, clear it and turn off the echo server.

clear u
echoudp("off")

Write and Read Binary Data with Datagram-Type UDP Port

Configure and Connect to the Server

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.

echoudp("on",4040)

Create a datagram-type udpport object.

u = udpport("datagram")
u = 

  UDPPort with properties:

         IPAddressVersion: "IPV4"
                LocalHost: "0.0.0.0"
                LocalPort: 65390
    NumDatagramsAvailable: 0

  Show all properties, functions

Set the maximum number of bytes of data to be written in a datagram packet. This determines the number of datagrams that will be written to the server.

u.OutputDatagramSize = 10;

Write Binary Data

Use the write function to write the values 1:15 to the server.

write(u,1:15,"localhost",4040)

The function suspends MATLAB execution until all the data is written or a timeout occurs as specified by the Timeout property of the udpport object.

By default, the write function writes binary data as uint8 data. For more information about specifying other data types, see write.

Read Binary Data

Confirm the success of the write operation by viewing the NumDatagramsAvailable property.

u.NumDatagramsAvailable
ans =

     2

Since you specified each datagram as ten bytes and 15 bytes were written, a total of two datagrams are available to be read from the object.

Since the udpport object is connected to an echo server, the data you write is returned to the object. Read the all the data as uint8 data.

data = read(u,u.NumDatagramsAvailable,"uint8")
data = 

  1×2 Datagram array with properties:

    Data
    SenderAddress
    SenderPort

View the first datagram and its values.

data1 = data(1)
data1 = 

  Datagram with properties:

             Data: [1 2 3 4 5 6 7 8 9 10]
    SenderAddress: "127.0.0.1"
       SenderPort: 4040

View the second datagram and its values.

data2 = data(2)
data2 = 

  Datagram with properties:

             Data: [11 12 13 14 15]
    SenderAddress: "127.0.0.1"
       SenderPort: 4040

Clean Up

When you are finished with the UDP object, clear it and turn off the echo server.

clear u
echoudp("off")

See Also

| | |

Related Topics