Main Content

canChannel

Connection to CAN channel through specified device

Since R2021b

Add-On Required: This feature requires the MATLAB Support Package for Raspberry Pi Hardware add-on.

Description

Use this object to create a connection to the CAN channel through the specified device connected to the Raspberry Pi® hardware. To transmit and receive data from Raspberry Pi hardware through the CAN bus, use the Object Functions.

Creation

Description

ch = canChannel(raspiObj) creates a CAN channel.

ch = canChannel(raspiObj,device) creates a CAN channel through specified device.

ch = canChannel(raspiObj,device,Name=Value) also specifies the channelName or sets the BusSpeed and SilentMode properties using one or more optional name-value arguments.

ch = canChannel(raspiObj,interface) creates a CAN channel and sets the interface property.

ch = canChannel(raspiObj,interface,Name,Value) also specifies the channelName or sets the BusSpeed and SilentMode properties using one or more optional name-value arguments.

Input Arguments

expand all

Connection to a Raspberry Pi hardware board, specified as a raspi object.

Data Types: object

The CAN shield device.

Example: PiCAN2

Data Types: string

Name-Value Arguments

Specify comma-separated pair of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN

Example: ch = canChannel(raspiObj,"PiCAN2", "BusSpeed", 500e3)

CAN channel for transmission, specified as CAN A or CAN B. You can use this input argument along with BusSpeed and SilentMode properties for the PiCAN2 Duo board.

Properties

expand all

CAN interface, specified as can0 or can1.

Example: can0

Data Types: string

This property is read-only.

Communication protocol, returned as CAN. This property specifies the protocol used for communication.

Example:

>> ch.ProtocolMode

ans =
    "CAN"

Data Types: string

CAN bus frequency, specified as a number.

Option to set CAN channel in the silent mode, specified as a numeric or logical 1 (true) or 0 (false). If you set SilentMode to true, the CAN channel only receives messages and does not transmit.

CAN database information, specified as a can.Database object or as an empty structure [ ].

To set this property, you require a Vehicle Network Toolbox™ license.

Example:

db = canDatabase(exp.dbc);
ch.Database = db;   
    

Object Functions

Use these object functions to transmit and receive messages from the CAN bus.

readRead messages from CAN channel
writeWrite messages to CAN channel

Examples

Create canChannel Object for PiCAN2 Shield

Create a Raspberry Pi object.

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

  raspi with properties:

         DeviceAddress: '192.168.1.101'                
                  Port: 18734                         
             BoardName: 'Raspberry Pi 3 Model B+'     
         AvailableLEDs: {'led0'}                      
  AvailableDigitalPins: [4,5,6,10,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
  AvailableSPIChannels: {'CE1'}                       
     AvailableI2CBuses: {'i2c-0','i2c-1'}             
      AvailableWebcams: {}                            
           I2CBusSpeed: 100000                        
AvailableCANInterfaces: {"can0"}                      

  Supported peripherals

Create a connection to the PiCAN2 shield.

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

  Channel with properties:

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

  Show all properties

clear raspiObj ch

Create canChannel Object for PiCAN2 DUO Shield

Create a Raspberry Pi object.

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

  raspi with properties:

         DeviceAddress: '192.168.1.101'                
                  Port: 18734                         
             BoardName: 'Raspberry Pi 3 Model B+'     
         AvailableLEDs: {'led0'}                      
  AvailableDigitalPins: [4,5,6,10,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
  AvailableSPIChannels: {'CE1'}                       
     AvailableI2CBuses: {'i2c-0','i2c-1'}             
      AvailableWebcams: {}                            
           I2CBusSpeed: 100000                        
AvailableCANInterfaces: {"can0", "can1"}                      

  Supported peripherals

Create a connection to the PiCAN2 DUO shield.

ch = canChannel(raspiObj,"PiCAN2 DUO")
ch = 

  Channel with properties:

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

  Show all properties

clear raspiObj ch

Create canChannel Object for can0 Interface

Create a Raspberry Pi object.

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

  raspi with properties:

         DeviceAddress: '192.168.1.101'                
                  Port: 18734                         
             BoardName: 'Raspberry Pi 3 Model B+'     
         AvailableLEDs: {'led0'}                      
  AvailableDigitalPins: [4,5,6,10,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
  AvailableSPIChannels: {'CE1'}                       
     AvailableI2CBuses: {'i2c-0','i2c-1'}             
      AvailableWebcams: {}                            
           I2CBusSpeed: 100000                        
AvailableCANInterfaces: {"can0"}                      

  Supported peripherals

Create a connection with the can0 interface.

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

  Channel with properties:

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

  Show all properties

clear raspiObj ch

Set Properties of CAN Channel for PiCAN2 Shield

Create a Raspberry Pi object.

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

  raspi with properties:

         DeviceAddress: '192.168.1.101'                
                  Port: 18734                         
             BoardName: 'Raspberry Pi 3 Model B+'     
         AvailableLEDs: {'led0'}                      
  AvailableDigitalPins: [4,5,6,10,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
  AvailableSPIChannels: {'CE1'}                       
     AvailableI2CBuses: {'i2c-0','i2c-1'}             
      AvailableWebcams: {}                            
           I2CBusSpeed: 100000                        
AvailableCANInterfaces: {"can0"}                      

  Supported peripherals

Create a connection to the PiCAN2 shield and set the channel properties.

ch = canChannel(raspiObj,"PiCAN2","BusSpeed",250e3,"SilentMode",true)
ch = 

  Channel with properties:

       Interface: 'can0'
    ProtocolMode: 'CAN'
        BusSpeed: 250000
      SilentMode: 1
        Database: []

  Show all properties

clear raspiObj ch

Set Properties of can0 Interface Channel

Create an Raspberry Pi object.

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

  raspi with properties:

         DeviceAddress: '192.168.1.101'                
                  Port: 18734                         
             BoardName: 'Raspberry Pi 3 Model B+'     
         AvailableLEDs: {'led0'}                      
  AvailableDigitalPins: [4,5,6,10,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
  AvailableSPIChannels: {'CE1'}                       
     AvailableI2CBuses: {'i2c-0','i2c-1'}             
      AvailableWebcams: {}                            
           I2CBusSpeed: 100000                        
AvailableCANInterfaces: {"can0"}                      

  Supported peripherals

Create a connection with the can0 interface and set the channel properties.

ch = canChannel(raspiObj,"can0","BusSpeed",250e3,"SilentMode",true)
ch = 

  Channel with properties:

       Interface: 'can0'
    ProtocolMode: 'CAN'
        BusSpeed: 250000
      SilentMode: 1
        Database: []

  Show all properties

clear raspiObj ch

Tips

  • If you cannot create a canChannel object, the canchannel object creation fails, check if the CAN interface is connected to the internet.

    To check if the CAN interface is connected to the internet, type the following command in the MATLAB command window.

    raspiObj.openShell

    In the Raspberry Pi terminal, type this command.

     $ ifconfig

    You can check for can0 presence in all Raspberry Pi boards. For PiCAN2 Duo, you can check if can1 is in the output. If present, execute the following command in the Raspberry Pi terminal and then recreate the canchannel object in MATLAB.

    r.system(' sudo ip link set can0 down type can');
  • To troubleshoot PiCAN2, PiCAN2 Duo, and PiCAN3 shields, see here.

Extended Capabilities

Version History

Introduced in R2021b