Logging of one PLC variable with FB_DBWrite

Download: "Example to log a PLC variable with FB_DBWrite" sample3.zip

In this example the log of a PLC variable out of the PLC into a database will be demonstrated. The functionalities of each write-modes are shown.

Logging of one PLC variable with FB_DBWrite 1:

Used database type

MS SQL

Compatible database type

ASCII, MS SQL, MS Compact SQL, MS Access, MySQL, PostgreSQL, DB2, Oracle, InterBase/Firebird

Used function blocks

FB_DBWrite

Integrated libraries

"TcDatabase.lib", "TcSystem.lib","TcBase.lib","TcStandard.lib"

Download data list

FB_DBWrite_Sample.pro, CurrentConfigDataBase.xml, SQLQuery.sql

To use this example, you need to adjust the server name and the authentication in the XML-configuration data.

Furthermore, you need to keep in mind that no "TestDB" database is available before you carry out the SQLQuery.sql Script.

Configuration example:

With the help of the variable "eWriteMode" you can set with which write-mode it should be logged.
With a rising edge at the variable "bSTART" the write operation can be started.


Table:

Used table structure for each table

Column name

Data type

Zero allowed

Property

ID

bigint

no

IDENTITY(1,1)

Timestamp

datetime

no

 

Name

ntext

no

 

Value

float

no

 

Variable declaration

PROGRAM MAIN
VAR(*Test symbol which will be logged into the different database tables*)
    lrTestValueVar : LREAL := 123.456;

    eState : E_SampleState := eIdle;
    R_TRIG1: R_TRIG;

    (*With a rising edge at bStart the FB_DBWrite block will be start once*)
    bSTART: BOOL;

    (*With eWriteMode you can select which FB_DBWrite block will be used*)
    eWriteMode: E_SampleState := eWriteAppend;

    FB_DBWrite_Append: FB_DBWrite;
    FB_DBWrite_Update: FB_DBWrite;
    FB_DBWrite_RingBuffer: FB_DBWrite;

    (*Status outputs from the three FB_DBWrite blocks*)
    bBusy: BOOL;
    bErr: BOOL;
    bErrid: UDINT;
    stSqlstate: ST_DBSQLError;
END_VAR

Enum E_SampleState

TYPE E_SampleState :(
    eIdle := 0,
    eWriteAppend := 1,
    eWriteUpdate := 2,
    eWriteRingBuffer := 3
);
END_TYPE

PLC Program

CASE eState OF
    eIdle :
        R_TRIG1(CLK:=bSTART);
        IF R_TRIG1.Q THEN
            lrTestValueVar := lrTestValueVar + 1;
            eState := eWriteMode;
            bSTART := FALSE;
        END_IF(*Add a new record to the table tbl_Append*)
    eWriteAppend : 
        FB_DBWrite_Append(
            sNetID:= ,
            hDBID:= 1,
            hAdsID:= 1,
            sVarName:= 'MAIN.lrTestValueVar',
            nIGroup:= ,
            nIOffset:= ,
            nVarSize:= ,
            sVarType:= ,
            sDBVarName:= 'lrTestValueVar',
            eDBWriteMode:= eDBWriteMode_Append,
            tRingBufferTime:= ,
            nRingBufferCount:= ,
            bExecute:= TRUE,
            tTimeout:= T#15s,
            bBusy=> bBusy,
            bError=> bErr,
            nErrID=> bErrid,
            sSQLState=> stSqlstate);

        IF NOT bBusy THEN
            FB_DBWrite_Append(bExecute := FALSE);
            eState := eIdle;
        END_IF(*Add a new record to the table tbl_Update if it not exist else the existing record will be updated*)      
    eWriteUpdate :
        FB_DBWrite_Update(
            sNetID:= ,
            hDBID:= 2,
            hAdsID:= 1,
            sVarName:= 'MAIN.lrTestValueVar',
            nIGroup:= ,
            nIOffset:= ,
            nVarSize:= ,
            sVarType:= ,
            sDBVarName:= 'lrTestValueVar',
            eDBWriteMode:= eDBWriteMode_Update,
            tRingBufferTime:= ,
            nRingBufferCount:= ,
            bExecute:= TRUE,
            tTimeout:= T#15s,
            bBusy=> bBusy,
            bError=> bErr,
            nErrID=> bErrid,
            sSQLState=> stSqlstate);

        IF NOT bBusy THEN
            FB_DBWrite_Update(bExecute := FALSE);
            eState := eIdle;
        END_IF(*Add a new record to the table tbl_RingBuffer. If the maximum count is reached the records will be deleted in a FIFO process*)    
    eWriteRingBuffer :
        FB_DBWrite_RingBuffer(
            sNetID:= ,
            hDBID:= 3,
            hAdsID:= 1,
            sVarName:= 'MAIN.lrTestValueVar',
            nIGroup:= ,
            nIOffset:= ,
            nVarSize:= ,
            sVarType:= ,
            sDBVarName:= 'lrTestValueVar',
            eDBWriteMode:= eDBWriteMode_RingBuffer_Count,
            tRingBufferTime:= ,
            nRingBufferCount:= 10,
            bExecute:= TRUE,
            tTimeout:= T#15s,
            bBusy=> bBusy,
            bError=> bErr,
            nErrID=> bErrid,
            sSQLState=> stSqlstate);

        IF NOT bBusy THEN
            FB_DBWrite_RingBuffer(bExecute := FALSE);
            eState := eIdle;
        END_IFEND_CASE
Logging of one PLC variable with FB_DBWrite 2:

All the Microsoft SQL Compact databases that are declared in the XML configuration file must exist. They are not generated automatically.
The declared ASCII files, on the other hand, are generated automatically if they do not exist.