FB_DBRecordArraySelect
The function block FB_DBRecordArraySelect can be used to read several records with any structure from the database. This function block can be used to execute an SQL SELECT command with up to 10,000 characters.
This function block is not compatible with ASCII files.
VAR_INPUT
VAR_INPUT
sNetID : T_AmsNetID;
hDBID : UDINT;
cbCmdSize : UDINT;
pCmdAddr : UDINT;
nStartIndex : UDINT;
nRecordCount : UDINT;
cbRecordArraySize: UDINT;
pDestAddr : POINTER TO BYTE;
bExecute : BOOL;
tTimeout : TIME;
END_VAR
sNetID: String containing the AMS network ID of the target device, at which the ADS command is directed.
hDBID: Indicates the ID of the database to be used.
cbCmdSize: Indicates the length of a SELECT command to be executed.
pCmdSize: Indicates the pointer address of a string variable with the SQL command to be executed.
nStartIndex: Indicates the index of the first record to be read.
nRecordCount: Indicates the number of records to be read.
cbRecordArraySize: Indicates the size of the structure array in bytes.
pDestAddr: Indicates the address of the structure array into which the records are to be written.
bExecute: The command is executed with a rising edge.
tTimeout: Indicates the time before the function is cancelled.
VAR_OUTPUT
VAR_OUTPUT
bBusy : BOOL;
bError : BOOL;
nErrID : UDINT;
sSQLState: ST_DBSQLError;
nRecords : UDINT;
END_VAR
bBusy: The command is in the process of being transmitted by ADS. No new command will be accepted as long as bBusy remains TRUE.
bError: Becomes TRUE, as soon as an error occurs.
nErrID: Returns the ADS error code or TcDatabaseSrv_Error_Codes if the bError output is set.
sSQLState: Returns the SQL error code of the corresponding database type
nRecords: Returns the number of data records.
Sample in ST
Since the table, from which the records are to be read, has the structure below, a PLC structure with a similar structure must be created.
Table:
Column name |
Data type |
---|---|
ID |
Bigint |
Timestamp |
datetime |
Name |
nvarchar(80) |
Value |
float |
Structure:
TYPE ST_Record:
STRUCT
ID : T_ULARGE_INTEGER;
Timestamp: DT;
Name : STRING(80);
VALUE : LREAL;
END_STRUCT
END_TYPE
The library TcUtilities.lib must be integrated in order to be able to use the data type T_ULARGE_INTEGER.
For ARM processors the data types have to be arranged differently due to the byte alignment, and a "dummy byte" has to be added.
TYPE ST_Record :
STRUCT
ID : T_ULARGE_INTEGER;
Timestamp: DT;
Value : LREAL;
Name : STRING(80);
Dummy : BYTE;
END_STRUCT
END_TYPE
PROGRAM MAIN
VAR
FB_DBRecordArraySelect1 : FB_DBRecordArraySelect;
cmd : T_Maxstring := 'SELECT * FROM myTable';
(* Unter ARM*)
(*cmd : T_Maxstring := 'SELECT ID,Timestamp,Value,Name FROM myTable'*)
(*----------*)
recordArray : ARRAY [1..5] OF ST_Record;
busy : BOOL;
err : BOOL;
errid : UDINT;
sqlstate : ST_DBSQLError;
recAnz : UDINT;
END_VAR
PLC program
FB_DBRecordArraySelect1(
sNetID:= ,
hDBID:= 1,
cbCmdSize:= SIZEOF(cmd),
pCmdAddr:= ADR(cmd),
nStartIndex:= 0,
nRecordCount:= 5,
cbRecordArraySize:= SIZEOF(recordArray),
pDestAddr:= ADR(recordArray),
bExecute:= TRUE,
tTimeout:= T#15s,
bBusy=> busy,
bError=> err,
nErrID=> errid,
sSQLState=> sqlstate,
nRecords=> recAnz);
Requirements
Development environment |
Target system type |
PLC libraries to be linked |
---|---|---|
TwinCAT v3.0.0 |
PC or CX (x86) |
Tc2_Database |