Structure of the CAN interface

The CAN interface looks different depending on the options selected. An 11-bit identifier interface is structured differently to a 29-bit identifier interface. In addition, the interface may contain the Transaction Number as well as the Time Stamp. When using structures the target system must be considered and which alignment it supports. Corresponding attributes are usable under TwinCAT 3 {attribute 'pack_mode':= '0'}.

The interface consists of the communication with the interface and up to 32 CAN messages. The inputs can only be read if the outputs are written.

The interface is addressed as follows:

Structure of the CAN interface 1:
CAN interface - inputs
Structure of the CAN interface 2:
CAN interface - outputs

Outputs.TxCounter is set to +1 if data are to be transmitted. NoOfTxMessages also indicates how many messages are to be transmitted from the buffer. The RxCounter indicates whether there are new data in the buffer. NoOfRxMessages indicates how many new data are in the buffer.

If you have fetched the data, then set Outputs.RxCounter:=Inputs.RxCounter. The CAN interface then knows that it can fill the buffer again. All data must always be read out, because the CAN interface fills all message structures again when necessary.

Sample code for transmission

if Outputs.TxCounter = Inputs.TxCounter then
      for i=0 to NumberOfMessagesToSend do
               Outputs.TxMessage[i] = MessageToSend[i];
      End_for
      Outputs.NoOfTxMessages = NumberOfMessagesToSend;
      Outputs.TxCounter := Outputs.TxCounter + 1;
end_if

Sample code for reading

if Outputs.RxCounter <> Inputs.RxCounter then
       for I := 0 to (Inputs.NoOfRxMessages-1) do
            MessageReceived[i] := Inputs.RxMessage [i];
       End_for
       Outputs.RxCounter := Inputs.RxCounter;
end_if

Message structure when using the 11-bit identifier

The message structure when using the 11-bit identifier consists of the COB ID [2 bytes] and the 8 bytes of data. The COB ID has the following structure:

Structure of the CAN interface 3:
Message structure when using the 11-bit identifier

Since COB ID, length and RTR bit are coded in one word in the 11-bit identifier, the following example is helpful for decoding the data from the word. Select a structure here in which to store the decoded data.

IF RXCounter_Out <> RXCounter_In THEN
   FOR I := 0 TO (NoOfTxMessages-1) DO
       stCANInterfaceMessageValue[i].Lengh:=WORD_TO_BYTE(stCANInterfaceMessage[i].CobID) AND 16#0F;
       stCANInterfaceMessageValue[i].RTR:=stCANInterfaceMessage[i].CobID.4;
       stCANInterfaceMessageValue[i].CobID :=ROR(stCANInterfaceMessage[i].CobID,5) AND 16#07FF;
       stCANInterfaceMessageValue[i].Data := stCANInterfaceMessage[i].Data;
       CASE stCANInterfaceMessageValue[i].CobID OF
            16#318: COB318:=COB318+1;
            16#718: COB718:=COB718+1;
            16#1CD: COB1CD:=COB1CD+1;
            memcpy(ADR(TempValue),ADR(stCANInterfaceMessage[i].Data[6]),2);
            16#1ED: COB1ED:=COB1ED+1;
       ELSE
            COBALLOther:=COBAllOther+1;
       END_CASE
   End_for
   RXCounter_Out:=RXCounter_In;
END_IF

Message structure when using the 29-bit identifier

The message structure when using the 29-bit identifier consists of the length [2 bytes] of the COB ID [4 bytes] and the 8 bytes of data.

Lenght: Length of the data (0…8)

The COB ID has the following structure:

Structure of the CAN interface 4:
Message structure when using the 29-bit identifier