CAN_FilterMessages

PCAN-Basic

PCAN-Basic Documentation
Home
PreviousUpNext
CAN_FilterMessages

Configures the reception filter.

Syntax
TPCANStatus __stdcall CAN_FilterMessages(
        TPCANHandle Channel,
        DWORD FromID,
        DWORD ToID,
        TPCANMode Mode
);
Parameters 
Description 
Channel 
The handle of a PCAN Channel (see TPCANHandle). 
FromID 
The lowest CAN ID wanted to be received. 
ToID 
The highest CAN ID wanted to be received. 
Mode 
The type of the filter being set (see TPCANType). 

The return value is a TPCANStatus code. PCAN_ERROR_OK is returned on success. The typical errors in case of failure are:

PCAN_ERROR_INITIALIZE: 
Indicates that the given PCAN channel was not found in the list of initialized channels of the calling application. 

Note that after a PCAN Channel is initialized, the status of its filter is fully opened. According with the current filter status, calling this function causes the following behavior:

  • Filter status is PCAN_FILTER_OPEN: The filter is automatically closed and then configured with the given range of IDs passed to this function [FromID, ToID] .
  • Filter status is PCAN_FILTER_CLOSE: The filter is set to the given range of IDs passed to this function [FromID, ToID] .
  • Filter status is PCAN_FILTER_CUSTOM: The filter is expanded with the given range of Ids [FromID, ToID]. If a smaller or different range is required than a range that has been configured before, the filter has to be closed first before calling the CAN_FilterMessages function. To do this use the function CAN_SetValue.

The parameter 'Mode' indicates which kind of ID is being used to register the new filter range. There are two possible values, Standard (11-bit identifier) or Extended (29-bit identifier). Standard frames are using the bit positions 28 to 18 of the Acceptance Mask/Code registers in the SJA1000 CAN controller. Drivers for 82C200 CAN controllers have to shift the bits down to positions 10 to 0. 

Take in account that configuring the message filter cause the CAN controller to enter the Reset state. This will affect other applications that communicate with the same PCAN hardware. 

Notes: 

  1. There is only one filter for standard and extended CAN messages. It seems that the ID from a standard message uses the most significant 11 bits (bit 18 to 28) of the 29 bits. I.e. the standard ID 400h is also received by indicating an extended ID 10000000h. For this reason it is not recommended to mix standard and extended filters, since it can increase the risk of receiving unwanted messages.
  2. Multiple calls of CAN_FilterMessages expand the reception filter.
  3. It is not guaranteed that an application only receives CAN messages in the range of FromID to ToID. This is caused by the operating principle of the SJA1000's acceptance filter. See also Philips Data Sheet "SJA1000 Stand-alone CAN-controller".

The following example shows the use of CAN_FilterMessages on the channel PCAN_USBBUS1 to receive a custom range of IDs. In case of failure, the returned code will be translated to a text (according with the operating system language) in English, German, Italian, French or Spanish, and it will be shown to the user. 

Note: It is assumed that the channel was already initialized. 

C++:  

TPCANStatus result;
char strMsg[256];
DWORD iBuffer;

// The message filter is closed first to ensure the reception of the new range of IDs.
//
iBuffer = PCAN_FILTER_CLOSE;
result = CAN_SetValue(PCAN_USBBUS1,PCAN_MESSAGE_FILTER,&iBuffer, sizeof(iBuffer));

if(result != PCAN_ERROR_OK)
{
    // An error occurred, get a text describing the error and show it
    //
    CAN_GetErrorText(result, 0, strMsg);
    MessageBox(strMsg);
}
else
{
    // The message filter is configured to receive the IDs 2,3,4 and 5 on the PCAN-USB, Channel 1
    //
    result = CAN_FilterMessages(PCAN_USBBUS1, 2, 5, PCAN_MESSAGE_STANDARD);
    if(result != PCAN_ERROR_OK)
    {
        // An error occurred, get a text describing the error and show it
        //
        CAN_GetErrorText(result, 0, strMsg);
        MessageBox(strMsg);
    }
    else
        MessageBox("Filter successfully configured for IDs 2,3,4 and 5");
}

CAN_SetValue 

 

Class-method Version: FilterMessages

Copyright © 2017. PEAK-System Technik GmbH. All rights reserved.
Send feedback to this documentation