canChannel

Connection to CAN channel connected to a specified device

Description

This object represents a connection to the CAN channel through the specified device connected to the Arduino® hardware. To transmit and receive data from Arduino hardware through the CAN bus use Object Functions.

Creation

Description

example

ch = canChannel(arduinoObj,device) creates a CAN channel connected to the specified device.

example

ch = canChannel(arduinoObj,'MCP2515',ChipSelectPin,InterruptPin) creates a CAN channel connected to the MCP2515 chip with specified ChipSelectPin and InterruptPin .

example

ch = canChannel(arduinoObj,device,Name,Value) creates a CAN channel connected to the specified device with the specified additional options.

example

ch = canChannel(arduinoObj,'MCP2515',ChipSelectPin,InterruptPin,Name,Value) creates a CAN channel connected to the MCP2515 chip with specified ChipSelectPin and InterruptPin and the additional options.

Input Arguments

expand all

Arduino hardware connection created using arduino, specified as an arduino object.

Name of the CAN device to which the channel is connected, specified as a string. See Supported CAN Shields for all the supported devices.

Chip select pin of MCP2515, specified as a string or character vector. This is mandatory when you create a connection to the CAN channel with a MCP2515.

Interrupt pin of MCP2515, specified as a string or character vector. This is mandatory when you create a connection to the CAN channel with MCP2515.

Name-Value Pair 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(arduinoObj,"MCP2515", "D10", "D2", "OscillatorFrequency", 16e6, "BusSpeed", 500e3);

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

Frequency of the oscillator specified in Hertz.

Properties

expand all

This property is read-only.

CAN device to which the channel is connected specified as a string. See Supported CAN Shields for all the supported devices.

Example:

>> ch.Device

ans = 
    "MCP2515"

Data Types: string

This property is read-only.

Specifies the protocol used for communication.

Example:

>> ch.ProtocolMode

ans =
    "CAN"

Data Types: string

This property is read-only.

The frequency at which the CAN bus runs, specified in Hertz.

Example:

>> ch.BusSpeed

ans = 
     500000

Displays database object of type can.Database if used. This property displays an empty structure, [ ], if not used.

Example:

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

Dependencies

You can use this property only with Vehicle Network Toolbox™.

This property is read-only.

Frequency of the oscillator specified in Hertz.

Example:

>> ch.OscillatorFrequency

ans =
    '10000000'

Chip select pin of MCP2515, specified as a string or character vector.

Example:

>> ch.ChipSelectPin

ans =
    "D9"

Data Types: string

Interrupt pin of MCP2515, specified as a string or character vector.

Example:

>> ch.InterrupPin

ans =
    "D2"

Data Types: string

Supported CAN Shields

These device input arguments currently support these shields:

ShieldChip Select PinInterruptPin Oscillator FrequencySupported Boards
MKR CAN ShieldD3D716MHzMKR Form factor boards
Sparkfun CAN-Bus ShieldD10D216MHzUno
Seeed Studio CAN-Bus Shield V2D9D216MHzUno, Mega, Due, Leonardo

Note

If you connect a CAN shield to an Arduino board whose form factor is not the same as the Arduino board, ensure the RESET pin on the CAN shield is connected to the RESET pin on the Arduino board.

Object Functions

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

readRead messages from CAN channel
writeWrite messages to CAN bus

Examples

Create Connection to CAN Channel on Shield

Create an arduino object

arduinoObj = arduino('COM24', 'Uno', 'Libraries', 'CAN')
arduinoObj = 

  arduino with properties:
 
                  Port: 'COM24'
                 Board: 'Uno'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11'}
   AvailableAnalogPins: {'A0-A5'}
    AvailableI2CBusIDs: [0]
             Libraries: {'CAN', 'SPI'}
Show all properties

Create a connection to the CAN channel on Sparkfun CAN-Bus Shield

ch = canChannel(arduinoObj,"Sparkfun CAN-Bus Shield")
ch =
 
  canChannel with properties:
 
               Device: 'Sparkfun CAN-Bus Shield'
         ProtocolMode: 'CAN'
             BusSpeed: 500000
             Database: []
Show all properties
 
               Device: 'Sparkfun CAN-Bus Shield'
         ProtocolMode: 'CAN'
             BusSpeed: 500000
             Database: []

  OscillatorFrequency: 1.600000e+07
        ChipSelectPin: "D9"
         InterruptPin: "D2"

Create Connection to CAN Channel on MCP2515

Create an arduino object

arduinoObj = arduino('COM24', 'Uno', 'Libraries', 'CAN')
arduinoObj = 

  arduino with properties:
 
                  Port: 'COM24'
                 Board: 'Uno'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11'}
   AvailableAnalogPins: {'A0-A5'}
    AvailableI2CBusIDs: [0]
             Libraries: {'CAN', 'SPI'}
Show all properties

Create a connection to the CAN channel on MCP2515

ch = canChannel(arduinoObj, "MCP2515", "D10", "D2")
ch =
 
  canChannel with properties:
 
               Device: 'MCP2515'
         ProtocolMode: 'CAN'
             BusSpeed: 500000
             Database: []
Show all properties
 
               Device: 'MCP2515'
         ProtocolMode: 'CAN'
             BusSpeed: 500000
             Database: []

  OscillatorFrequency: 1.600000e+07
        ChipSelectPin: "D10"
         InterruptPin: "D2"

Create Connection to CAN Channel on Shield with Specified Bus Speed

Create an arduino object

arduinoObj = arduino('COM24', 'Uno', 'Libraries', 'CAN')
arduinoObj = 

  arduino with properties:
 
                  Port: 'COM24'
                 Board: 'Uno'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11'}
   AvailableAnalogPins: {'A0-A5'}
    AvailableI2CBusIDs: [0]
             Libraries: {'CAN', 'SPI'}
Show all properties

Create a connection to the CAN channel on Sparkfun CAN-Bus Shield

ch = canChannel(arduinoObj,"Sparkfun CAN-Bus Shield", "BusSpeed", 500e3)
ch =
 
  canChannel with properties:
 
               Device: 'Sparkfun CAN-Bus Shield'
         ProtocolMode: 'CAN'
             BusSpeed: 500000
             Database: []
Show all properties
  
               Device: 'Sparkfun CAN-Bus Shield'
         ProtocolMode: 'CAN'
             BusSpeed: 500000
             Database: []

  OscillatorFrequency: 1.600000e+07
        ChipSelectPin: "D9"
         InterruptPin: "D2"

Create Connection to CAN Channel on MCP2515 with Specified Bus Speed

Create an arduino object

arduinoObj = arduino('COM24', 'Uno', 'Libraries', 'CAN')
arduinoObj = 

  arduino with properties:
 
                  Port: 'COM24'
                 Board: 'Uno'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11'}
   AvailableAnalogPins: {'A0-A5'}
    AvailableI2CBusIDs: [0]
             Libraries: {'CAN', 'SPI'}
Show all properties

Create a connection to the CAN channel on MCP2515

ch = canChannel(arduinoObj, "MCP2515", "D10", "D2", "OscillatorFrequency", 16e6, "BusSpeed", 500e3)
ch =
 
  canChannel with properties:
 
               Device: 'MCP2515'
         ProtocolMode: 'CAN'
             BusSpeed: 500000
             Database: []
Show all properties
  
               Device: "MCP2515"
         ProtocolMode: "CAN"
             BusSpeed: 500000
             Database: []

  OscillatorFrequency: 1.600000e+07
        ChipSelectPin: "D10"
         InterruptPin: "D2"

See Also

| | | (Vehicle Network Toolbox)

Introduced in R2020a