PLC Client
This PLC sample shows the use of the TwinCAT Analytics Storage Provider library. The sample code shows reading and writing. For the sample to work coherently, both the use of the Analytics Logger for sending measured data to an MQTT Message Broker and the import of historical data via the Analytics Stream Helper are shown.
The basis is an appropriately set up native MQTT Message Broker and an Analytics Storage Provider service.
The PLC sample shows the following steps:
- Analytics Logger: stream of variables from a Global Variable List to a MQTT Message Broker.
- Read Stream Description: reception of the stream description from the MQTT Message Broker. Evaluation of the JSON description for the use of the necessary IDs.
- Analytics Storage Provider: starting and stopping recordings, as well as reading recordings and historical data.
- Analytics Stream Helper: receiving the historical data from the Analytics Storage Provider and mapping the data into a Global Variable List for the historical data.
To use the Storage Provider in the PLC, different GUIDs are necessary for the identification of services and data. The following screenshots show where the corresponding GUIDs can be found.
- TwinCAT System ID
- Symbol Info GUID
- Storage Provider Recorder GUID
- Storage Provider Service GUID
TwinCAT System ID
Symbol Info GUID
Identification ID for the symbols from the TwinCAT Analytics binary format.
Storage Provider Recorder GUID
Identification GUID for the Storage Provider Recorder in Visual Studio® - this is installed with the TwinCAT Measurement setup.
Storage Provider Service GUID
GUID of the individually used Storage Provider Service. If you are connected to a Storage Provider in the Recorder window, you can find the Provider GUID in the properties.
Sample code architecture
All relevant parts of the configuration and the program code are marked in the following picture:
Stream Helper
For receiving the historical data sent by the Analytics Storage Provider via MQTT.
Variable Live/Historical
The GVL is for the live data and the GVL_Hist is for the historical data.
Storage Provider Command Helper Functions
These Helper Functions generate the commands for communication with the Storage Provider Service in JSON format.
IsRecordingActive
This shows you how to parse the description sent by the Analytics Storage Provider using the JsonXML library, for example, and obtain information as to whether a specified Record Alias name is currently being recorded.
Description Helper Function Block
This function block receives the information from the Description Topic, including the information about the SystemID.
MAIN Programs
The Main program invokes communication to the Analytics Storage Provider. The Main Historical program implements the mapping of historical data from the Stream Helper into the GVL_Hist.
Analytics Logger
Sends the variables of the GVL to an MQTT Message Broker.
Sample Start
Before the sample can be started, you must set the MQTT Message Broker you are using in three different places.
Analytics Stream Helper:
Analytics Logger:
MAIN program:
You must then change the sProviderGUID for the FB_ALY_StorageProvider. This can be found as described above in this document.
Now go to the MAIN program to control the sample. With the enum eCtrl you can set the action you would like to perform. The available options are:
- StartRecord
- StopRecord
- ReadRecords
- GetHistorical
With a rising edge at the variable bExecute the action selected in the enum is executed. If you have made more than one record, you can see this in the array aRecordInfo. With the index it is then possible to select the different records. The timespans are also displayed, you could theoretically still adjust these within the timespan. To do this, you would need to modify the logic of the sample in the helper function F_CreateAlySPGetHistCmd accordingly.
The Storage Provider Recoder GUID selected in the document above can optionally be set in the PLC in the F_CreateAlySPStartRecordCmd function. Theoretically, it can be any GUID, it is only used to identify the recorder - in this case the PLC. However, if you set the Recorder GUID from your own engineering, the record is displayed directly in the Analytics Storage Provider Recorder. If a different GUID is used, the filter function in the Recorder window may first have to be activated so that the record from the PLC is displayed.
Download: Sample