Main Content

canChannel

Connection to CAN channel connected to a 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 with a specified device.

ch = canChannel(raspiObj,device,Name,Value) creates a CAN channel with a specified device and additional options specified as name-value arguments.

ch = canChannel(raspiObj,interface) creates a CAN channel with a specified interface.

ch = canChannel(raspiObj,interface, Name,Value) creates a CAN channel with a specified interface and additional options specified as 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

CAN interface is specified as either can0 or can1.

Example: can0

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,"MCP2515", "BusSpeed", 500e3)

The frequency at which the CAN bus runs, specified as a number.

If SilentMode is specified true a CAN channel can only receive messages and not transmit.

This property can be used along with BusSpeed and SilentMode for PiCAN2 Duo.

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 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 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 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

Create canChannel for PiCAN2 with NV Pairs

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 to the PiCAN2 with NV pairs.

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

Create canChannel with can0 Interface with NV Pairs

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 can0 interface with NV pairs

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 the canchannel object creation fails, check if the CAN interface is online.

    To check if the CAN interface is online, type the following command in the MATLAB command window.

    raspiObj.openShell

    A Raspberry Pi terminal opens. Type the 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 too. 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