SerialLineControlADS

SerialLineControlADS 1:

The SerialLineControlADS function block looks after the communication between a virtual serial interface and the PLC. The function block is called cyclically, and places received data into the RxBuffer. Data made available in the TxBuffer transmit buffer is sent to the interface at the same time.

Because the function operates independently of the application, it is referred to as background communication and can, like the function block SerialLineControl, also operate in a fast task (see Communication concept and Supported hardware).

As soon as the function block is called cyclically and the input Connect is set, the parameterized serial COM port is opened automatically.
Thereby this COM port is blocked for other applications. Do you want to unblock the COM port to get access with another application, you can reset the input Connect. Thereby the current port will be closed. Is the COM port number or another parameter in the input structure SerialCfg changed, the previous port will get closed automatically and after it the new port will get opened.

Interface

FUNCTION_BLOCK SerialLineControlADS
VAR_INPUT
    Connect     :BOOL;              (* connect to serial port [TRUE=connect, FALSE=disconnect] *)
    SerialCfg   :ComSerialConfig;
    NetId       :T_AmsNetId :='';               (* host NetId *)
    Timeout     :TIME :=DEFAULT_ADS_TIMEOUT;    (* Timeout for ADS calls *)
END_VAR
VAR_IN_OUT
    TxBuffer    :ComBuffer;       (* serial Tx ComBuffer *)
    RxBuffer    :ComBuffer;       (* serial Rx ComBuffer *)
END_VAR
VAR_OUTPUT
    PortOpened  :BOOL;          (* Indicates if selected serial port is opened *)
    Error       :BOOL;          (* 'TRUE' if an error occurred *)
    ErrorID     :UDINT;         (* Displays the error code; 0 = no error *)
    Busy        :BOOL;          (* 'TRUE' if internal ADS communication is busy *)
    TxBufCount  :UDINT;         (* number of bytes in internal Tx buffer *)
    RxBufCount  :UDINT;         (* number of bytes in internal Rx buffer *)
END_VAR

Input variables

Connect

To open a connection to a serial port, Connect has to be TRUE. Is Connect FALSE specified, an open port will get closed.

Note: If this input variable switches, it can take up to 6 times of the Timeout delay to finish the requested action. Therefore the application has to look after the output PortOpened and wait until it takes the desired state.

SerialCfg

This input structure defines a COM port and with which parameters the COM port is used and opened. For details see the description of ComSerialConfig.

NetId

For local access don't specify this input or allocate an empty string. For remote access to another TwinCAT target system specify its AMS Net Id.

Timeout

Specifies a maximum length of time for the execution of the function block. The default value is 5 seconds. (At least 1000ms should be adjusted.)

Input/output variables

TxBuffer

Buffer with transmit data for the serial hardware being used. The transmit buffer is filled by functions such as SendByte, SendData or SendString.

RxBuffer

Buffer into which received data is placed. The receive buffer is read by functions such as ReceiveByte, ReceiveData or ReceiveString.

Output variables

PortOpened

Indicates if the selected serial port is opened and connected.

Error

The Error output becomes TRUE as soon as an error occurs.

ErrorID

The ErrorID output provides an error code if an error occurs. A list of possible values as well as hints for fault correction are located in the chapter Error codes.

Busy

This output is TRUE as long as the internal ADS communication of the function block is active.

TxBufCount

The TxBufCount output indicates that some data bytes exist in the internal PLC buffer which are not yet sent.

RxBufCount

The RxBufCount output indicates that some received data bytes exist in the internal PLC buffer which are not yet transferred to the RxBuffer.
It has to be ensured by the application that the received bytes are collected out off the RxBuffer fast enough.

PLC libraries to be linked

COMlibV2.lib [version 2.003.008 or higher]