Logging of a PLC variable with FB_DBWrite
This sample illustrates logging of a PLC variables from the PLC in a database and the operating principle of the individual write modes.
Download: TcDBSrv_InfoSysSamples.zip

Database type used | MS SQL |
Compatible database types | ASCII, MS SQL, MS Compact SQL, MS Access, MySQL, PostgreSQL, DB2, Oracle, InterBase/Firebird, XML |
Function blocks used | FB_DBWrite |
Libraries to be integrated | Tc2_Database, Tc2_System, Tc2_Standard |
Download file list | TcDBSrv_InfoSysSamples.tszip, CurrentConfigDataBase.xml, SQLQuery.sql |
In order to be able to use this sample, you have to adapt the server name and the authentication in the XML configuration file (CurrentConfigDataBase.xml). Ensure that no "TestDB" database is present before executing the SQLQuery.sql script.
Sample configuration:
The variable "eWriteMode" can be used to set the write mode for logging.
The write operation can then be started with a positive edge at the variable "bSTART".
Table assignment:
- ADS_TO_DB_APPEND => eWriteAppend -> "tbl_Append"
- ADS_TO_DB_UPDATE => eWriteUpdate -> "tbl_Update"
- ADS_TO_DB_RINGBUFFER => eWriteRingBuffer -> "tbl_RingBuffer"
Table structure used
Column name |
Data type |
Null permitted |
Feature |
---|---|---|---|
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_IF
END_CASE
![]() | TwinCAT Database Server All Microsoft SQL Compact databases, which 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. |
Requirements
Development environment |
Target platform |
PLC libraries to be linked |
---|---|---|
TwinCAT v3.0.0 |
PC or CX (x86) |
Tc2_Database |