bg

Write data to a MODBUS device

Problem scenario:

My goal is to write an alarm flag back to PLC when read value is higher than a limit.

Requirements:

It is assumed that:

You have configured the communication settings on the device:

  • MODBUS TCP - IP address, Subnet, Gateway. You must assign a static IP address for the device.
  • MODBUS RTU - baud rate and the number of data bits.

Solution:

1. Create a new configuration from the main window using the "Green Plus" button. (fig. 1). This example shows the connection settings for MODBUS TCP. If your device uses MODBUS RTU, look here. If you've already configured the connection, then go to step #4.

IP connection settings
Fig. 1: IP connection settings

2. Go to your configuration setting: Modules - Query Parser Filter. Select the "MODBUS TCP" or "MODBUS RTU" plugin from lists. Then click the "Setup" button.

MODBUS plugin selection
Fig. 2: Selecting the MODBUS plugin

3. Click "Action - Add request" and add your request for reading data (fig. 3). Of course, in your application, you should configure the request parameters for your data (Offset, Registers to read, Response items).

Adding the "Read" request
Fig. 3: Adding the "Read" request

4. Click "Action - Add request" again and add a new request for writing data (fig. 4). The MODBUS function code should be:

  • 6 (Write single register) - if you need to write a value that allocated one MODBUS register in the PLC memory. For example, the value of the data type word, uint16, int16, decimal16.
  • 16 (Write multiple registers) - if you need to write larger values (dword, uint32, int32, decimal32, float, etc.).

Select the "Event" request method. Then specify the "WRITE" event identifier. You may use another identifier, but you should change in the next steps too.

The "Response items" group actually defines items to write. The "Default value" property defines a value to be written.

Adding the "Write" request
Fig. 4: Adding the "Write" request

5. Configure any of three filter plugins to generate the "WRITE" event on the necessary conditions.

5.1. Expressions - the simple plugin, but it includes all necessary operations (fig. 5, 6). The expression in this example compares the "VALUE" incoming value (from the "Read" request), and generates an event if the value is greater than 100.

Selecting the Expressions plugin
Fig. 5: Expressions plugin

SEND_EVENT_IF(VALUE > 100, 'WRITE')

Expression
Fig. 6: Expression

5.2. Script execute - the most flexible plugin. It allows implementing any logic, but it requires some programming skills (fig. 7, 8). The script sends an event if the "VALUE" incoming value is greater than 100.

Selecting the Script Execute plugin
Fig. 7: Script Execute plugin

const name = 'VALUE';
var v: variant;
begin
 if IsVariableDefined(name) then
  begin
   v := GetVariable(name);
   if v > 100 then
    SendEvent('WRITE');
  end;
end.

Script
Fig. 8: Script

5.3. Events generator - the simplest plugin, but it operates with text data. Therefore, it can only compare equal values. (fig. 9, 10).

Events generator plugin
Fig. 9: Events generator plugin

The rule
Fig. 10: The rule

6. Click the "OK" button and save all settings.

7. Now, the MODBUS plugin sends requests and parses responses for periodic requests. A filter plugin analyzes data for every request and sends an event to the MODBUS plugin when it is necessary. Keep in mind; the filter plugin cannot generate events if it did not receive data from the parser.

Advanced setup

The "Script Execute" plugin can send one or more named values with the event signal. It allows you to specify a value and the MODBUS offset in the event.

ADDRESS - the optional MODBUS offset.
VALUE - the optional value. The MODBUS plugin searches for a response item with the "VALUE" name. If the item exists, the plugin uses the value from the event instead of the default value.

const name = 'VALUE';
var v: variant;
begin
 if IsVariableDefined(name) then
  begin
   v := GetVariable(name);
   if v > 100 then
    SendEventEx('WRITE', ['ADDRESS', 1, 'VALUE', 2]);
  end;
end.

Here is an example for JScript.

const name = "VALUE";
var v;
{
 if (IsVariableDefined(name))
 {
   v = GetVariable(name);
   if (v > 100)
   {
     SendEventEx("WRITE", ["ADDRESS", 1, "VALUE", 2]);
   }
 }
}

Related articles: Write data to a MODBUS device

MODBUS RTU, MODBUS ASCII, MODBUS/TCP

IEC 62056-21