Sample of iDTRONIC RFID reader in TwinCAT

This sample program enables the use of an iDTRONIC RFID reader in the TwinCAT PLC.

Connection takes place by means of USB and the corresponding driver creates a virtual COM port. This port is displayed in the Windows Device Manager and the assigned port number can be determined and if necessary changed.

Sample of iDTRONIC RFID reader in TwinCAT 1:

Further requirements for the use of the TwinCAT sample program:

Download TwinCAT 3 project: TC3_IdTronicRfid_Sample

The complete application is located in the program block IdTronic_Communication(). The background communication is handled by an instance of the function block SerialLineControlADS from the Tc2_SerialCom library. Various command messages can be transmitted to the reader after parameterizing the structure stSendMessage:ST_IdTronic_CommandMessage. Messages are received and made available in the structure stRcvMessage:ST_IdTronic_ReplyMessage for further processing.

The reader sends only on request. It is a bidirectional communication. No transponder cards are automatically detected and this is transmitted. Various commands are available for this purpose.

The correct COM port must be parameterized in order to ensure the background communication with the RFID reader. Details for the serial communication are documented in TF6340 Serial Communication.

stSerialCfg : ComSerialConfig := ( ComPort:= 7, 
                                   Baudrate:= 9600, 
                                   Parity:= ComParity_t.PARITY_NONE, 
                                   DataBits:= 8, 
                                   StopBits:= ComStopBits_t.STOPBITS_ONE );

A correctly opened port is signaled via the variable bPortOpened. Communication with the RFID reader can commence if this variable is TRUE. The function blocks FB_IdTronic_Rfid_RcvMessage and FB_IdTronic_Rfid_SendMessage are used for this; they are connected to the background communication via the buffer instances RxBuffer and TxBuffer. These two function blocks offer reading and writing options and are not adapted themselves. They are already generally implemented such that an individual application can be created solely by means of parameterization within the program block IdTronic_Communication.

A choice of over 40 commands is available with the enumeration E_IdTronic_Command. For example, E_IdTronic_Command.MF_GetSRN to query the serial number of a Mifare® transponder. The parameterization of the command message takes place in accordance with the proprietary protocol (see protocol documentation in SDK: https://download.idtronic.de/Embedded/Embedded%20HF%20SDK.zip) in the structure stSendMessage.

TYPE ST_IdTronic_CommandMessage :
STRUCT
    nStationId : BYTE;
    nDataLength : USINT := 1; // includes the cmd byte and the data bytes
    eCmd : E_IdTronic_Command;
    aData : ARRAY[0..253] OF BYTE;
END_STRUCT
END_TYPE

Following the adaptation of this parameter structure the command message can be transmitted to the RFID reader.

(* send message *)
IF bSendMessage AND NOT fbSendMsg.bBusy THEN
    bSendMessage := FALSE;
    fbSendMsg.bExecuteCmd := TRUE;
    // todo: adapt stSendMessage for the selected command
    fbSendMsg.stSendMessage := stSendMessage;
END_IF
fbSendMsg(TxBuffer:=TxBuffer);

Messages received are displayed in the structure stRcvMessage and only the details need to be processed further.

(* receive message *)
fbRcvMsg(RxBuffer:=RxBuffer);
IF NOT fbRcvMsg.bBusy AND NOT fbRcvMsg.bError AND fbRcvMsg.bMsgReceived THEN
    stRcvMessage := fbRcvMsg.stRcvMessage;
    // todo: process the received message
END_IF

stRcvMessage is structured as follows:

TYPE ST_IdTronic_ReplyMessage :
STRUCT
    nStationId : BYTE;
    nDataLength : USINT; // includes the status byte and the data bytes
    eStatus : E_IdTronic_StatusCodes;
    aData : ARRAY[0..253] OF BYTE;
END_STRUCT
END_TYPE

Analogous to the sequence above, this sample program can be used to program complex state machines, in the course of which comprehensive communication is established between the TwinCAT PLC and the iDTRONIC RFID reader.