Notes on the documentation

This description is only intended for the use of trained specialists in control and automation engineering who are familiar with the applicable national standards.
It is essential that the following notes and explanations are followed when installing and commissioning these components.

The responsible staff must ensure that the application or use of the products described satisfy all the requirements for safety, including all the relevant laws, regulations, guidelines and standards.

Disclaimer

The documentation has been prepared with care. The products described are, however, constantly under development.
For that reason the documentation is not in every case checked for consistency with performance data, standards or other characteristics.
In the event that it contains technical or editorial errors, we retain the right to make alterations at any time and without warning.
No claims for the modification of products that have already been supplied may be made on the basis of the data, diagrams and descriptions in this documentation.

Trademarks

Beckhoff®, TwinCAT®, EtherCAT®, Safety over EtherCAT®, TwinSAFE® and XFC® are registered trademarks of and licensed by Beckhoff Automation GmbH.
Other designations used in this publication may be trademarks whose use by third parties for their own purposes could violate the rights of the owners.

Patent Pending

The TwinCAT Technology is covered, including but not limited to the following patent applications and patents:
EP0851348, US6167425 with corresponding applications or registrations in various other countries.

Copyright

© Beckhoff Automation GmbH & Co. KG, Germany.
The reproduction, distribution and utilization of this document as well as the communication of its contents to others without express authorization are prohibited.
Offenders will be held liable for the payment of damages. All rights reserved in the event of the grant of a patent, utility model or design.


Overview

The TwinCAT PLC library COMlib supplies function blocks and data structures for serial data communication. COMlib supports the Beckhoff KL6xxx serial bus terminals.

The COMlib3964RBC communication library extends COMlib to implement an error-protected protocol for the transmission of any type of data. A checksum in combination with repetition of faulty telegrams provide the error protection.

The RK512 protocol is used to transmit data blocks such as are used in Siemens controllers. The COMlibRK512BC communication library uses the 3964R protocol for this purpose, and handles the telegram traffic that it requires. The user only has to parameterise the RK512 function block.

The "Serial Communication RK512" communication package contains all the necessary libraries. It supports the transmission of data blocks with a length of up to 128 data words.

The documentation for the PLC-Library Serial Communication BC COMlib provides a basis for understanding this communication process.


Installation

Installation is performed by copying the following library files into the TwinCAT directory TwinCAT\PLC\LIB.

  • COMlibBC5B.LB6
  • COMlib3964RBC.LB6
  • COMlibRK512BC.LB6

The associated test program should be copied to any project directory of your choice, e.g. to TwinCAT\PLC.

  • COMlibRK512BCTest.pr6


Data Block Configuration

The RK512 function block has two operating modes. In passive mode it receives data and request telegrams from a communication partner who is addressing data blocks by means of its data block number. Numbered data blocks are not known to IEC-1131, and therefore initially also not to TwinCAT. In TwinCAT, data blocks are variables of various types such as arrays or data structures (STRUCT).

In order to define a numbered data block, the method DBconfig belonging to the RK512 function block as called with all the necessary parameters during the initialisation phase. The method is called once for each data block that is to be addressed by communication partners. This configuration is not needed for the active operating mode (send and fetch).

In the active operating mode variable contents can be sent to or fetched from communication partners, independently of this data block configuration. In active mode the data block number refers to the partner device, and only needs to be known there.

Parameters

DbAdr

DbAdr is the memory address of a PLC variable that is to be defined as a data block. The address is determined using the ADR function.

e.g.: DbAdr := ADR( PLCvar );

The PLC variable can be of any type. An ARRAY OF WORD, for example, or a STRUCT data structure would be suitable.

DbSize

DbSize is the size of the PLC variable at DbAdr in bytes, determined by the SIZEOF function.

e.g.: DbSize := SIZEOF( PLCvar );

RemoteDbNr

RemoteDbNr contains the number of the data block at the communication partner.

TxBuffer

The transmit data buffer TxBuffer is of type COMbuffer. This parameter is not needed by the configuration, but must however be passed as an IN_OUT parameter.

RxBuffer

The receive data buffer RxBuffer is of type COMbuffer. This parameter is not needed by the configuration, but must however be passed as an IN_OUT parameter.

Example

