Main Content

read

Read data from a connected Modbus device

    Description

    [out,status] = read(m,target,address) reads one data value to Modbus object m from target type target at the starting address address. The function reads one value by default. If you want to read more than one value, add the count argument.

    example

    [out,status] = read(m,target,address,count) reads data to Modbus object m from target type targetat the starting address address using the number of values to read count.

    Examples

    collapse all

    This example shows how to use modbus function to configure the NVIDIA® Jetson® TX2 as a Modbus® client and read from a remote server.

    Create a live hardware connection from the MATLAB® software to the NVIDIA hardware by using the jetson function. To create a live hardware connection object, provide the host name or IP address, user name, and password of the target board. For example:

    hwobj = jetson('jetson-board-name','ubuntu','ubuntu');
    

    Create a MATLAB function tx2ModbusClient.m that configures the Jetson TX2 board as a client and connect to the remote server at address '127.0.0.1' and port 502. This function acts as the entry-point for code generation.

    function out = tx2ModbusClient(target_func, address, count, val) %#codegen
    hwobj = jetson();
    
    % Modbus object creation
    m = modbus(hwobj,'127.0.0.1',1478,'Mode','Client');
    
    % Write to target register
    write(m, target_func, address, val);
    pause(0.1);
    
    % Read from a target register
    [out, ~] = read(m, target_func, address, count);
    pause(0.1);
    
    % Clear the Modbus object
    clear(m);
    
    end
    

    Create a GPU code configuration object for generating an executable. Use the coder.hardware function to create a configuration object for the Jetson platform and assign it to the Hardware property of the code configuration object cfg.

    cfg = coder.gpuConfig('exe');
    cfg.GenerateReport = true;
    cfg.Hardware = coder.hardware('NVIDIA Jetson');
    cfg.CustomInclude = fullfile('codegen','exe','tx2ModbusClient','examples');
    cfg.CustomSource  = fullfile('codegen','exe','tx2ModbusClient','examples','main.cu');

    The main.cu file is generated as part of code generation process. For this example, this file can be used without any modification.

    To generate CUDA code, use the codegen command and pass the GPU code configuration object along with the tx2ModbusClient entry-point function. After the code generation takes place on the host, the generated files are copied over and built on the target.

    codegen -config cfg tx2ModbusClient

    Use the runApplication function to launch the executable on the TX2 board.

    runApplication(hwobj,'tx2ModbusClient');

    Input Arguments

    collapse all

    Target area to read, specified as a character vector or string. You can perform a Modbus read operation on four types of targets: coils, inputs, input registers, and holding registers, corresponding to the values 'coils', 'inputs', 'inputregs', and 'holdingregs'. Target must be the first argument after the object name. This example reads 8 coils starting at address 1.

    Example: read(m,'coils',1,8)

    Data Types: char

    Starting address to read from, specified as a double. Address must be the second argument after the object name. This example reads 10 coils starting at address 2.

    Example: read(m,'coils',2,10)

    Data Types: double

    Number of values to read, specified as a double. Count must be the third argument after the object name. If you do not specify a count, the default of 1 is used. This example reads 12 coils starting at address 2.

    Example: read(m,'coils',2,12)

    Data Types: double

    Output Arguments

    collapse all

    Data read from the target area as a N-by-1 vector, where N represents the number of registers.

    • When reading coil or discrete inputs, the read operation is 1-bit and the output data is logical.

    • For holding register or input registers, the read is 16-bit and the output is uint16 datatype.

    Data Types: logical | uint16

    The status of the read operation.

    • 0: Indicates an unsuccessful read operation, which means that the data received on the out is invalid.

    • 1: Indicates a successful read operation, which means that the data received on the out is valid.

    Data Types: uint8

    Version History

    Introduced in R2022a

    See Also

    Functions

    Objects