Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# Configuration

Usually this library is used with a particular CAN interface, this can be specified in code, read from configuration files or environment variables.

See `can.util.load_config()` for implementation.

## In Code

The `can` object exposes an `rc` dictionary which can be used to set the interface and channel.

    import can
    can.rc['interface'] = 'socketcan'
    can.rc['channel'] = 'vcan0'
    can.rc['bitrate'] = 500000
    from can.interface import Bus

    bus = Bus()

You can also specify the interface and channel for each Bus instance:

    import can

    bus = can.interface.Bus(interface='socketcan', channel='vcan0', bitrate=500000)

## Configuration File

On Linux and macOS systems the config file is searched in the following paths:

1. `~/can.conf`

2. `/etc/can.conf`

3. `$HOME/.can`

4. `$HOME/.canrc`

On Windows systems the config file is searched in the following paths:

1. `%USERPROFILE%/can.conf`

2. `can.ini` (current working directory)

3. `%APPDATA%/can.ini`

The configuration file sets the default interface and channel:

    [default]
    interface = <the name of the interface to use>
    channel = <the channel to use by default>
    bitrate = <the bitrate in bits/s to use by default>

The configuration can also contain additional sections (or context):

    [default]
    interface = <the name of the interface to use>
    channel = <the channel to use by default>
    bitrate = <the bitrate in bits/s to use by default>

    [HS]
    # All the values from the 'default' section are inherited
    channel = <the channel to use>
    bitrate = <the bitrate in bits/s to use. i.e. 500000>

    [MS]
    # All the values from the 'default' section are inherited
    channel = <the channel to use>
    bitrate = <the bitrate in bits/s to use. i.e. 125000>

    from can.interface import Bus

    hs_bus = Bus(context='HS')
    ms_bus = Bus(context='MS')

## Environment Variables

Configuration can be pulled from these environmental variables:

> - CAN_INTERFACE

> - CAN_CHANNEL

> - CAN_BITRATE

> - CAN_CONFIG

The `CAN_CONFIG` environment variable allows to set any bus configuration using JSON.

For example:

`CAN_INTERFACE=socketcan CAN_CONFIG={"receive_own_messages": true, "fd": true}`

## Interface Names

Lookup table of interface names:

Name | Documentation
--- | ---
`"canalystii"` | CANalyst-II
`"cantact"` | CANtact CAN Interface
`"etas"` | ETAS
`"gs_usb"` | Geschwister Schneider and candleLight
`"iscan"` | isCAN
`"ixxat"` | IXXAT Virtual Communication Interface
`"kvaser"` | Kvaser’s CANLIB
`"neousys"` | Neousys CAN Interface
`"neovi"` | Intrepid Control Systems neoVI
`"nican"` | National Instruments NI-CAN
`"nixnet"` | National Instruments NI-XNET
`"pcan"` | PCAN Basic API
`"robotell"` | Robotell CAN-USB interface
`"seeedstudio"` | Seeed Studio USB-CAN Analyzer
`"serial"` | CAN over Serial
`"slcan"` | CAN over Serial / SLCAN
`"socketcan"` | SocketCAN
`"socketcand"` | socketcand Interface
`"systec"` | SYSTEC interface
`"udp_multicast"` | Multicast IP Interface
`"usb2can"` | USB2CAN Interface
`"vector"` | Vector
`"virtual"` | Virtual

Additional interface types can be added via the Plugin Interface.