Main Content


Perform a write operation to the connected Modbus device

Since R2022a

    Add-On Required: This feature requires the MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms add-on.



    status = write(m,target,address,values) writes data to Modbus object m to target type target at the starting address address using the values to read values. You can write to coils or holding registers.


    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);
    for i=1:1000
        % Holding the server live

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

    The file is generated as part of code generation process. For this example, you can this file without 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.


    Input Arguments

    collapse all

    Target area to write to, specified as a character vector or string. You can perform a Modbus write operation on two types of targets: coils and holding registers, so you must set the target type as either 'coils' or 'holdingregs'. Target must be the first argument after the object name. This example writes to 4 coils starting at address 8289.

    Example: write(m,'coils',8289,[1 1 0 1])

    Data Types: char

    Starting address to write to, specified as a double. Address must be the second argument after the object name. This example writes to 6 coils starting at address 5200.

    Example: write(m,'coils',5200,[1 1 0 1 1 0])

    Data Types: double

    Array of values to write, specified as a double or array of doubles. values must be the third argument after the object name. If the target is coils, valid values are 0 and 1. If the target is holding registers, valid values must be in the range of the specified precision. You can include the array of values in the syntax, as shown here, or use a variable for the values.

    This example writes to 4 coils starting at address 8289.

    Example: write(m,'coils',8289,[0 1 0 1])

    Data Types: double

    Output Arguments

    collapse all

    The status of the write operation.

    • 0: Indicates an unsuccessful write operation.

    • 1: Indicates a successful write operation.

    Data Types: uint8

    Extended Capabilities

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    GPU Code Generation
    Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

    Version History

    Introduced in R2022a

    See Also