Filter data by tag value, write to database only on change
Problem scenario
I have this item: Kemro.opc.4.IF1.1.92.Mars1.SVs.system.sv_ShotCounter[14]="1562" in OPC Data Logger. I want to take data and write it to MySQL, only when this counter changes.
Requirements:
It is assumed that:
1. You've created a configuration (using the "Green Plus" button in the main window and added an OPC group in the OPC logger and can receive any data from a selected OPC server.
2. You've configured a data export to MySQL.
Solution:
1. Enable highlighted options in the OPC group settings.
Fig.1. OPC group settings
2. Download and install the "Script Execute" plugin.
3. Enable it (fig. 2).
Fig.2. Script Execute plugin
4. Select the plugin in the list and click the "Setup" button below.
5. In the script editor window select "PascalScript" from the "Script type" list
6. Copy and paste the following code. Modify it if necessary. The row sName1:string = 'Kemro.opc.4.IF1.1.92.Mars1.SVs.system.sv_ShotCounter'; contains the name of the controlled OPC tag.
7. Click the "Check" button and verify that the script was compiled successfully.
8. Click the "OK" button and save all changes.
The PascalScript code
var v1,v2:double;
sName1:string = 'Kemro.opc.4.IF1.1.92.Mars1.SVs.system.sv_ShotCounter';
sName2:string = 'LAST_VALUE';
begin
// checks that a variable is stored before
if IsVariableStored(sName2) then
// retrieves a stored variable
v1 := PopVariable(sName2)
else
// otherwise initialize our value
v1 := 0;
// checks a variable in a data packet
if IsVariableDefined(sName1) then
begin
v2 := GetVariable(sName1);
// stores a value between script executions
PushVariable(sName2, v2);
// the script will discard data packets by the following condition
if v1 = v2 then
DiscardDataPacket();
end
else
// discard data also when the counter didn't read yet
DiscardDataPacket();
end.
Related articles:
OPC and DCOM Configuration on Windows 2008 and Windows 7
OPC and DCOM Configuration on Windows 10, 11, Windows Server 2019, 2022