VAR
    (* declare some DB
    (* the type of data doen't matter but the
    (* size shouldn't be larger than 128 bytes
    (**)
    DB1 : ARRAY[1..64] OF WORD; (* exemplary type of db *)
    DB5 : ARRAY[1..64] OF WORD; (* exemplary type of db *)
    DB10 : ARRAY[1..64] OF WORD; (* exemplary type of db *)

    (* input and ouput data for the RK512 function block *)
    RK512com : RK512;

    initialized : BOOL;
END_VAR

IF NOT initialized THEN
    RK512com.DBconfig( RemoteDbNr:=5, DbAdr:=ADR(DB5), DbSize:=SIZEOF(DB5), TxBuffer:=TxBuffer, RxBuffer:=RxBuffer );
    RK512com.DBconfig( RemoteDbNr:=10, DbAdr:=ADR(DB10), DbSize:=SIZEOF(DB10), TxBuffer:=TxBuffer, RxBuffer:=RxBuffer );
    initialized := TRUE;
END_IF

The initialisation in this example has the effect that the communication partner may read or write data blocks 5 and 10. Any attempt to access another data block is considered as an error and is refused.


RK512 Function Block

VAR_INPUT
    Send : BOOL;                       (* SEND command *)
    Fetch : BOOL;                      (* FETCH command *)
    DbAdr : POINTER TO BYTE;           (* ADR(DB), SEND=source, FETCH=target *)
    DbSize : UINT;                     (* SIZEOF(DB), size in byte for Send or Fetch *)
    RemoteDbNr :BYTE;                  (* SEND=target, FETCH=source *)
    NrOfBytes : UINT;                  (* number of bytes to send or fetch *)
    ByteOffset : UINT;                 (* byte offset in remote DB *)
    HighPriority : BOOL;               (* 3964R priority *)
    SwapWords : BOOL;                  (* swaps every two bytes in the data stream before/after transmission *)
    TREA : TIME;                       (* Timeout for answer *)
END_VAR
VAR_OUTPUT
    Busy : BOOL;                       (* FB is busy *)
    (* send or fetch (active) mode *)
    BusySendFetch : BOOL;              (* FB is with SendFetchCommand busy *)
    SendFetchSuccess : BOOL;           (* Send or Fetch successfully finished *)
    Error : BOOL;                      (* RK512 error *)
    ErrorId : INT;                     (* RK512 error id *)
    (* receive (passive) mode only *)
    BusyReceive : BOOL;                (* FB is busy with remote request *)
    ReceiveSuccess : BOOL;             (* DB has been received from the remote PLC *)
    ReceivedDbNr : BYTE;               (* Number of the DB that has been received *)
    ErrorRx : BOOL;                    (* RK512 error. For receive mode only *)
    ErrorIdRx : INT;                   (* RK512 error id. For receive mode only *)
    Aux : RK512_Auxiliary_t;           (* Additional errors for debugging *)
END_VAR
VAR_IN_OUT
    TxBuffer : ComBuffer;
    RxBuffer : ComBuffer;
END_VAR

Operating Modes

The RK512 function block distinguishes two quite different operating modes, passive and active operation.

In the passive mode the block waits for and answers data telegram from the communication partner. The communication partner can transmit the data blocks to the local controller, or can fetch data from it. In order for the RK512 function block to be able to accept data blocks or to return data that has been requested, it must previously be configured for passive operation by DBconfig.

In active mode, the RK512 function block either sends data blocks to the communication partner (SEND) or fetches them from it (FETCH).

As long as neither of the function block's Send or Fetch inputs switch it into active operation it waits for telegrams from communication partners, changing when appropriate into passive mode. The two operating modes can be mixed. In that case the RK512 function block attempts to synchronise the telegram traffic. Since mixed mode operation can introduce delays into the flow of data, it should be avoided if possible.

One instance of the RK512 function block serves just one serial interface. It is not possible for multiple instances to handle data traffic over the same serial interface at the same time.

Input and Output Parameters

Send

At a rising edge at the Send input data from the variable whose location is given by the DbAdr input is sent to the communication partner where it is saved in the data block whose number is given at RemoteDbNr. Send and fetch cannot be executed at the same time.

Fetch

With a rising edge at the Fetch input, data from the data block specified by RemoteDbNr is requested from the communication partner and saved in the variable specified by the DbAdr input. Send and fetch cannot be executed at the same time.

DbAdr

DbAdr is the memory address of a PLC variable determined by the ADR function.

e.g.: DbAdr := ADR( PLCvar );

The PLC variable can be of any type. An ARRAY OF WORD, for example, or a STRUCT data structure would be suitable.

Data from this variable is transmitted to the communication partner with a send command, and is fetched and stored in this variable with a fetch command.

DbSize

DbSize is the size of the PLC variable at DbAdr in bytes, determined by the SIZEOF function.

e.g.: DbSize := SIZEOF( PLCvar );

RemoteDbNr

RemoteDbNr contains the number of the data block at the communication partner to which the data is to be sent or from which it is to be fetched.

NrOfBytes

NrOfBytes contains the number of data bytes to be transferred. The number can be less than or equal to the variable size DbSize.

ByteOffset

The ByteOffset indicated the data byte within the communication partner's data block from where the data is to be fetched or where it is to be stored.

HighPriority

The priority is relevant to the 3964R protocol. The two communication partners should have different priorities. If it happens that the two partners transmit at the same time, there is a collision. This collision is resolved by the partner with the lower priority switching to receive mode, allowing the higher priority partner to transmit again.

SwapWords

Data blocks are usually organised by words. Depending on how the data words are stored in the memory, it may be necessary to swap data bytes within the transmitted words. If SwapWords is TRUE, the data bytes of each word in all the data telegrams are swapped.

TREA

Every telegram from a communication partner is acknowledged by an answer telegram. The time required for this acknowledgement is monitored with respect to the TREA timeout period. TREA might be about 15 seconds.

Busy

Busy becomes TRUE as soon as the block switches into either active or passive mode, i.e. as soon as it leaves the idle state. It cannot accept a new command as long as Busy remains TRUE.

BusySendFetch

BusySendFetch becomes TRUE as soon as a rising edge at the Send or Fetch input switches the block into active mode. Once BusySendFetch has become FALSE the transmission has been completed, and either the SendFetchSuccess or the Error output is set.

SendFetchSuccess

SendFetchSuccess indicates that a data transfer triggered by Send or Fetch has been successfully completed.

Error

Error becomes TRUE if an error occurs during a data transfer triggered by Send or Fetch.

ErrorId

ErrorId provides an error number if an error occurs.

BusyReceive

BusyReceive indicates that the RK512 function block is in receive mode, i.e. in the passive operating mode. The block changes automatically into passive mode from the idle state (Busy is FALSE) as soon as a telegram is received from the communication partner. As BusyReceive falls, either ReceiveSuccess or ErrorRx is set.

ReceiveSuccess

ReceiveSuccess is set to TRUE after a data block has been successfully received from the communication partner. This signal is only relevant to passive operation. In other words, ReceiveSuccess is not set to TRUE if a data block has been actively fetched from the communication partner with Fetch.

ReceivedDbNr

As soon as ReceiveSuccess becomes TRUE, ReceivedDbNr indicates the number of the data block that has been received.

ErrorRx

The ErrorRx signal indicates that an error has occurred during data reception in passive mode.

ErrorIdRx

If an error occurs in passive mode, ErrorIdRx indicates the error number.

Aux

Aux is a data structure containing additional error messages for diagnostic purposes.

TxBuffer

The transmit data buffer TxBuffer is of type COMbuffer. This buffer is used by the RK512 function block, and is not changed by the user.

RxBuffer

The receive data buffer RxBuffer is of type COMbuffer. This buffer is used by the RK512 function block, and is not changed by the user.


Error Codes

No | No. (hex) | Error constant | Description
4 16#04 RK512ERR_P3964ERR_ZVZ The character delay time was exceeded. ZVZ = 220 ms.

The character delay time is the maximum time interval between the transmission of two succeeding characters.
5 16#05 RK512ERR_P3964ERR_QVZ The acknowledgement delay time was exceeded.

The other end sent an acknowledgement character (DLE = 10H) twice as the telegram was being handled. QVZ = 2 s.

The first acknowledgement was expected at the start of the transmission. The output ErrorState when an error initially occurs is "Wait_DLE_TXstart". The cause might lie with a faulty physical connection or an incorrect interface parameter.

The second acknowledgement is expected after the user data has been transmitted. If an error occurs, the ErrorState is "Wait_DLE_TXend". In this case the cause might, for instance, be data loss, data corruption or data bytes of a data word that have become swapped (see the SwapWords input).
6 16#06 RK512ERR_P3964ERR_WVZ The repeat delay time was exceeded. WVZ = 4 s.

A telegram is repeated if an error occurs. If the telegram is not repeated by the other device, the 3964R function block reports this error.
7 16#07 RK512ERR_P3964ERR_WRONGBCC Checksum error during data reception.

Each telegram is provided with a checksum. When the data is received, the calculated checksum is compared with the checksum that has been received.
9 | 16#09 | RK512ERR_P3964ERR_COMERRTX | Interface error when transmitting.
10 | 16#0A | RK512ERR_P3964ERR_COMERRRX | Interface error when receiving.
11 | 16#0B | RK512ERR_P3964ERR_NOTXDATA | Parameterisation error. The number of bytes that are to be sent, TxCount, is zero.
120 16#78 RK512ERR_P3964ERR_NAK A telegram was refused by the other device with a negative acknowledgement (NAK).

This error can occur in the following transmission states (ErrorState), amongst others:

Wait_DLE_TXstart: The local PLC begins a telegram with a start character. The other device refuses the telegram with NAK.

Wait_DLE_TXend: The other device refuses the telegram with NAK after the user data has been transmitted. In this case the cause can, for example, be a checksum error at the other device.
12 16#0C RK512ERR_INVALIDDATATYPE Invalid data type

A telegram has been received whose data type identifier is not supported. Only data blocks with the identifier 'D' can be handled.
16 16#10 RK512ERR_ERRORPROTOCOLHEADER Faulty protocol header

The header of a data telegram is not in accordance with the RK512 specification.
20 16#14 RK512ERR_DBNOTAVAILIBLE Data block not available

A data block that is not available has been transmitted or requested. Data blocks that are accessed by the partner device must first be registered for this purpose with the DBconfig method.
22 16#16 RK512ERR_INVALIDCOMMAND Invalid command

A telegram with an invalid command identifier (SEND / FETCH) has been received.
52 16#34 RK512ERR_INVALIDSIZE Invalid length quoted

Either the DbSize or the NrOfBytes parameter is invalid, or a data telegram that is too long has been received.
53 16#35 RK512ERR_INVALIDDBADR Invalid data block address

The data block address DbAdr is invalid.
54 16#36 RK512ERR_SYNCERROR Synchronisation error

A synchronisation error can occur if both communication partners start to transmit at the same time.
257 16#101 RK512ERR_TIMEOUT Timeout on the RK512 telegram level

An expected reaction telegram has not been received within the waiting time TREA.
258 16#102 RK512ERR_ERRORREMOTEREATEL Error in the reaction telegram

An error was reported in the communication partner's reaction telegram. The error number can be read from the additional error information in AUX.
259 | 16#103 | RK512ERR_INVALIDSIZEREMOTEREATEL | The communication partner's reaction telegram has an incorrect length.
260 16#104 RK512ERR_TIMEOUTREAPEATSENDFETCH Timeout when transmitting

In spite of a number of repetitions, a send or fetch telegram could not be transmitted.
261 16#105 RK512ERR_3964ERROR Error on the 3964R protocol level

Predictable errors on this level are not reported as a general RK512ERR_3964ERROR error, but with a detailed error number.
262 | 16#106 | RK512ERR_3964NOTBUSYNOTREADY | The transmission of a telegram has been halted without having been successfully completed.


Link Libraries

Create a new PLC project with TwinCAT PLC Control in order to perform the library linking.

Go to Library Management and add the libraries ComLibBC5B.LIB, ComLib3964RBC.LIB and ComLibRK512BC.LIB.


Global Variables

Four global data structures are needed to access a serial interface. Two provide the connection to the hardware in the send and receive directions. Two data buffers are also necessary for intermediate storage.

The data structures of type PcComInData or PcComOutData are linked with the hardware in the TwinCAT System Manager. Please read the corresponding section in the COMlib documentation in this regard.


Background Communication

Communication between the serial hardware and the data buffer, which is of type ComBuffer, is handled by a separate function block. The general principles of this subject are described in the documentation for the COMlib communications library.


RK512 Protocol

The example program defines three WORD arrays, in order to use them as data blocks. During initialisation the data blocks 5 and 10 are registered for access by the communication partners by calling the DBconfig method. The function block RK512 behaves passively, and is ready to accept and to answer data telegrams from the partner device. The test program allows active sending or fetching of data to be initiated by writing to the Send or Fetch variables.