Important
After a PCAN Channel is initialized, the status of its filter is fully opened.
public static TPCANStatus FilterMessages(
ushort Channel,
uint FromID,
uint ToID,
TPCANMode Mode
)
Public Shared Function FilterMessages (
Channel As UShort,
FromID As UInteger,
ToID As UInteger,
Mode As TPCANMode
) As TPCANStatus
public:
static TPCANStatus FilterMessages(
unsigned short Channel,
unsigned int FromID,
unsigned int ToID,
TPCANMode Mode
)
The return value is a TPCANStatus value. PCAN_ERROR_OK is returned on success. The typical errors in case of failure are:
Based on the current state of the filter, calling this method causes the following behavior:
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 into account that configuring the message filter causes the CAN controller to enter the reset state. This will affect other applications that communicate with the same PCAN hardware.
The following example shows the use of FilterMessages(UInt16, UInt32, UInt32, TPCANMode) method 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.
TPCANStatus result;
System.Text.StringBuilder strMsg;
UInt32 iBuffer;
strMsg = new System.Text.StringBuilder(256);
// ... channel is initialized ...
// The message filter is closed first to ensure the reception of the new range of IDs.
//
iBuffer = PCANBasic.PCAN_FILTER_CLOSE;
result = PCANBasic.SetValue(PCANBasic.PCAN_USBBUS1, TPCANParameter.PCAN_MESSAGE_FILTER, ref iBuffer, sizeof(UInt32));
if (result != TPCANStatus.PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
PCANBasic.GetErrorText(result, 0, strMsg);
Console.WriteLine(strMsg.ToString());
}
else
{
// The message filter is configured to receive the IDs 2,3,4 and 5 on the PCAN-USB, Channel 1
//
result = PCANBasic.FilterMessages(PCANBasic.PCAN_USBBUS1, 2, 5, TPCANMode.PCAN_MODE_STANDARD);
if (result != TPCANStatus.PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
PCANBasic.GetErrorText(result, 0, strMsg);
Console.WriteLine(strMsg.ToString());
}
else
Console.WriteLine("Filter successfully configured for IDs 2,3,4 and 5");
}
Dim result As TPCANStatus
Dim strMsg As System.Text.StringBuilder
Dim iBuffer As UInt32
strMsg = New System.Text.StringBuilder(256)
' ... channel is initialized ...
' The message filter is closed first to ensure the reception of the new range of IDs.
'
iBuffer = PCANBasic.PCAN_FILTER_CLOSE
result = PCANBasic.SetValue(PCANBasic.PCAN_USBBUS1, TPCANParameter.PCAN_MESSAGE_FILTER, iBuffer, Convert.ToUInt32(Len(iBuffer)))
If result <> TPCANStatus.PCAN_ERROR_OK Then
' An error occurred, get a text describing the error and show it
'
PCANBasic.GetErrorText(result, 0, strMsg)
Console.WriteLine(strMsg.ToString)
Else
' The message filter is configured to receive the IDs 2,3,4 and 5 on the PCAN-USB, Channel 1
'
result = PCANBasic.FilterMessages(PCANBasic.PCAN_USBBUS1, 2, 5, TPCANMode.PCAN_MODE_STANDARD)
If result <> TPCANStatus.PCAN_ERROR_OK Then
' An error occurred, get a text describing the error and show it
'
PCANBasic.GetErrorText(result, 0, strMsg)
Console.WriteLine(strMsg.ToString)
Else
Console.WriteLine("Filter successfully configured for IDs 2,3,4 and 5")
End If
End If
TPCANStatus result;
System::Text::StringBuilder^ strMsg;
UInt32 iBuffer;
strMsg = gcnew System::Text::StringBuilder(256);
// ... channel is initialized ...
// The message filter is closed first to ensure the reception of the new range of IDs.
//
iBuffer = PCANBasic::PCAN_FILTER_CLOSE;
result = PCANBasic::SetValue(PCANBasic::PCAN_USBBUS1, TPCANParameter::PCAN_MESSAGE_FILTER, iBuffer, sizeof(iBuffer));
if (result != TPCANStatus::PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
PCANBasic::GetErrorText(result, 0, strMsg);
Console::WriteLine(strMsg->ToString());
}
else
{
// The message filter is configured to receive the IDs 2,3,4 and 5 on the PCAN-USB, Channel 1
//
result = PCANBasic::FilterMessages(PCANBasic::PCAN_USBBUS1, 2, 5, TPCANMode::PCAN_MODE_STANDARD);
if (result != TPCANStatus::PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
PCANBasic::GetErrorText(result, 0, strMsg);
Console::WriteLine(strMsg->ToString());
}
else
Console::WriteLine("Filter successfully configured for IDs 2,3,4 and 5");
}