canReceiveMessage (Functionblock)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

MX2 pro, DX4 pro, CX1 pro-c/warp-c, MX2 turbo/encore/warp, NX-200, NX-400, LX2, LX5

Firmware version:

1.00 / 1.00.00


This receives a message from the CAN network. Before any reception is realised, a receive filter must have been created with canFilterCreate, canFilterCreateOnce, canFilterCreateX, or canFMSFilterCreate. If the canLoopBackMode is enabled, all transmitted messages from the RTCU will be received - but only if a receive filter is set up. The function block must also be initialized and manually updated in order to receive any messages. When a message has been received, the ready flag will be set, and the buffer defined during initialization contains the data while the function block output variables contain the message identifier (ID) information. The data is valid until the function block is updated again. The RTCU will buffer all valid incoming messages in an internal buffer with room for 100 messages - further messages will be lost. This will not, however, affect the logger which has its own buffer.

 

The function block can only receive CAN messages from the first CAN bus.

 

 

Input:

data : PTR

Address of the buffer to receive the data in.

Note: the buffer must be 8 bytes long.

 

 

Output:

xtd : BOOL

TRUE:

Message uses the extended identifier (29 bits).

FALSE:

Message uses the standard identifier (11 bits).

 

id : DINT

The identifier of the message.

 

datasize : SINT

Number of bytes received.

 

ready : BOOL

True if a message has been received.

 

 

Declaration:

FUNCTION_BLOCK canReceiveMessage;
VAR_INPUT
  data     : PTR;
END_VAR;
VAR_OUTPUT
  xtd     : BOOL;
  ID       : DINT;
  datasize : SINT;
  ready   : BOOL;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
VAR
  canRX : canReceiveMessage;
  buf   : ARRAY [1..8] OF SINT;
END_VAR;
 
PROGRAM CANExample;
VAR
  FilterID : SINT;
  rc       : INT;
END_VAR;
 
// Open can
canOpen(baud := 250, monitor := FALSE);
canRX(data := ADDR(buf));
 
canLoopBackMode(enable := ON);
 
// startID: Priority=3 Reserved=1 Data page=0 PGN=00FDD6
FilterID := canFilterCreate(xtd:=TRUE,startID:=16#0EFDD600,length:=6);
 
rc := canSendMessage(xtd:=TRUE,ID:=16#0EFDD600,data:=ADDR(buf),datasize:=8);
IF rc = 0 THEN
  DebugMsg(message:="CAN message sent");
ELSE
  DebugFmt(message:="CAN message failed (\1)",v1:=rc);
END_IF;
 
BEGIN
  canRX();
  ...
  IF canRX.ready THEN
    DebugMsg(message:="Message received!");
    DebugFmt(message:="canRX.xtd= \1", v1:=INT(canRX.xtd));
    DebugFmt(message:="canRX.ID= \4", v4:=canRX.ID);
    DebugFmt(message:="canRX.DataSize= \1", v1:=INT(canRX.DataSize));
  END_IF;
  ...
END;
 
END_PROGRAM;