FB_IEC870_5_101Slave

FB_IEC870_5_101Slave 1:

An instance of the function block FB_IEC870_5_101Slave can be used to implement an IEC 60870-5-101 substation (slave, high-level) in the TwinCAT PLC. Normally, the data exchange starts automatically once the connection has been established. This is the default configuration of the function block.

VAR_IN_OUT

VAR_IN_OUT
    hSerial         : T_HSERIALCTRL;
END_VAR

hSerial: Connection handle to FB_IEC870_SerialLineCtrl. Via this variable the data to be send and received are exchanged with FB_IEC870_SerialLineCtrl.

VAR_INPUT

VAR_INPUT
    protPara        : ST_IEC870_5_101ProtocolParams;
    sysPara         : ST_IEC870_5_101SystemParams;
    pAOEntries      : POINTER TO ARRAY[0..IEC870_ANYSIZE_ARRAY] OF ST_IEC870_5_101AODBEntry := 0;
    cbAOEntries     : UDINT := 0;
    pInputs         : PVOID := 0;
    cbInputs        : UDINT := 0;
    pOutputs        : PVOID := 0;
    cbOutputs       : UDINT := 0;
    pMemory         : PVOID := 0;
    cbMemory        : UDINT := 0;
    pData           : PVOID := 0;
    cbData          : UDINT := 0;
    bEnable         : BOOL := TRUE;
END_VAR

protPara: IEC 60870-5-101 protocol parameters.

sysPara: System parameter.

pAOEntries: Address of the first application object database variable.

cbAOEntries: Byte size of the application object database variables.

pInputs: Address of the PLC process data area for the inputs.

cbInputs: Byte size of the PLC process data area for the inputs.

pOutputs: Address of the PLC process data area for the outputs.

cbOutputs: Byte size of the PLC process data area for the outputs.

pMemory: Address of the PLC process data area for the flags.

cbMemory: Byte size of the PLC process data area for the flags.

pData: Address of the PLC data area.

cbData: Byte size of the PLC data area.

bEnable: Activates/deactivates the function block (communication and connection).

The addresses can be determined with the ADR operator and the byte sizes with the SIZEOF operator.

VAR_OUTPUT

VAR_OUTPUT
    system      : ST_IEC870_5_101SystemInterface;
    eState      : E_IEC870_5_101SerialLinkState := eSERIALLINK_DISCONNECTED;
END_VAR

system: System interface. This variable is used by other PLC functions or function blocks as communication interface with the IEC device (here: substation).

eState: Status of the connection to the master.

Examples (high-level interface):

Further examples:

PROGRAM test
VAR
    slave1AODB      : ARRAY[1..50] OF ST_IEC870_5_101AODBEntry;

    inputs AT%IB0   : ARRAY[0..999] OF BYTE;
    outputs AT%QB0  : ARRAY[0..999] OF BYTE;
    memory AT%MB0   : ARRAY[0..999] OF BYTE;
    data            : ARRAY[0..999] OF BYTE;

    server1         : FB_IEC870_5_101Slave;

    bEnable         : BOOL;
    eState          : E_IEC870_5_101SerialLinkState;

    bError          : BOOL;
    iecError        : ST_IEC870_5_101ErrorFifoEntry;
END_VAR
server1.protPara.linkAddr := 220;
server1.protPara.eLinkMode := eIEC870_LinkMode_Unbalanced;
server1.protPara.elinkAddrSize := eIEC870_LinkAddr_TwoOctets;

server1.sysPara.asduFmt.eAsduAddrSize := eIEC870_AsduAddr_TwoOctets;
server1.sysPara.asduFmt.eObjAddrSize := eIEC870_ObjAddr_ThreeOctets;
server1.sysPara.asduFmt.eCOTSize := eIEC870_COT_TwoOctets;
server1.sysPara.asduAddr := 7;
server1.sysPara.bUsePCTime := TRUE;
server1.sysPara.bSyncTime := TRUE;
server1.sysPara.bSyncPCTime := FALSE;
server1.sysPara.bEndOfInit := TRUE;
server1.sysPara.bPerCyclic := FALSE;
server1.sysPara.tPerCyclicBase := T#5s;
server1.sysPara.bBackScan := FALSE;
server1.sysPara.tBackScanCycle := T#30s;
server1.sysPara.bPerFRZ := TRUE;
server1.sysPara.tPerFRZCycle := T#15s;
server1.sysPara.dbgMode := IEC870_DEBUGMODE_LINKLAYER;(* OR IEC870_DEBUGMODE_DEVSTATE OR IEC870_DEBUGMODE_ASDU;*)
server1.sysPara.bTimeCOT3 := FALSE;

server1( pInputs := ADR( inputs ),
    cbInputs := SIZEOF( inputs ),
    pOutputs := ADR( outputs ),
    cbOutputs := SIZEOF( outputs ),
    pMemory := ADR( memory ),
    cbMemory := SIZEOF( memory ),
    pData := ADR( data ),
    cbData := SIZEOF( data ),
    pAOEntries := ADR( slave1AODB ),
    cbAOEntries := SIZEOF( slave1AODB ),
    hSerial := P_SerialComm_HighSpeed.hSerial,
    bEnable := bEnable,
    eState=>eState );

In the following example, the device error FIFO is read cyclically, and the registered errors are written to the TwinCAT XAE->"Error Log" window.

REPEAT
    server1.system.device.errors.RemoveError( getError=>iecError, bOk=>bError );
    IF bError THEN
        ADSLOGSTR( ADSLOG_MSGTYPE_ERROR OR ADSLOG_MSGTYPE_LOG, 'IEC 60870-5-101 slave error: 0x%s',
                DWORD_TO_HEXSTR( iecError.nErrId, 8, FALSE) );
    END_IF
UNTIL NOT bError
END_REPEAT

Requirements

Development environment

Target system type

PLC libraries to be linked (category group)

TwinCAT v3.1.4012.0

PC oder CX (x86, x64, ARM)

Tc2_IEC60870_5_10x (Communication->IEC60870)
Tc2_SerialCom (Communication->Serial)
Tc2_TcpIp (Communication->TcpIp)
Tc2_Utilities (System)