Main Content

configureCallback

Set callback function and trigger condition for communication

Since R2021a

    Description

    configureCallback(t,"terminator",callbackFcn) sets the callback function callbackFcn to trigger whenever a terminator is available to be read from the client connected to the TCP/IP server t. The syntax sets the BytesAvailableFcnMode property of t to "terminator" and the BytesAvailableFcn property to callbackFcn.

    Set the terminator character using configureTerminator.

    example

    configureCallback(t,"byte",count,callbackFcn) sets the callback function callbackFcn to trigger whenever a new count number of bytes are available to be read. The syntax sets the BytesAvailableFcnMode property of t to "byte", the BytesAvailableFcnCount property to count, and the BytesAvailableFcn property to callbackFcn.

    example

    configureCallback(t,"off") turns off callbacks. The syntax sets the BytesAvailableFcnMode property of t to "off".

    example

    Examples

    collapse all

    Create a callback function called readFcn and save it as a .m file in the current working directory.

    function readFcn(src,~)
    message = readline(src);
    disp(message)
    end
    

    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
    
    

    Set the callback function readFcn to trigger when a terminator is available to be read in the server. The callback function readFcn reads and displays the message sent from the connected client.

    configureCallback(server,"terminator",@readFcn)

    View the properties to confirm the change.

    server.BytesAvailableFcnMode
    ans = 
    "terminator"
    
    server.BytesAvailableFcn
    ans = function_handle with value:
        @readFcn
    
    

    Write a string of ASCII data to the TCP/IP client. Since the client is connected to the server, this data is available in the server. When the server receives the data with a terminator from the client, server triggers its callback function, which displays the data.

    writeline(client,"Hello, world!")

    Hello, world!

    Turn the callback off.

    configureCallback(server,"off")

    Verify that the callback is off.

    server.BytesAvailableFcnMode
    ans = 
    "off"
    

    Create a callback function called readByteFcn and save it as a .m file in the current working directory.

    function readByteFcn(src,~)
    data = read(src,src.NumBytesAvailable);
    disp(data)
    end
    

    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
    
    

    Set the callback function readByteFcn to trigger when five bytes of new data are available to be read in the server. The callback function readByteFcn reads and displays all the data sent from the connected client.

    configureCallback(server,"byte",5,@readByteFcn)

    View the properties to confirm the change.

    server.BytesAvailableFcnMode
    ans = 
    "byte"
    
    server.BytesAvailableFcnCount
    ans = 5
    
    server.BytesAvailableFcn
    ans = function_handle with value:
        @readByteFcn
    
    

    Write five bytes of data to the TCP/IP client. Since the client is connected to the server, this data is available in the server. When the server receives the five bytes of data from the client, server triggers its callback function, which displays the data.

    write(client,1:5,"uint8")

    1 2 3 4 5

    Turn the callback off.

    configureCallback(server,"off")

    Verify that the callback is off.

    server.BytesAvailableFcnMode
    ans = 
    "off"
    

    Input Arguments

    collapse all

    TCP/IP server, specified as a tcpserver object.

    Example: configureCallback(t,"byte",128,@callbackFcn) sets the callbackFcn callback to trigger each time 128 bytes of new data are available to be read from the TCP/IP server t.

    Number of bytes of available data to trigger the callback, specified as a positive integer value. Set the BytesAvailableFcnCount property using this argument.

    Example: configureCallback(t,"byte",128,@callbackFcn) sets the callbackFcn callback to trigger each time 128 bytes of new data are available to be read.

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

    Callback function to run when trigger condition is met, specified as a function handle. The function handle can be a named function handle or an anonymous function with input arguments. Set the BytesAvailableFcn property using this argument.

    Example: configureCallback(t,"terminator",@callbackFcn) sets the callbackFcn callback to trigger when a terminator is available to be read.

    Data Types: function_handle

    Version History

    Introduced in R2021a