FB_IEC870_PartyLineCtrl

From product version: TwinCAT PLC Library IEC870-5-101 Substation v2.0.2 and higher.

FB_IEC870_PartyLineCtrl 1:

The function block FB_IEC870_PartyLineCtrl handles the data exchange to the controlled station in partyline mode.

The function block must be called cyclically in the PLC task. The eMode input activates/deactivates the partyline mode.Partyline mode is not used if you do not use the function block (default setting). The deviceID input variable must match the device ID from the TwinCAT System Manager IO configuration (general COM port tab). It is only required if you use the serial PC interface for communication in partyline mode.
The hSerial variable is a structure and is used for internal data exchange between the fast and slow communication task. Every time the IEC slave wants to transmit, the bRTS output (request to send) is first set to TRUE. Once transmit mode has been switched on the IEC slave is notified by setting the bCTS input (clear to send) to TRUE. The IEC slave then starts transmitting. Once the data have been sent (internal hardware buffers are empty) the IEC slave resets the bRTS output to FALSE. The send line can now be released for another device. Once this has occurred the IEC slave must be notified by setting the bCTS input to FALSE. In other words, the state of the bCTS input always follows the state of the bRTS output.
In the serial PC interface the bRTS output is only set to FALSE (data sent) when the ADS query of the internal hardware send buffer returns zero bytes in the buffer.
 

VAR_IN_OUT

VAR_IN_OUT
    hSerial         : T_HSERIALCTRL;
ND_VAR

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

 

VAR_INPUT

VAR_INPUT
    eMode       : E_IEC870_5_101PartylineMode := eIEC870_PartylineMode_Off; (* Partyline modes (On/Off)*)
    deviceID    : UDINT := 0; (*Used by SERIALLINEMODE_PC_COM_PORT only. DeviceId specifies the device on which the function is to be executed.
    The device Ids are specified by the TwinCAT System Manager during the hardware configuration.*)
    bCTS        : BOOL := FALSE; (* Clear to send *)
END_VAR

eMode : Partyline activation mode.

deviceID : TwinCAT System Manager device ID. This parameter is only needed for communication via the serial PC interface. If activation mode is set to eIEC870_PartylineMode_Ext_On the deviceID parameter is not used.

bCTS : Clear to send (for link layer).

VAR_OUTPUT

VAR_IN_OUT
    bError      : BOOL;
    nErrID      : UDINT;
    bRTS        : BOOL := FALSE; (* Request to send *)
ND_VAR

bError : This output is switched to TRUE if an error occurs.

nErrID: Returns an error code if the bError output is set.

bRTS: Request to send (from link layer).

 

Example for TwinCAT v2.10 Build <1313 and older (CE image < v2.16 or older):

Implementation of partyline in the fast communication task: The connection for operation mode is switched ON / OFF via the RTS_SWITCH variable.

The tRTS_DEALY_ON delay time assures that the connection of the cord of IEC-Slave has been finished.  The tRTS_DELAY_OFF delay time assures, that the last sent data byte was received by the controlled station.

FB_IEC870_PartyLineCtrl 2:
PROGRAM P_SerialComm_HighSpeed
VAR
    fbSerialLineCtrl    : FB_IEC870_SerialLineCtrl;
    Mode        : ComSerialLineMode_t := SERIALLINEMODE_PC_COM_PORT; (* SERIALLINEMODE_KL6_5B_STANDARD *)

    serial_in AT%IB0   : PcComInData;
    serial_out AT%QB0  : PcComOutData;
    KL6_in AT%IB100    : KL6inData5B;
    KL6_out AT%QB100   : KL6outData5B;

    hSerial         : T_HSERIALCTRL;


    fbPartyLineCtrl     : FB_IEC870_PartyLineCtrl;
    delay       : TON;
    tRTS_DEALY_ON   : TIME := T#100ms;
    tRTS_DELAY_OFF  : TIME := T#100ms;
    RTS_SWITCH AT%QX200.0 : BOOL; (* RTS line switch *)

END_VAR

 

fbSerialLineCtrl( Mode := Mode,
        Baudrate := 19200,
        NoDatabits := 8,
        Parity := PARITY_EVEN,
        Stopbits := 1,
        Handshake := HANDSHAKE_NONE,
        ContinousMode := FALSE,
        pComIn := SEL( Mode = SERIALLINEMODE_KL6_5B_STANDARD, ADR( serial_in ), ADR( KL6_in ) ),
        pComOut := SEL( Mode = SERIALLINEMODE_KL6_5B_STANDARD, ADR( serial_out ), ADR( KL6_out ) ),
        SizeComIn := SEL( Mode = SERIALLINEMODE_KL6_5B_STANDARD, SIZEOF( serial_in ), SIZEOF( KL6_in ) ),
        hSerial := hSerial );


