canOpen (Function)

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 will open a CAN port. Before the CAN functions can be used, the interface must be initialized and the baud rate must be set. A standard baud rate range is available for easy configuration. When the function has successfully returned, the messages can be transmitted (canSendMessage) and received (if a receive filter is created with canFilterCreate, canFilterCreateOnce, or canFilterCreateX). Once done with sending and receiving messages, the port can be closed with canClose.

 

Monitor mode is a way for the RTCU device to listen for and receive messages without interacting with the CAN network.

When in monitor mode, the canSendMessage is disabled and no messages can be sent. The RTCU device does not acknowledge messages received.

This means that if no other CAN device is present on the CAN network, the sender will repeat the message regularly; or if the sender is another RTCU device, that canSendMessage will return 4 (Timeout).

 

Note: when the CAN port is opened without having monitor enabled (and the hardware has write access, please see the technical documentation for the device), an acknowledgement is sent to the CAN network for all incoming messages as per the CAN standard.

 

 

Input:

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

The port of the CAN bus.

 

baud : INT (50, 62 (62.5 kbps), 100,125,250,500,1000) (default 100)

Selects the desired baud rate in Kbps.

 

monitor : BOOL (default TRUE)

Selects monitor mode.

 

 

Returns: INT

0

- Successful.

1

- Unsupported baud rate.

2

- The baud rate is not possible at the selected CPU speed (see pmSetSpeed).

3

- The CAN bus is not present.

 

Declaration:

FUNCTION canOpen : BOOL;
VAR_INPUT
  port   : SINT := 1;
  baud   : INT := 100;
  monitor : BOOL := TRUE;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
VAR
  canin : canReceiveMessage;
  buf   : ARRAY[1..8] OF SINT;
END_VAR;
 
PROGRAM CANExample;
VAR
  CAN_ID : SINT;
END_VAR;
 
// Open can
canOpen(baud:=250);
canin(data:=ADDR(buf));
 
// startID: Priority=3 Reserved=1 Data page=0 PGN=00FDD6
CAN_ID := canFilterCreate(xtd:=TRUE,startID:=16#0EFDD600,length:=6);
 
BEGIN
  canin();
  ...
  IF canin.ready THEN
         // Message received
     ...
  END_IF;
  ...
END;
 
END_PROGRAM;