Main Content

modbus

Create a Modbus object for NVIDIA boards

    Description

    Use this object to create a MODBUS® connection over TCP/IP and communicate with another MODBUS device on the network, such as a PLC or temperature controller. You can use the 'Mode' property to configure the NVIDIA DRIVE® or Jetson® hardware as a MODBUS client and read from a server or configure the NVIDIA® hardware as a MODBUS server. To perform read, write, or configuration operations, use this object with the functions listed in Object Functions.

    Creation

    Description

    example

    m = modbus(hwObj,deviceAddress) creates a Modbus client object, m and connects to the MODBUS server at the specified address using the TCP/IP port 502. Port number 502 is the reserved port for the MODBUS protocol.

    m = modbus(hwObj,deviceAddress,port) optionally specifies the remote port to be used when connecting to the MODBUS server.

    example

    m = modbus(hwObj,Mode='server') configures the NVIDIA hardware board as a MODBUS server at the IP address of the hardware object and TCP/IP port 502. Port number 502 is the reserved port for the MODBUS protocol.

    m = modbus(hwObj,port,Mode='server') optionally specifies the port used by the MODBUS server.

    m = modbus(___,Name,Value) specifies additional options with one or more name-value pair arguments using any of the previous syntaxes for the MODBUS server.

    Properties

    expand all

    Connection to a specific NVIDIA hardware board, specified as a jetson or drive object.

    IP address or host name of the remote MODBUS server that the Modbus client object connects to, specified as a character vector or string.

    Example: m = modbus(jetsonObj,'192.168.2.1')

    Data Types: char

    TCP/IP port, specified as a double. When the 'Mode' is 'Client', port indicates the TCP/IP port of the remote MODBUS server the client connects to. When the 'Mode' is 'Server', port indicates the TCP/IP port used by the MODBUS server object. The default of 502 is used if none is specified.

    Example: m = modbus(jetsonObj,'192.168.2.1',308)

    Data Types: double

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

    Example: m = modbus(jetsonObj,1478,Mode='server',... CoilStartAddress=1,NumCoils=10)

    There are a number of name-value pairs that can be used when you create the modbus object, including the two shown here. Some can only be used with either TCP/IP or Serial RTU, and some can be used with both transport types.

    Mode of operation of the Modbus object for the specified jetson or drive object.

    Example: m = modbus(jetsonObj,1478,Mode='server',... CoilStartAddress=1,NumCoils=10)

    To read or write to a group of coils on the server, specify the address of the first coil in the group. For example, to read or write to coils with addresses ranging from 10 to 14, enter 10.

    Example: m = modbus(jetsonObj,1478,Mode='server',... CoilStartAddress=10)

    To read or write to a group of coils on the server, specify the number of coils in the group. For example, to read or write to 4 coils, enter 4.

    Example: m = modbus(jetsonObj,1478,Mode='server',... CoilStartAddress=10,NumCoils=4)

    To read from a group of discrete inputs on the server, specify the address of the first discrete input in the group. For example, to read from discrete inputs with addresses ranging from 15 to 24, enter 15.

    Example: m = modbus(jetsonObj,1478,Mode='server',... InputStartAddress=15)

    To read from a group of discrete inputs on the server, specify the number of discrete input in the group. For example, to read from 10 discrete inputs, enter 10.

    Example: m = modbus(jetsonObj,1478,Mode='server',... InputStartAddress=15,NumInputs=10)

    To read from a group of input registers on the server, specify the address of the first input register in the group. For example, to read from input registers with addresses ranging from 25 to 34, enter 25.

    Example: m = modbus(jetsonObj,1478,Mode='server',... InputRegStartAddress=25)

    To read from a group of input registers on the server, specify the number of input registers in the group. For example, to read from 10 input registers, enter 10.

    Example: m = modbus(jetsonObj,1478,Mode='server',... InputRegStartAddress=25,NumInputRegs=10)

    To read or write to a group of holding registers on the server, specify the address of the first holding register in the group. For example, to read or write to holding registers with addresses ranging from 35 to 44, enter 35.

    Example: m = modbus(jetsonObj,1478,Mode='server',... HoldingRegStartAddress=35)

    To read or write to a group of holding registers on the server, specify the number of holding registers in the group. For example, to read or write to 10 holding registers, enter 10.

    Example: m = modbus(jetsonObj,1478,Mode='server',... HoldingRegStartAddress=35,NumHoldingRegs=10)

    Object Functions

    readRead data from a connected MODBUS device
    writePerform a write operation to the connected MODBUS device

    Examples

    collapse all

    This example shows how to use modbus function to configure the NVIDIA Jetson TX2 as a MODBUS 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 tx2ModbusServer.m that configures the Jetson TX2 board as a server and allocate address space for coil, discrete inputs, input registers and holding registers. This function acts as the entry-point for code generation.

    function tx2ModbusServer() %#codegen
    
    hwobj = jetson();
    m = modbus(hwobj, 1478, 'Mode', 'server',...
        'CoilStartAddress', 1, 'NumCoils', 10, ...
        'InputStartAddress', 11, 'NumInputs', 10,...
        'InputRegStartAddress', 21, 'NumInputRegs', 10,...
        'HoldingRegStartAddress', 31, 'NumHoldingRegs', 10);
    
    val = 1;
    for i = 1:10
        write(m, 'coils', i, val);
        write(m, 'inputs', i+10, val);
        write(m, 'inputregs', i+20, i+20);
        write(m, 'holdingregs', i+30, i+30);
        pause(0.1);
    end
    
    for i=1:1000
        % Holding the server live
        pause(0.5);
    end
    
    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','tx2ModbusServer','examples');
    cfg.CustomSource  = fullfile('codegen','exe','tx2ModbusServer','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 tx2ModbusServer 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 tx2ModbusServer

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

    runApplication(hwobj,'tx2ModbusServer');

    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');

    Version History

    Introduced in R2022a

    See Also

    Functions

    Objects