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 serial Beckhoff KL6xxx bus terminal and the standard PC COMx interfaces.

In addition to the basic functions of sending and receiving transparent data, COMlib3964R supports the 3964R protocol. The COMlib and COMlib3964R libraries thus replace the previous P3964lib library. Both libraries are included in the 3964R package.

The COMlib library is described in the document “COMlib PLC Library – Serial Data Communication”. The present document is to be seen as a supplement to that.


Installation

In order to perform installation following libraries are copied into the TwinCAT directory TwinCAT\PLC\LIB.

  • COMlib.LIB
  • ChrAsc.LIB
  • ChrAsc.OBJ
  • COMlib3964R.LIB

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

  • COMlib3964Test.pro
  • COMlib3964Test.wsm


Data structure P3694buffer

Data buffers of type P3964buffer are used in association with the P3964R function block. These buffers are written and read by the user.

TYPE P3964buffer
STRUCT
        D : ARRAY [0..16#0FFF] OF BYTE;
END_STRUCT
END_TYPE


P3964R Function Block

VAR_INPUT
      TXstart: BOOL;     (* Start signal, edge triggered *)
      RXstart: BOOL;     (* Start signal, edge triggered *)
      Priority : BOOL;   (* 3964 priority *)
      TXcount: INT;      (* Number of data bytes in TxData *)
END_VAR
VAR_OUTPUT
      Busy: BOOL;        (* Block active *)
      TXbusy : BOOL;     (* Sending now *)
      TXready: BOOL;     (* Sending finished *)
      RXbusy : BOOL;     (* Receiving now *)
      RXready: BOOL;     (* Receiving finished *)
      RXcount: INT;      (* Number of data bytes in TxData *)
      Error : BOOL;      (* Error *)
      ErrorID: INT;      (* Error number *)
      ErrorState : STRING(80); (* Internal error state *)
END_VAR
VAR_IN_OUT
      TXbuffer: COMbuffer;  (* Intermediate buffer for hardware *)
      RXbuffer: COMbuffer;  (* Intermediate buffer for hardware *)
      TXdata : P3964buffer; (* Application data (to be sent) *)
      RXdata : P3964buffer; (*Application data (to be received) *)
END_VAR

Connection to Hardware

The P3964R function block handles the 3964R protocol. The background communication, which is handled by a separate block (PcComControl, KL6Control and KL6Control5B) decides on the hardware interface used. The associated data buffers, Txbuffer and Rxbuffer, are passed to the P3964R block.

Sending

Send data are entered into the Txdata send data buffer by the PLC. The number of data bytes entered is passed on in Txcount, and then Txstart is set to TRUE. Txstart is edge-triggered, so that before a new attempt to send is made, the block must be called with Txstart=FALSE. While sending, Txbusy=TRUE. Once the data has been successfully transferred, Txready changes to TRUE.

Receiving

RxStart is set to TRUE to receive data. After a complete data set has been received, the Rxready output goes TRUE and Rxcount data bytes are ready in the receive data buffer Rxdata. Rxstart is also edge-triggered. While receiving, Rxbusy=TRUE.

Interrupt reception

The P3964R block can be used in either send or receive mode. It is worthwhile switching the block to receive when transmission is not taking place. As long as the block is in receive mode, but no start character has been received from the other end, a rising edge at Txstart can interrupt the reception and initiate the sending of data. After the transfer the block goes into its initial state (Busy=FALSE), and is only switched over to receive by a new edge at Rxstart.

Priority

In a 3964R data connection, either end may send at any time. If both ends attempt to send at the same time, there is a collision. In that case, the end with the lower priority goes into receive mode, while the end with the higher priority sends again. For this reason, when priority is low the RxReady output can be TRUE, even though data are to be sent. The data in RxData may then require evaluation. The setting of the priority at the two ends is to be agreed.

Error

In the event of an error the block goes into its initial state (Busy=FALSE), and the Error output goes TRUE. The ErrorId returns the error code. ErrorState provides information about the state in which the error occurred when the internal protocol structure is known.


Error Codes

No | Error constant | Description
4 P3964ERR_ZVZ The character delay time was exceeded. CDT = 220 ms.

The character delay time is the maximum time interval between the transmission of two succeeding characters.
5 P3964ERR_QVZ The acknowledgement delay time was exceeded.

The other end sent an acknowledgement character (DLE = 10H) twice as the telegram was being handled. ADT = 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 P3964ERR_WVZ The repeat delay time was exceeded. RDT = 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 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 | P3964ERR_COMERRTX | Interface error when transmitting
10 | P3964ERR_COMERRRX | Interface error when receiving
11 | P3964ERR_NOTXDATA | Parameterisation error. The number of bytes that are to be sent, TxCount, is zero.
20 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.


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 ChrAsc.LIB, ComLib.LIB and ComLib3964R.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 KL6inData or KL6outData are linked with the hardware in the TwinCAT System Manager. Please read the corresponding section in the COMlib documentation in this regard.


Task Configuration

The speed of the serial interfaces must be considered in the task configuration. For example, in order that, with 9600 bps at the serial bus terminal, the data can all actually be processed at this speed, the associated communication block must be active at least once per millisecond. The task that operates the block must have a correspondingly fast setting. The simplest case is when the entire PLC program runs in this fast task. If the task is set slower, then as long as the interface operates with a hardware handshake, the communication will function at reduced speed. Without handshake, data to be received can be lost.


Background Communication

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


3964R Protocol

The example program sends, for the purposes of demonstration, data to machine 1 every 10 seconds, and receives data constantly from machine 2