canChannel
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
Input Arguments
Object Functions
Use these object functions to transmit and receive messages from the CAN bus.
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. ForPiCAN2 Duo
, you can check ifcan1
is in the output too. If present, execute the following command in the Raspberry Pi terminal and then recreatecanchannel
object in MATLABr.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