Using data logging services from the PyPLCNextRSC library in Python
| Version Number | Description |
|---|---|
| 1.0 | Initial version |
This application note shows how to use the PyPLCNextRSC library to access data logging services.
1. Supported Components
| Component Name | Version |
|---|---|
| iC9200 series | ≥ 2024.3 |
| iCube Engineer | ≥ 2024.3 |
2. Solution details
The DataLogger is a service component designed to transfer real-time data through the GDS port to a database file, such as SQLite, for recording, analysis & storage purposes. If no triggering conditions are specified, the DataLogger session automatically starts when the controller powers up. It then collects task-synchronous values from the configured GDS ports at a defined sampling rate and stores them, along with a timestamp, in the database file.
A DataLogger instance can be configured in multiple ways: through iCube Engineer DataLogger Sessions tab or by placing the XML configuration file directly in the project’s services folder via SSH, or using the RSC IDataLoggerService as detailed in this application note.
Note:
The DataLogger session intiated via this RSC service will not perform the Download Changes command as this is true even if sessions are not currently running but are created already.
2.1 iCube Engineer Project Creation
To demonstrate datalogging services, we first need to create an iCube project and define some GDS variables. These variables will serve as the data points that can be recorded using the datalogging services. The datalogger can only subscribe to a GDS port to read real-time application data.
Create an iCube project with a program called Main. Add some GDS variables as follows
1. 'MyVar1' & 'MyVar2' of type
DINT.
2. 'Trigger' of type
BOOL.
Select the usage as
OUT Port
to configure it as
GDS
ports.
Now, add some logic in the code worksheet to increment the variables to some random number every scan.
Rebuild and download the project to the controller. Once the project has been downloaded and is running, switch to debug mode and verify the GDS Port List. The variables
'MyVar1'
& 'MyVar2' should be visible in the GDS Port List as OUT Ports and should increment with every scan.
Now, let's use the RSC datalogger service in
Python
to read and store the real-time values of these variables to a database file (.db).
2.2 RSC Installation & Connection
To use the RSC service in a
Python
script, start by installing the PyPlcnextRsc service package by entering the following command in the terminal.
Python
version 3.7.6 or higher is required for this installation. After the installation, import the supporting modules and objects.
Use the Device class to establish communication with the iC9200 controller as shown below. Define the ipAddress, user name & password as required.
2.3 Create and configure the datalogger session
A new or empty session can be created using the CreateSession method from IDataLoggerService2. The following parameters are required when creating a new session:- sessionName: Name for the new session. The name must be unique. Must not begin with PCWE_
- persistent:
- false (default value): when the attribute is set to false, the session will be deleted when the controller is disconnected.
- true: when it is set to true, the session will remain active until it is deleted.
Use configureSession method to configure or change the existing configuration of a session. The settings include the publishing rate, sampling rate, buffer capacity and sink type which specifies the output form of the data that is being recorded.
SinkType::Database: Use an SQLite-based database to store the recordings permanently.
SinkType::Volatile:
Store data in RAM within the application. If the controller is switched Off or is restarted, the data will be lost. Access via file system is not possible with the volatile sink.
Variables
of this sink type can only be queried using Remote Service Calls (RSC) via ReadVariableData
2.4 Add Variables to be recorded
Variables
can be added or removed using the SetVariables
method. A maximum of 996 variables per session is allowed.
2.5 Start/Stop Recording
The recording can be initiated in two ways.
1. Trigger Conditions : Starts recording when the specified trigger condition is met.
2. Continuous: Starts recording immediately when the session begins.
Trigger Condition:
If there is no trigger condition defined, then it will automatically be assumed as continuous recording by default.
Once, the trigger condition is set the session can be started by calling StartSession method as shown below.
When executing the python script, the session will start recording when the 'Trigger' variable turns to TRUE and will finish recording after 20 cycles as previously configured.
Set the Trigger Variable to 'TRUE' to initiate recording
2.6 Access the database file
Once the session has finished recording. The database file (test.db) can be obtained at the configured location i.e opt/plcnext/user/test.db using ssh.
The file can then be viewed using any SQLite database viewer like DB Brower