Main Content

read

Read messages from CAN channel

Description

message = read(ch) reads a timetable of CAN messages received on the CAN channel ch.

message = read(ch,numMessages) reads a timetable of CAN messages equal to numMessages received on the CAN channel ch.

Examples

Read CAN Messages

Create a Raspberry Pi™ object.

raspiObj = raspi("192.168.1.101","pi","raspberry");

Create a canChannel object.

ch = canChannel(raspiObj,"can0");

Read messages as a timetable.

read(ch)

ans =

  1×7 timetable

            Time            ID     Extended       Name              Data            Length      Signals       Error
    ____________________    ___    ________    __________    ___________________    ______    ____________    _____

    08-Apr-2021 11:36:32    291     false      {0×0 char}    {[0 1 2 3 4 5 6 7]}      8       {0×0 struct}    false

Read Specified Number of CAN Messages

Create a Raspberry Pi object.

raspiObj = raspi("192.168.1.101","pi","raspberry");

Create a canChannel object.

ch = canChannel(raspiObj,"can0")

Specify the length of message to read.

read(ch,5)

ans =

  5×7 timetable

            Time             ID     Extended       Name              Data            Length      Signals       Error
    ____________________    ____    ________    __________    ___________________    ______    ____________    _____

    08-Apr-2021 11:37:42     291     false      {0×0 char}    {[0 1 2 3 4 5 6 7]}      8       {0×0 struct}    false
    08-Apr-2021 11:37:42    1110     true       {0×0 char}    {[7 6 5 4 3 2 1 0]}      8       {0×0 struct}    false
    08-Apr-2021 11:37:42     291     false      {0×0 char}    {[0 1 2 3 4 5 6 7]}      8       {0×0 struct}    false
    08-Apr-2021 11:37:42    1110     true       {0×0 char}    {[7 6 5 4 3 2 1 0]}      8       {0×0 struct}    false
    08-Apr-2021 11:37:42     291     false      {0×0 char}    {[0 1 2 3 4 5 6 7]}      8       {0×0 struct}    false

Read CAN Messages with Database

Create a Raspberry Pi object.

raspiObj = raspi("192.168.1.101","pi","raspberry");

Create a canChannel object.

ch = canChannel(raspiObj,"can0")
ch = 

  Channel with properties:

       Interface: 'can0'
    ProtocolMode: 'CAN'
        BusSpeed: 500000
      SilentMode: 0
        Database: []

  Show all properties

Attach a database.

ch.Database = canDatabase('test.dbc')
ch = 

  Channel with properties:

       Interface: 'can0'
    ProtocolMode: 'CAN'
        BusSpeed: 500000
      SilentMode: 0
        Database: [1×1 can.Database]

  Show all properties

Read the can channel object.

read(ch,5)
ans =

    5×8 timetable

            Time             ID     Extended             Name                     Data            Length      Signals       Error    Remote
    ____________________    ____    ________    _______________________    ___________________    ______    ____________    _____    ______

    08-Apr-2021 11:39:24     291     false      {'StandardTestMessage'}    {[0 1 2 3 4 5 6 7]}      8       {1×1 struct}    false    false 
    08-Apr-2021 11:39:24    1110     true       {'ExtendedTestMessage'}    {[7 6 5 4 3 2 1 0]}      8       {1×1 struct}    false    false 
    08-Apr-2021 11:39:24     291     false      {'StandardTestMessage'}    {[0 1 2 3 4 5 6 7]}      8       {1×1 struct}    false    false 
    08-Apr-2021 11:39:24    1110     true       {'ExtendedTestMessage'}    {[7 6 5 4 3 2 1 0]}      8       {1×1 struct}    false    false 
    08-Apr-2021 11:39:24     291     false      {'StandardTestMessage'}    {[0 1 2 3 4 5 6 7]}      8       {1×1 struct}    false    false 

Input Arguments

collapse all

Connection to the CAN channel, specified as an object.

Maximum number of messages to read, specified as a positive numeric value.

Output Arguments

collapse all

CAN messages from the channel, returned as a timetable of messages. If you have the Vehicle Network Toolbox™ the message output contains additional information about CAN messages. If you use the Database property, the Name and Signals information of messages will also be filled.

Note

The message does not support Remote frames.

Extended Capabilities

Introduced in R2021b