Main Content

Modbus Client Write

Write data to Modbus server

Since R2024b

  • Modbus Client Write Block

Libraries:
Industrial Communication Toolbox / Modbus

Description

The Modbus Client Write block writes data to one or more registers on a Modbus® TCP/IP or serial server.

Each input port writes data to a corresponding register specified in the Registers parameter of the Modbus Client Write block.

Note

You need a license for Industrial Communication Toolbox™ and Simulink® to use this block.

Note

You must connect the Modbus Client Write block to a Modbus TCP/IP or serial server. You can create the client for connection to the Modbus TCP/IP or serial server using the Modbus pane in the Configuration Parameters dialog box of the model. You must add at least one Modbus Client Read or Modbus Client Write block in the model to enable this pane. For more information, see Configure Modbus Client in Simulink.

Other Supported Features

The Modbus Client Write block supports the use of Simulink accelerator and rapid accelerator modes. You can speed up the execution of Simulink models by using these modes. For more information on these simulation modes, see Acceleration (Simulink).

Examples

Ports

Input

expand all

Values to write to specified registers on a Modbus TCP/IP or serial server. The number of input ports (x) corresponds to the number of rows you add in the Registers parameter. When you write a vector of N values to a port, the block writes the values to N consecutive registers starting with the register address mentioned in the Registers parameter.

Dependencies

To enable this port, specify one or more registers in the Registers parameter.

Data Types: double

Parameters

expand all

To edit block parameters interactively, use the Property Inspector. From the Simulink Toolstrip, on the Simulation tab, in the Prepare gallery, select Property Inspector.

Select the Modbus client associated with the block. The block updates the Connection endpoint parameter once you select a client.

  • To add the clients using the Modbus pane, click Configure clients. For more information, see Configure Modbus Client in Simulink.

  • To update the client list and to show the latest Modbus clients, click Refresh.

Programmatic Use

To set the block parameter value programmatically, use the set_param (Simulink) function.

To get the block parameter value programmatically, use the get_param (Simulink) function.

Parameter: ClientList
Values: Modbus client name
Data Types: character vector | string

Server address to write to, specified as a nonnegative integer in the range [0, 255]. The default value is 1. The server address refers to the unit identifier for the Modbus TCP/IP server.

Programmatic Use

To set the block parameter value programmatically, use the set_param (Simulink) function.

To get the block parameter value programmatically, use the get_param (Simulink) function.

Parameter: ServerId
Values: 1 (default) | nonnegative integer in the range [0, 255]
Data Types: double

Add registers to write to the specified client.

  • To add a register, click Add. This action adds a row in the Registers table with the default values of Name, Register Type and Precision specified as Reg_1, Holding Register and uint16, respectively. You must specify an address for the register to write to in the Address column. Each click adds a new row in the Registers table with the name incremented by 1.

    • Sl. no. : This field is non editable and specifies the serial number of the register. The serial number determines the order in which the block inputs the signals at the input ports.

    • Name: This is an editable text field that specifies a name for the register. This field also defines the names for the input ports.

    • Address: This is an editable text field that specifies a starting address for the Modbus write operation.

    • Register Type: This is a drop-down list that specifies the target area to write. You can perform a Modbus write operation on two types of targets: coils and holding registers.

    • Precision: This is a drop-down list that specifies the data format of the write register. Note that the Precision field does not refer to the input port data type. It specifies how to interpret the register data. The input data type is always double.

      • For coils, set this field to bit.

      • For holding registers, set this field to int16, uint16, int32, uint32, single, double, int64 or uint64.

  • To delete registers, select the registers and click Delete.

  • To import a register table saved as a MAT file, click Import. This action opens the Load register table dialog box for the current workspace. Using this browser, you can select the MAT file with the register information. The MAT file that you select must have a table named ModbusRegisterTable.

    Note

    The Modbus Explorer app exports the contents of the register table as a MAT file. You can import this MAT file into the Modbus Client Simulink blocks to reuse the register table. For more information, see Use the Modbus Explorer App.

  • To save the register table as a MAT file, click Export. This action opens the Save register table dialog box for the current workspace. Using this browser, you can save the register table as a MAT file in the location of your choice. The default filename is ModbusRegisterTable.mat.

Programmatic Use

To set the block parameter value programmatically, use the set_param (Simulink) function.

To get the block parameter value programmatically, use the get_param (Simulink) function.

Parameter: RegisterTable
Values: register name and information
Data Types: character vector | string

Define the sample time of the block in seconds. The block writes the data to the server synchronously at the specified sample time. You can specify 0 for continuous mode, or -1 to inherit the sample time of the block connected to the input of the Modbus Client Write block. For more information, see Specify Sample Time (Simulink).

Programmatic Use

To set the block parameter value programmatically, use the set_param (Simulink) function.

To get the block parameter value programmatically, use the get_param (Simulink) function.

Parameter: SampleTime
Values: -1 (default) | numeric
Data Types: double

Extended Capabilities

Version History

Introduced in R2024b