Declaring and calling an instance of the IEC60870-5-104 substation

Here you can unpack the complete PLC sources: TutorialSample.zip

The complete functionality of a substation is encapsulated in the FB_IEC870_5_104Slave function block. An instance can be used to establish a connection to the master. For establishing a further connection a further instance can be declared, and the same server handle (hServer variable) can be transferred to this second instance or use the FB_IEC870_5-104SlaveGrp function block (preferred!). You have change the host address (IP address) to the address of your target system.

Add the following PLC code to the declaration part of MAIN:

PROGRAM MAIN
VAR
    AODB        : ARRAY[0..49] OF ST_IEC870_5_101AODBEntry;

    init        : BOOL := TRUE;
    initError           : UDINT;

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


     bEnable         : BOOL := TRUE;
    hServer         : T_HSERVER;
    server      : FB_IEC870_5_104Slave;
END_VAR

and the instance is called in the program part:

IF init THEN
    init := FALSE;
...
F_CreateServerHnd( '', '127.0.0.1'(* change this! *), 2404, nMode := LISTEN_MODE_CLOSEALL OR CONNECT_MODE_ENABLEDBG, bEnable, hServer );
ELSE

    server( 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( AODB ),
        cbAOEntries := SIZEOF( AODB ),
        hServer := hServer,
        bEnable := bEnable );
END_IF