canReceiveX (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

NX-200, NX-400, LX2, LX5

Firmware version:

1.30.02


This receives a message from a CAN network. Before any reception is realized, 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.

 

 

Input:

port : SINT (1/2) (default 1)

The port of the CAN bus.

 

timeout : INT (-1..32767) (default 0)

The number of milliseconds to wait for a CAN message

0

- Return immediately if no message

-1

- Wait for ever.

 

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 : INT

Number of bytes received.

 

 

Returns: INT

1

- Successful.

0

- The canReceiveX function is not supported.

-1

- The CAN bus is not present.

-2

- The CAN bus is not open.

-3

- Illegal parameter.

-4

- Timeout.

 

 

Declaration:

FUNCTION canReceiveX : INT;
VAR_INPUT
  port     : SINT := 1;
  timeout  : INT := 0;
  data     : PTR;
  xtd      : ACCESS BOOL;
  ID       : ACCESS DINT;
  datasize : ACCESS INT;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
VAR
  buf   : ARRAY [1..8] OF SINT;
END_VAR;
 
PROGRAM CANExample;
VAR
  FilterID : SINT;
  rc       : INT;
  msg_xtd  : BOOL;
  msg_id   : DINT;
  msg_size : INT;
END_VAR;
 
// Open can
canOpen(baud := 250, monitor := FALSE);
 
canLoopBackMode(enable := ON);
 
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
  ...
  rc := canReceiveX(port := 1, timeout := -1, data := ADDR(buf), xtd := msg_xtd, id := msg_id, datasize := msg_size);
  CASE rc OF

  1:  DebugMsg(message:="CAN message received.");
      DebugFmt(message:="canRX - xtdd= \1", v1:=INT(msg_xtd));
      DebugFmt(message:="canRX - ID= \4", v4:=msg_id);
      DebugFmt(message:="canRX - DataSize= \1", v1:=msg_size);
  0:  DebugMsg(message:="The canReceiveX function is not supported.");
  -1: DebugMsg(message:="The CAN bus is not present.");
  -2: DebugMsg(message:="The CAN bus is not open.");
  -3: DebugMsg(message:="Illegal parameter.");
  -4: DebugMsg(message:="Timeout on CAN receive.");
  ELSE
      DebugFmt(message:="canReceiveX (rc=\1)", v1:=rc);
  END_CASE;
  ...
END;
 
END_PROGRAM;