(* The deviceID may vary! *)
fbPartyLineCtrl( eMode:= eIEC870_PartylineMode_On, hSerial:= hSerial, deviceID := 1 );
IF fbPartyLineCtrl.bRTS <> fbPartyLineCtrl.bCTS THEN
    IF fbPartyLineCtrl.bRTS THEN
        RTS_SWITCH := TRUE; (* switch RTS line ON *)
        delay( in := TRUE, PT := tRTS_DEALY_ON ); (* wait until line enabled *)
        IF delay.Q THEN
            delay( in := FALSE );
            fbPartyLineCtrl.bCTS := TRUE; (* set clear to send *)
        END_IF
    ELSE
        delay( in := TRUE, PT := tRTS_DELAY_OFF ); (* wait until all data send *)
        IF delay.Q THEN
            delay( in := FALSE );
            RTS_SWITCH := FALSE; (* switch RTS line OFF *)
            fbPartyLineCtrl.bCTS := FALSE; (* reset clear to send *)
        END_IF
    END_IF
END_IF

 

Example for TwinCAT v2.10 Build >= 1313 or newer (CE image v2.16 or newer):

In an newer TwinCAT Version, the RTS line can be mapped directly as IO output in the PLC. To do this, you have to enable the Extended Ctrl/Status option in the TwinCAT System Manager. The  eMode parameter at the functionsblock has to be set to eMode := eIEC870_PartylineMode_Ext_On . The deviceID parameter is not used in this modus and has not to be configurated.

FB_IEC870_PartyLineCtrl 3:

 

The RTS_SWITCH variable has to be linked with Bit 1 in ExtCtrl (RTS output ) and TX_BUFFER_EMPTY variable has to be linked with Bit 15 in ExtStatus.

 

FB_IEC870_PartyLineCtrl 4:
FB_IEC870_PartyLineCtrl 5:

 

PROGRAM P_SerialComm_HighSpeed
VAR
    fbSerialLineCtrl    : FB_IEC870_SerialLineCtrl;
    Mode        : ComSerialLineMode_t := SERIALLINEMODE_PC_COM_PORT; (* SERIALLINEMODE_KL6_5B_STANDARD *)

    serial_in AT%IB0   : PcComInData;
    serial_out AT%QB0  : PcComOutData;
    KL6_in AT%IB100    : KL6inData5B;
    KL6_out AT%QB100   : KL6outData5B;

    hSerial         : T_HSERIALCTRL;
    fbPartyLineCtrl     : FB_IEC870_PartyLineCtrl;
    delay       : TON;
    tRTS_DEALY_ON   : TIME := T#100ms;
    tRTS_DELAY_OFF  : TIME := T#100ms;
    RTS_SWITCH AT%QX200.0 : BOOL; (* RTS line switch *)
    TX_BUFFER_EMPTY AT%IX200.0 : BOOL; (* UART's tx buffer is empty *)
END_VAR
fbSerialLineCtrl( Mode := Mode,
        Baudrate := 19200,
        NoDatabits := 8,
        Parity := PARITY_EVEN,
        Stopbits := 1,
        Handshake := HANDSHAKE_NONE,
        ContinousMode := FALSE,
        pComIn := SEL( Mode = SERIALLINEMODE_KL6_5B_STANDARD, ADR( serial_in ), ADR( KL6_in ) ),
        pComOut := SEL( Mode = SERIALLINEMODE_KL6_5B_STANDARD, ADR( serial_out ), ADR( KL6_out ) ),
        SizeComIn := SEL( Mode = SERIALLINEMODE_KL6_5B_STANDARD, SIZEOF( serial_in ), SIZEOF( KL6_in ) ),
        hSerial := hSerial );



fbPartyLineCtrl( hSerial:= hSerial, eMode := eIEC870_PartylineMode_Ext_On );
IF fbPartyLineCtrl.bRTS <> fbPartyLineCtrl.bCTS THEN
    IF fbPartyLineCtrl.bRTS THEN
        RTS_SWITCH := TRUE; (* switch RTS line ON *)
        delay( in := TRUE, PT := tRTS_DEALY_ON ); (* wait until line enabled *)
     IF delay.Q THEN
            delay( in := FALSE );
            fbPartyLineCtrl.bCTS := TRUE; (* set clear to send *)
        END_IF
    ELSE
        IF TX_BUFFER_EMPTY THEN
            delay( in := TRUE, PT := tRTS_DELAY_OFF ); (* wait until all data send *)
            IF delay.Q THEN
                delay( in := FALSE );
                RTS_SWITCH := FALSE; (* switch RTS line OFF *)
                fbPartyLineCtrl.bCTS := FALSE; (* reset clear to send *)
            END_IF
        END_IF
    END_IF
END_IF

 

Requirements

Development Environment

Target System

PLC libraries to include

TwinCAT v2.9.0 Build >= 1030

PC or CX (x86)

TcIEC870_5_101Link.Lib

( Standard.Lib; TcBase.Lib; TcSystem.Lib; TcUtilities.Lib; TcIEC870_5_101.Lib; COMlibV2.Lib; are included automatically )

TwinCAT v2.10.0 Build >= 1301

CX (ARM)