canFilterCreateOnce (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 create a receive filter similar to canFilterCreate but with automatic destruction after the first valid message has been received (one shot).

When a filter has been successfully created, the function returns an ID for the filter.

This ID can be used to check the status of the filter with canFilterStatus. It is recommended to check if a filter is still active (no valid message received) before creating it again.

When using multiple one shot filters, it is mandatory to destroy the filter with canFilterDestroy before creating the filter again. If one is only using a single one shot filter, it is optional to destroy it.

Note: the filter created with this function will add the received message both to the internal buffer and to the Logger.

 

Please see the canFilterCreate function for more information on working with receive filters.

 

 

Input:

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

The port of the CAN bus.

 

xtd : BOOL

Filter for standard or extended identifiers. Set to TRUE for extended identifiers.

 

startID : DINT (16#0...16#1FFF_FFFF)

The first identifier that is accepted.

 

length : DINT (16#1...16#2000_0000)

The length of the range of identifiers that are accepted. For a single identifier, the length should only be set to 1.

 

 

Returns: SINT

>0

- ID of the new filter.

-1

- Illegal start ID.

-2

- Illegal length.

-3

- No free filters.

-8

- The CAN bus is not open.

 

Declaration:

FUNCTION canFilterCreateOnce : SINT;
VAR_INPUT
  port   : SINT := 1;
  xtd     : BOOL;
  startid : DINT;
  length : DINT;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
VAR
  canRX : canReceiveMessage;
  buf   : ARRAY [1..8] OF SINT;
END_VAR;
 
PROGRAM CANExample;
VAR
  Filter_ID : SINT;
  FilterOnce : ARRAY [1..2] OF SINT;
  timer     : TON;
END_VAR;
 
// Open can
canOpen(baud := 250, monitor := FALSE);
canRX(data := ADDR(buf));
 
// Open filters
timer.pt := 1000;
Filter_ID := canFilterCreate(xtd := TRUE, startID := 16#0EFDD600, length := 6);
 
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;
  ...
  timer(trig := TRUE);
  IF timer.q THEN
    IF canFilterStatus(filterid := FilterOnce[1]) <> 1 THEN
        canFilterDestroy(filterid := FilterOnce[1]);
        FilterOnce[1] := canFilterCreateOnce(xtd := TRUE, startID := 16#0E00FDD3, length := 1);
    END_IF;
    IF canFilterStatus(filterid := FilterOnce[2]) <> 1 THEN
        canFilterDestroy(filterid := FilterOnce[2]);
        FilterOnce[2] := canFilterCreateOnce(xtd := TRUE, startID := 16#0E00FDD4, length := 1);
    END_IF;
     timer(trig := FALSE);
  END_IF;
  ...
END;
 
END_PROGRAM;