Background Communication

Communication between the serial hardware and the data buffer, whose type is ComBuffer, is handled in the standard task or in a separate fast task depending on the hardware specifications.

A fast separate task will be necessary at high baud rates or with KL6001 terminals which have a small process image (3 or 5 data bytes only). See also the Communication Principle.

The tutorial uses the following three hardware interfaces:

hardware interface

background communication

Pc COM port

in standard task

EL6001

in seperate fast task

KL6001 (3 bytes)

in seperate fast task

data strutures

Four data structures are needed to access a serial interface.

We distinguish between two different kinds of data structures:
- Hardware dependent structures provide the connection to the hardware in the send and receive directions.
- Two data buffers are also necessary for 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 und 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 each PLC cycle.
It communicates with the serial hardware device and sends and receives data.
The block can be called from the standard task or 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