Background Communication

Depending on the requirements, communication between the serial hardware and the ComBuffer data buffers is handled in either the standard task or a separate fast task.

A separate fast task is necessary when using high baud rates or KL6001 Terminals, which have a small process image (only 3 or 5 bytes). See also the Communication Principle.

The tutorial uses the following three hardware interfaces:

Hardware interface

Background communication

PC COM port

In the standard task

EL6001

In the separate fast task

KL6001 (3 bytes)

In the separate fast task

Data structures

Four data structures are required to address a serial interface.

There are two different types of data structures:
- Hardware-dependent structures establish the connection to the hardware in the send and receive directions.
- In addition, two data buffers are required as intermediate storage.

PC COM port:

PROGRAM MAIN
VAR
    (* I/O variables for a PC-COM port *)
    stIn_PcCom AT %I*  : PcComInData; (* linked to the port in the TwinCAT System Manager *)
    stOut_PcCom AT %Q*  : PcComOutData; (* linked to the port in the TwinCAT System Manager *)
    RxBufferPcCom       : ComBuffer; (* Receive data buffer; used with all receive function blocks *)
    TxBufferPcCom       : ComBuffer; (* Transmit data buffer; used with all receive function blocks *)
END_VAR

EL6xxx and KL6xxx:

VAR_GLOBAL
    RxBufferEL : ComBuffer; (* Receive data buffer; used with all receive function blocks *)
    TxBufferEL : ComBuffer; (* Transmit data buffer; used with all receive function blocks *)

    RxBufferKL : ComBuffer; (* Receive data buffer; used with all receive function blocks *)
    TxBufferKL : ComBuffer; (* Transmit data buffer; used with all receive function blocks *)
END_VAR
PROGRAM Background
VAR
    (* I/O variables for a EL6001 terminal*)
    stIn_EL6001 AT %I*    : EL6inData22B; (* linked to the EL6001 in the TwinCAT System Manager *)
    stOut_EL6001 AT %Q*    : EL6outData22B;(* linked to the EL6001 in the TwinCAT System Manager *)
     (* I/O variables for a KL6001 terminal*)
    stIn_KL6001 AT %I*    : KL6inData; (* linked to the KL6001 in the TwinCAT System Manager *)
    stOut_KL6001 AT %Q*   : KL6outData; (* linked to the KL6001 in the TwinCAT System Manager *)
END_VAR

SerialLineControl

The SerialLineControl function block must be called in every PLC cycle.
It communicates with the serial hardware device and transmits and receives data.
The function block can be called both from the standard task and from the separate fast task.

Called from the standard task, PC COM port:

PROGRAM MAIN
VAR
    (* background communication with the PC COM port device *)
    fbPcComCtrl       : SerialLineControl;
    bPcComCtrlError   : BOOL;
    ePcComCtrlErrorID : ComError_t;
END_VAR
(* background communication with the PC COM port device *)
fbPcComCtrl(
    Mode      := SERIALLINEMODE_PC_COM_PORT, 
    pComIn    := ADR(stIn_PcCom), 
    pComOut   := ADR(stOut_PcCom), 
    SizeComIn := SIZEOF(stIn_PcCom), 
    Error      => bPcComCtrlError, 
    ErrorID    => ePcComCtrlErrorID, 
    TxBuffer  := TxBufferPcCom, 
    RxBuffer  := RxBufferPcCom );

Called from the separate fast task, EL6xxx and KL6xxx:

PROGRAM Background
VAR
    (* background communication with the EL6001 terminal *)
    fbEL6001Ctrl       : SerialLineControl;
    bEL6001CtrlError   : BOOL;
    eEL6001CtrlErrorID : ComError_t;

    (* background communication with the KL6001 terminal (3byte) *)
    fbKL6001Ctrl         : SerialLineControl;
    bKL6001CtrlError     : BOOL;
    eKL6001CtrlErrorID   : ComError_t;
    fbKL6001Config       : KL6configuration;
    bKL6001ConfigError   : BOOL;
    eKL6001ConfigErrorID : ComError_t;
    bKL6001ConfigExe     : BOOL := TRUE;
END_VAR
(* background communication with the EL6001 terminal *)
fbEL6001Ctrl(
    Mode:= SERIALLINEMODE_EL6_22B,
    pComIn:= ADR(stIn_EL6001),
    pComOut:= ADR(stOut_EL6001),
    SizeComIn:= SIZEOF(stIn_EL6001),
    Error=> bEL6001CtrlError,
    ErrorID=> eEL6001CtrlErrorID,
    TxBuffer:= TxBufferEL,
    RxBuffer:= RxBufferEL );

(* background communication with the KL6001 terminal (3byte) *)
fbKL6001Config(
    Execute:= bKL6001ConfigExe,
    Mode:= SERIALLINEMODE_KL6_3B_ALTERNATIVE,
    Baudrate:= 9600,
    NoDatabits:= 8,
    Parity:= 0,
    Stopbits:= 1,
    Handshake:= HANDSHAKE_NONE,
    ContinousMode:= FALSE,
    pComIn:= ADR(stIn_KL6001),
    pComOut:= ADR(stOut_KL6001),
    SizeComIn:= SIZEOF(stIn_KL6001),
    Done=> ,
    Busy=> ,
    Error=> bKL6001ConfigError,
    ErrorId=> eKL6001ConfigErrorID );
IF NOT fbKL6001Config.Busy AND NOT bKL6001ConfigError THEN
    bKL6001ConfigExe := FALSE;
    fbKL6001Ctrl(
        Mode:= SERIALLINEMODE_KL6_3B_ALTERNATIVE,
        pComIn:= ADR(stIn_KL6001),
        pComOut:= ADR(stOut_KL6001),
        SizeComIn:= SIZEOF(stIn_KL6001),
        Error=> bKL6001CtrlError,
        ErrorID=> eKL6001CtrlErrorID,
        TxBuffer:= TxBufferKL,
        RxBuffer:= RxBufferKL );
END_IF