Main Content

本页面提供的是上一版软件的文档。当前版本中已删除对应的英文页面。

CAN 通道报文筛选

以下示例说明如何使用 CAN 报文筛选器以仅允许包含指定标识符的报文通过通道。它使用以环回配置形式连接的 MathWorks Virtual CAN 通道。

创建传输和接收通道

一个通道将向另一个通道传输报文。筛选器在接收通道上设置。

txCh = canChannel('MathWorks', 'Virtual 1', 1);
rxCh = canChannel('MathWorks', 'Virtual 1', 2);

创建报文

在整个示例中,这些报文被多次发送到接收通道。请注意,其中一条报文具有扩展标识符。

txMsgs(1) = canMessage(250, false, 8);
txMsgs(2) = canMessage(500, false, 8);
txMsgs(3) = canMessage(1000, false, 8);
txMsgs(4) = canMessage(1500, true, 8);
txMsgs(5) = canMessage(2000, false, 8);

基于打开的筛选器接收报文

将通道设置为在线状态,传输并接收报文。请注意,发送的所有报文均已收到。新创建的通道上的筛选器设置对于标准标识符和扩展标识符都完全开放。

start(rxCh);
start(txCh);
transmit(txCh, txMsgs);
pause(0.5);
rxMsgs1 = receive(rxCh, Inf, 'OutputFormat', 'timetable')
stop(rxCh);
stop(txCh);
rxMsgs1 =

  5x8 timetable

        Time          ID     Extended       Name          Data        Length      Signals       Error    Remote
    _____________    ____    ________    __________    ___________    ______    ____________    _____    ______

    0.0064332 sec     250     false      {0x0 char}    {1x8 uint8}      8       {0x0 struct}    false    false 
    0.0064345 sec     500     false      {0x0 char}    {1x8 uint8}      8       {0x0 struct}    false    false 
    0.0064361 sec    1000     false      {0x0 char}    {1x8 uint8}      8       {0x0 struct}    false    false 
    0.006438 sec     1500     true       {0x0 char}    {1x8 uint8}      8       {0x0 struct}    false    false 
    0.0064487 sec    2000     false      {0x0 char}    {1x8 uint8}      8       {0x0 struct}    false    false 

绘制收到的报文的标识符,可以看到通道接收了所有发出的报文。

plot(1, rxMsgs1.ID, 'x')
h_gca = gca;
h_gca.XTick = 0:1:2;
h_gca.XTickLabel = {'', 'Transmit 1', ''};
axis([0 2 0 2047])
xlabel('Message Transmits')
ylabel('CAN Identifiers')

基于配置的筛选器接收报文

使用 filterAllowOnly 命令指定要按 CAN 标识符和标识符类型接收报文。

filterAllowOnly(rxCh, [500 2000], 'Standard');

显示通道以查看报文筛选器的配置状态。

rxCh
rxCh = 

  Channel with properties:

   Device Information
            DeviceVendor: 'MathWorks'
                  Device: 'Virtual 1'
      DeviceChannelIndex: 2
      DeviceSerialNumber: 0
            ProtocolMode: 'CAN'

   Status Information
                 Running: 0
       MessagesAvailable: 0
        MessagesReceived: 5
     MessagesTransmitted: 0
    InitializationAccess: 1
        InitialTimestamp: 29-Jan-2020 20:59:50
           FilterHistory: 'Standard ID Filter: Allow Only | Extended ID Filter: Allow All'

   Channel Information
               BusStatus: 'N/A'
              SilentMode: 0
         TransceiverName: 'N/A'
        TransceiverState: 'N/A'
       ReceiveErrorCount: 0
      TransmitErrorCount: 0
                BusSpeed: 500000
                     SJW: []
                   TSEG1: []
                   TSEG2: []
            NumOfSamples: []

   Other Information
                Database: []
                UserData: []

再次将报文传输到接收通道。请注意,这次收到的报文较少。

start(rxCh);
start(txCh);
transmit(txCh, txMsgs);
pause(0.5);
rxMsgs2 = receive(rxCh, Inf, 'OutputFormat', 'timetable')
stop(rxCh);
stop(txCh);
rxMsgs2 =

  3x8 timetable

        Time          ID     Extended       Name          Data        Length      Signals       Error    Remote
    _____________    ____    ________    __________    ___________    ______    ____________    _____    ______

    0.0011395 sec     500     false      {0x0 char}    {1x8 uint8}      8       {0x0 struct}    false    false 
    0.0011435 sec    1500     true       {0x0 char}    {1x8 uint8}      8       {0x0 struct}    false    false 
    0.0011448 sec    2000     false      {0x0 char}    {1x8 uint8}      8       {0x0 struct}    false    false 

将新接收数据添加到绘图中以查看筛选器阻止了哪些报文。具有扩展标识符的报文应该根据数值被阻止,但是它通过了筛选器,因为筛选器仅针对标准标识符进行了配置。

plot(1, rxMsgs1.ID, 'x', 2, rxMsgs2.ID, 'x');
h_gca = gca;
h_gca.XTick = 0:1:3;
h_gca.XTickLabel = {'', 'Transmit 1', 'Transmit 2', ''};
axis([0 3 0 2047])
xlabel('Message Transmits')
ylabel('CAN Identifiers')

重置报文筛选器

