canChannel
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
Syntax
Description
creates a CAN channel.ch
= canChannel(raspiObj
)
also specifies the channelName or sets the BusSpeed and SilentMode
properties using one or more optional name-value arguments.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
,Name
,Value
)
Input Arguments
Properties
Object Functions
Use these object functions to transmit and receive messages from the CAN bus.
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. ForPiCAN2 Duo
, you can check ifcan1
is in the output. If present, execute the following command in the Raspberry Pi terminal and then recreate thecanchannel
object in MATLAB.r.system(' sudo ip link set can0 down type can');
To troubleshoot
PiCAN2
,PiCAN2 Duo
, andPiCAN3
shields, see here.
Extended Capabilities
Version History
Introduced in R2021b
See Also
read
| write
| enableCAN
| disableCAN