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.
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.
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).
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.
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.
Fig. 5: Expressions plugin
SEND_EVENT_IF(VALUE > 100, 'WRITE')
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.
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.
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).
Fig. 9: Events generator plugin
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
- MODBUS power meter data logging (easy method)
- Sunspec-compatible MODBUS power meters, inverters (easy method)
- MODBUS RTU/TCP polling: Configuring master station (MODBUS RTU, MODBUS TCP, requests, response items).
- MODBUS poll: How to make sure that the application sends requests and receives responses?
- MODBUS poll: How to view register values, not raw MODBUS packets?
- MODBUS polling: How to make sure that the application correctly interprets the responses received from the device?
- MODBUS polling: How to view MODBUS register values in a more easy-to-grasp form (graphs, indicators, etc.)?
- MODBUS: How to combine the data of two requests?
- MODBUS: What is the right way to poll multiple devices?
- Copy settings from Simply MODBUS RTU Master to our Modbus Data Logger.
- Copy settings from the MODBUS Poll utility.
- Controlling PLC coil registers status using MODBUS TCP (MODBUS data parser, custom scripts, events generating, and handling).
- MODBUS to MSSQL: Write MODBUS registers to separate columns
- MODBUS to MySQL: Write MODBUS values to the MySQL database
- MODBUS to a database: Writing MODBUS RTU/TCP values to a database
- MODBUS to a database: Write data to two different tables.
- MODBUS to a database: Write data to two different databases, making a complete copy.
- Sentron PAC 3200: MODBUS TCP Data Logging
- Write data to a MODBUS device
- SQL to MODBUS: Send data from a SQL database to MODBUS.
- MODBUS TCP ↔ MODBUS RTU real-time conversion.
IEC 62056-21