您可以使用 filterAllowAll 命令将报文筛选器重置为其默认的开放状态。

filterAllowAll(rxCh, 'Standard');

显示通道以查看报文筛选器的配置状态。

rxCh
rxCh = 

  Channel with properties:

   Device Information
            DeviceVendor: 'MathWorks'
                  Device: 'Virtual 1'
      DeviceChannelIndex: 2
      DeviceSerialNumber: 0
            ProtocolMode: 'CAN'

   Status Information
                 Running: 0
       MessagesAvailable: 0
        MessagesReceived: 3
     MessagesTransmitted: 0
    InitializationAccess: 1
        InitialTimestamp: 29-Jan-2020 20:59:51
           FilterHistory: 'Standard ID Filter: Allow All | Extended ID Filter: Allow All'

   Channel Information
               BusStatus: 'N/A'
              SilentMode: 0
         TransceiverName: 'N/A'
        TransceiverState: 'N/A'
       ReceiveErrorCount: 0
      TransmitErrorCount: 0
                BusSpeed: 500000
                     SJW: []
                   TSEG1: []
                   TSEG2: []
            NumOfSamples: []

   Other Information
                Database: []
                UserData: []

第三次传输和接收,可以看到所有报文再次通过筛选器并进入通道。

start(rxCh);
start(txCh);
transmit(txCh, txMsgs);
pause(0.5);
rxMsgs3 = receive(rxCh, Inf, 'OutputFormat', 'timetable')
stop(rxCh);
stop(txCh);
rxMsgs3 =

  5x8 timetable

        Time          ID     Extended       Name          Data        Length      Signals       Error    Remote
    _____________    ____    ________    __________    ___________    ______    ____________    _____    ______

    0.0010984 sec     250     false      {0x0 char}    {1x8 uint8}      8       {0x0 struct}    false    false 
    0.0010992 sec     500     false      {0x0 char}    {1x8 uint8}      8       {0x0 struct}    false    false 
    0.0011 sec       1000     false      {0x0 char}    {1x8 uint8}      8       {0x0 struct}    false    false 
    0.0011006 sec    1500     true       {0x0 char}    {1x8 uint8}      8       {0x0 struct}    false    false 
    0.0011012 sec    2000     false      {0x0 char}    {1x8 uint8}      8       {0x0 struct}    false    false 

将新数据添加到绘图中,观察第一次和第三次传输是相同的,因为报文筛选器在这两种情况下均为完全开放。

plot(1, rxMsgs1.ID, 'x', 2, rxMsgs2.ID, 'x', 3, rxMsgs3.ID, 'x')
h_gca = gca;
h_gca.XTick = 0:1:4;
h_gca.XTickLabel = {'', 'Transmit 1', 'Transmit 2', 'Transmit 3', ''};
axis([0 4 0 2047])
xlabel('Message Transmits')
ylabel('CAN Identifiers')

按名称筛选报文

使用 CAN 数据库文件时,filterAllowOnly 命令还可以按名称引用报文。

db = canDatabase('demoVNT_CANdbFiles.dbc');
rxCh.Database = db;
filterAllowOnly(rxCh, 'EngineMsg');
rxCh
rxCh = 

  Channel with properties:

   Device Information
            DeviceVendor: 'MathWorks'
                  Device: 'Virtual 1'
      DeviceChannelIndex: 2
      DeviceSerialNumber: 0
            ProtocolMode: 'CAN'

   Status Information
                 Running: 0
       MessagesAvailable: 0
        MessagesReceived: 5
     MessagesTransmitted: 0
    InitializationAccess: 1
        InitialTimestamp: 29-Jan-2020 20:59:52
           FilterHistory: 'Standard ID Filter: Allow Only | Extended ID Filter: Allow All'

   Channel Information
               BusStatus: 'N/A'
              SilentMode: 0
         TransceiverName: 'N/A'
        TransceiverState: 'N/A'
       ReceiveErrorCount: 0
      TransmitErrorCount: 0
                BusSpeed: 500000
                     SJW: []
                   TSEG1: []
                   TSEG2: []
            NumOfSamples: []

   Other Information
                Database: [1x1 can.Database]
                UserData: []

阻止特定类型的所有报文

使用 filterBlockAll 命令可以快速设置筛选器以阻止标准或扩展标识符类型的所有报文。

filterBlockAll(rxCh, 'Extended');
rxCh
rxCh = 

  Channel with properties:

   Device Information
            DeviceVendor: 'MathWorks'
                  Device: 'Virtual 1'
      DeviceChannelIndex: 2
      DeviceSerialNumber: 0
            ProtocolMode: 'CAN'

   Status Information
                 Running: 0
       MessagesAvailable: 0
        MessagesReceived: 5
     MessagesTransmitted: 0
    InitializationAccess: 1
        InitialTimestamp: 29-Jan-2020 20:59:52
           FilterHistory: 'Standard ID Filter: Allow Only | Extended ID Filter: Block All'

   Channel Information
               BusStatus: 'N/A'
              SilentMode: 0
         TransceiverName: 'N/A'
        TransceiverState: 'N/A'
       ReceiveErrorCount: 0
      TransmitErrorCount: 0
                BusSpeed: 500000
                     SJW: []
                   TSEG1: []
                   TSEG2: []
            NumOfSamples: []

   Other Information
                Database: [1x1 can.Database]
                UserData: []