Main Content

read

Read data from UDP socket

Since R2020b

    Description

    data = read(u,count) reads the specified number of values from the udpport socket u, using the default precision of uint8 to interpret numeric data. For a byte-type udpport object u, the result data is a row vector of doubles. For a datagram-type udpport object u, the result data is a Datagram structure or array of structures.

    example

    data = read(u,count,datatype) reads from the udpport socket u, with the precision specified by datatype. For a numeric datatype, the values are returned as double. For datatype of "char" or "string", the results are the specified type.

    example

    Examples

    collapse all

    This example shows how to read byte data.

    Read 5 values of uint32 data from the udpport socket.

    u = udpport("IPV4");
    data = read(u,5,"uint32");

    The 5 values comprise a total of 20 bytes of uint32 data at the udpport socket. In MATLAB, data is an array of doubles.

    This example shows how to read datagram data.

    Turn echoudp on at port 3030, then create a datagram-type udpport object with an OutputDatagramSize of 5.

    echoudp("on",3030);
    u = udpport("datagram","OutputDatagramSize",5);

    Send 20 bytes of uint8 data to the echoudp port.

    write(u,1:20,"uint8","127.0.0.1",3030);

    Because OutputDatagramSize is set to 5, the 20 bytes are sent as 4 datagram packets, each containing 5 bytes of data.

    Verify that 4 datagrams were received from the echo server.

    u.NumDatagramsAvailable
    ans =
    
         4

    Read the 4 datagrams received from the echo server.

    data = read(u,u.NumDatagramsAvailable,"uint8")
    data = 
    
      1×4 Datagram array with properties:
    
        Data
        SenderAddress
        SenderPort

    The first datagram contains the values 1-5 (5 bytes), the second 6-10, the third 11-15, and the fourth 16-20.

    View the third datagram.

    data(3)
    ans = 
    
      Datagram with properties:
    
                 Data: [11 12 13 14 15]
        SenderAddress: "127.0.0.1"
           SenderPort: 3030

    Input Arguments

    collapse all

    UDP socket, specified as a udpport object.

    Example: u = udpport

    Data Types: udpport object

    Number of values or datagrams to read from udpport socket, specified as a numeric value. You cannot specify count as 0, Inf, or NaN. If count is greater than the NumBytesAvailable or NumDatagramsAvailable property of the udpport object, then the function waits until the specified number of values or datagrams are read or until timeout occurs.

    Example: 16

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    MATLAB data type for each value, specified as a string or character vector. datatype specifies the number of bits to read for each value, and the interpretation of those bits as a MATLAB data type. Allowed values are "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "double", "single", "char", and "string".

    Example: "uint16"

    Data Types: char | string

    Output Arguments

    collapse all

    Values read from the udpport socket. For a byte-type udpport object, the result is a string, character vector or 1-by-N row vector of doubles where N is the number of values specified by count. For a datagram-type udpport object, the result is a Datagram structure or array of structures. If no data is returned, the data is empty.

    Version History

    Introduced in R2020b

    See Also

    Functions