FB_DBRecordArraySelect

FB_DBRecordArraySelect 1:

The FB_DBRecordArraySelect allows some individual data records to be read from a database. The length of the SQL-command could be till 10000 Symbols.

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          : DWORD;
    bExecute           : BOOL;
    tTimeout           : TIME;
END_VAR

sNetID                              : Is a string containing the AMS network identifier of the target device to which the ADS command is directed.

hDBID                               : Indicates the ID of the database to be used.

cbCmdSize                     : Indicates the size of the SELECT command.

pCmdAddr                      : Pointer to the executed SELECT command.

nStartIndex                    : Gives the index of the first data record that is to be read.

nRecordCount               : Gives the count of data records that are to be read.

cbRecordArraySize     : Provides the size of an arraystructure  in bytes.

pDestAddr                      : Provides the address of the arraystructure into which the data records are to be written.

bExecute                        : The command is executed with the rising edge.

tTimeout                         : States 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 if "bBusy" remains TRUE.

bError          : Becomes TRUE, as soon as an error occurs.

nErrID          : Supplies the ADS Error Code or the TcDatabaseSrv_Error_Codes when the bError output is set.

sSQLState  : Supplies the SQL error code of the specified database type.

nRecords    : Returns the number of data records.

Example in ST:

Because the table out of which a data record is to be read has the following structure...

Column name

Data type

ID

bigint

Timestamp

datetime

Name

nvarchar(80

Value

float

... a PLC structure must be created having a comparable structure.

TYPE ST_Record :
STRUCT
    ID         : T_ULARGE_INTEGER;
    Timestamp  : DT;
    Name       : STRING(80);
    VALUE      : LREAL;
END_STRUCT
END_TYPE

To get the data type T_ULARGE_INTEGER, you have to add the library TcUtilities.lib to the PLC-Program

For ARM - processors the order of the data types is different and you have to add a "Dummy-BYTE" to the struct because of the different byte alignment at ARM - processors.

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 v2.10.0

PC or CX (x86)

TcDatabase.Lib

TwinCAT v2.10.0

CX (ARM)