canSendMessage (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


Send a CAN message with the defined ID and contents of a buffer. The message Identifier (ID) is by default set to be an extended ID. The maximum data size to send is limited to 8 bytes per transmission.

 

Note:
From firmware version 4.68 / R1.10.00 this function supports reporting of bus-error conditions.

 

 

Input:

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

The port of the CAN bus.

 

xtd : BOOL (default TRUE)

If true, the package uses the extended identifier (29 bits).

 

ID : DINT

The identifier of the package.

 

data : PTR

Address of the buffer that contains the data.

 

datasize : INT (1..8)

Number of bytes in buffer.

 

 

Returns: SINT

0

- Success. The Message was sent and acknowledged on the bus.

1

- The CAN bus is not open.

2

- Datasize out of range.

3

- Invalid buffer.

4

- Timeout. The message was not acknowledged on the bus.

5

- Monitor mode. Transmission not allowed.

6

- An bus-error was detected. This includes a short-circuit condition or write operation impossible (missing jumper condition).

 

Declaration:

FUNCTION canSendMessage : SINT;
VAR_INPUT
  port     : SINT := 1;
  xtd     : BOOL := TRUE;
  ID       : DINT;
  data     : PTR;
  datasize : INT;
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;