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:
- Advanced OPC Data Logger Enterprise or a trial version.
- Script Execute plugin.
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: Filter data by tag value, write to database only on change
- Inserting OPC data to MS Access database through ODBC (features: adding groups and items, inserting to an ODBC database)
- Adding a timestamp in the CSV file (features: OPC groups)
- How to log OPC data to a SQL database.
- OPC to MSSQL: Writing a lot of OPC tags to MS SQL 2008 database
- OPC to MSSQL: Writing several OPC tags to separate columns
- OPC to MySQL: Writing OPC tags to MySQL 5 database
- OPC to a database: Writing OPC tags to a database
- OPC to Excel: Writing OPC data to Excel
- Filtering data by a tag value and write data to a database only when the value will change
- Filtering data out if an OPC tag value is not equal to a specified value
- Processing or storing OPC data by an event from the OPC server
- Aggregating data from multiple servers to one OPC server
Related topics: Advanced OPC Data Logger
hereOPC Logger RS232 pinout and signals Cables and signals Data monitor cables