Main Content

readbinblock

Read one binblock of data sent to TCP/IP server

    Description

    example

    data = readbinblock(t) reads a binblock of data sent to the TCP/IP server t from the client connected to it and returns the data as a row vector of doubles. The function suspends MATLAB® execution until the number of values specified in the binblock is read or a timeout occurs.

    example

    data = readbinblock(t,datatype) reads a binblock of data interpreted as the type specified by datatype. For numeric types, the data is returned as a row vector of doubles. For text types, the data is returned as a character vector or string, as specified.

    Examples

    collapse all

    Create a TCP/IP server that listens for connections at localhost and port 4000.

    server = tcpserver("localhost",4000)
    server = 
      TCPServer with properties:
    
            ServerAddress: "127.0.0.1"
               ServerPort: 4000
                Connected: 0
            ClientAddress: ""
               ClientPort: []
        NumBytesAvailable: 0
    
      Show all properties, functions
    
    

    Create a TCP/IP client to connect to your server object using tcpclient. You must specify the same IP address and port number you use to create server.

    client = tcpclient("localhost",4000)
    client = 
      tcpclient with properties:
    
                  Address: 'localhost'
                     Port: 4000
        NumBytesAvailable: 0
    
      Show all properties, functions
    
    

    Write a binblock of data to the client in uint8 format.

    writebinblock(client,[4,8,15,16,23,42],"uint8")

    Since the client is connected to the server, the data you write to the client is available to be read from the server object. Read the binblock of data.

    readbinblock(server)
    ans = 1×6
    
         4     8    15    16    23    42
    
    

    If you write a binblock of data to the client that is not in uint8 format, you must specify the data type when reading it from the server object.

    writebinblock(client,"Hello, world!","string")
    readbinblock(server,"string")
    ans = 
    "Hello, world!"
    

    Input Arguments

    collapse all

    TCP/IP server, specified as a tcpserver object.

    Example: readbinblock(t) reads a binblock of data sent to the TCP/IP server t from the client connected to it.

    Size and format of each value, specified as a character vector or string. datatype determines the number of bytes to read for each value and the interpretation of those bytes as a MATLAB data type.

    Example: readbinblock(t,"double") reads a binblock of double data.

    Data Types: char | string

    Introduced in R2021a