Example with the FB_DBRecordInsert and FB_DBRecordSelect function blocks
This example illustrates logging of several values in a database from the PLC with the function block FB_DBRecordInsert. In this example, several PLC variables are logged in a single record. In addition, the function block FB_DBRecordSelect can be used to read a record from this database.
Download: TcDBSrv_InfoSysSamples.zip

Database type used | MS Access |
Compatible database types | MS SQL, MS Compact SQL, MS Access, MySQL, PostgreSQL, DB2, Oracle, InterBase/Firebird, XML |
Function blocks used | FB_DBRecordInsert, FB_DBRecordSelect |
Libraries to be integrated | "Tc2_Database", "Tc2_System", "Tc2_Standard", "Tc2_Utilities" |
Download file list | TcDBSrv_InfoSysSamples.tszip, CurrentConfigDataBase.xml, TestDB_Access.mdb |
The following table structure is used for writing:
Column name | Data type |
---|---|
Timestamp | datetime |
PLC_TestValue1 | float |
PLC_TestValue2 | float |
PLC_TestValue3 | float |
PLC_TestValue4 | String |
Variable Declaration
(* Declaration *)PROGRAM MAIN
VAR
eState : E_SQLStatement;
NT_GetTime1 : NT_GetTime;
bTimestart : BOOL;
tTime : TIMESTRUCT;
FB_FormatStringDateTime: FB_FormatString;
sDateTimeString : T_MaxString;
TestValue1 : REAL := 123.456;
TestValue2 : REAL := 234.567;
TestValue3 : REAL := 345.678;
TestValue4 : STRING(255) := 'No error occurred';
FB_FormatString1 : FB_FormatString;
sInsertString : T_MaxString;
bError : BOOL;
nErrid : UDINT;
FB_DBRecordInsert1: FB_DBRecordInsert;
bStartstopInsert : BOOL;
bBusyInsert : BOOL;
bErrInsert : BOOL;
nErridInsert : UDINT;
stSQLStateInsert : ST_DBSQLError;
stRecord : ST_Record;
FB_DBRecordSelect1: FB_DBRecordSelect;
nRecIndex : UDINT := 0;
bStartstopSelect : BOOL;
bBusySelect : BOOL;
bErrorSelect : BOOL;
nErrIDSelect : UDINT;
stSQLStateSelect : ST_DBSQLError;
nRecordCount : UDINT;
END_VAR
Enum E_SQLStatement
TYPEE_SQLStatement:(
eSQL_INSERT := 0,
eSQL_SELECT := 1
);
END_TYPE
Struct ST_Record
TYPEST_Record :
STRUCT
Timestamp : DT;
PLC_Value1: REAL;
PLC_Value2: REAL;
PLC_Value3: REAL;
PLC_Value4: STRING;
END_STRUCT
END_TYPE
PLC program
CASEeState OF
eSQL_INSERT:
(*Create the timestamp*)
NT_GetTime1( START:= bTimestart, TIMESTR=> tTime);
IF NOT NT_GetTime1.BUSY THEN
bTimestart:= NOT bTimestart;
END_IF
FB_FormatStringDateTime(
sFormat := '%D.%D.%D %D:%D:%D',
arg1 := F_WORD(tTime.wYear),
arg2 := F_WORD(tTime.wMonth),
arg3 := F_WORD(tTime.wDay),
arg4 := F_WORD(tTime.wHour),
arg5 := F_WORD(tTime.wMinute),
arg6 := F_WORD(tTime.wSecond),
sOut => sDateTimeString);
(*Create the SQL-INSERT command*)
FB_FormatString1(
sFormat := 'INSERT INTO tbl_Test VALUES($'%S$',%F,%F,%F,$'%S$')',
arg1 := F_STRING(sDateTimeString),
arg2 := F_REAL(TestValue1),
arg3 := F_REAL(TestValue2),
arg4 := F_REAL(TestValue3),
arg5 := F_STRING(TestValue4),
sOut => sInsertString,
bError => bError,
nErrId => nErrid);
(*Write the record to the database*)
FB_DBRecordInsert1(
sNetID := ,
hDBID := 1,
sInsertCmd:= sInsertString,
bExecute := bStartstopInsert,
tTimeout := T#15s,
bBusy => bBusyInsert,
bError => bErrInsert,
nErrID => nErridInsert,
sSQLState => stSQLStateInsert);
eSQL_SELECT:
(*Read one record from the database*)
FB_DBRecordSelect1(
sNetID := ,
hDBID := 1,
sSelectCmd:= 'SELECT * FROM tbl_Test',
nRecordIndex:= nRecIndex,
cbRecordSize:= SIZEOF(stRecord),
pDestAddr := ADR(stRecord),
bExecute := bStartstopSelect,
tTimeout := T#15s,
bBusy => bBusySelect,
bError => bErrorSelect,
nErrID => nErrIDSelect,
sSQLState => stSQLStateSelect,
nRecords => nRecordCount);
END_CASE
To use this sample, you have to declare the Access database "Sample7.mdb" in the XML configuration file.
A record with the four PLC values and the timestamp is created in the database by generating a positive edge at the variable "bStartstopInsert".

Requirements
Development environment |
Target platform |
PLC libraries to be linked |
---|---|---|
TwinCAT v3.0.0 |
PC or CX (x86) |
Tc2_Database |