PCAN Basic API

Interface to Peak-System’s PCAN-Basic API.

Configuration

Here is an example configuration file for using PCAN-USB:

[default]
interface = pcan
channel = PCAN_USBBUS1
listen_only = True
bitrate = 500000
channel (default "PCAN_USBBUS1")

CAN interface name. Valid channel values:

PCAN_ISABUSx
PCAN_DNGBUSx
PCAN_PCIBUSx
PCAN_USBBUSx
PCAN_PCCBUSx
PCAN_LANBUSx

Where x should be replaced with the desired channel number starting at 1.

listen_only (default None: leave the channel’s mode unchanged)

Open the channel in listen-only mode.

state (default None)

BusState of the channel. Deprecated since 4.7.0, use listen_only instead. If listen_only is also given with a contradicting value, ValueError is raised.

bitrate (default 500000)

Channel bitrate

Linux installation

Beginning with version 3.4, Linux kernels support the PCAN adapters natively via SocketCAN, refer to: PCAN.

Timestamps

Timestamps are relative to the Unix epoch. On macOS and Cygwin, this requires the uptime package (pip install python-can[pcan]); without it, timestamps are relative to system startup.

On macOS, PCBUSB 0.13 timestamps have been reported to lag the wall clock by the total time the Mac has slept since boot, including sleep before the bus was opened, see #2092 and PCBUSB issue #26.

Bus

class can.interfaces.pcan.PcanBus(*args, **kwargs)[source]

A PCAN USB interface to CAN.

On top of the usual Bus methods provided, the PCAN interface includes the flash() and status() methods.

Parameters:
  • channel (str) – The can interface name. An example would be ‘PCAN_USBBUS1’. Alternatively the value can be an int with the numerical value. Default is ‘PCAN_USBBUS1’

  • device_id (int) – Select the PCAN interface based on its ID. The device ID is a 8/32bit value that can be configured for each PCAN device. If you set the device_id parameter, it takes precedence over the channel parameter. The constructor searches all connected interfaces and initializes the first one that matches the parameter value. If no device is found, an exception is raised.

  • state (BusState | None) –

    BusState of the channel. If listen_only is also given with a contradicting value, ValueError is raised.

    Deprecated since version 4.7.0: Use listen_only instead.

  • timing (BitTiming | BitTimingFd | None) – An instance of BitTiming or BitTimingFd to specify the bit timing parameters for the PCAN interface. If this parameter is provided, it takes precedence over all other timing-related parameters. If this parameter is not provided, the bit timing parameters can be specified using the bitrate parameter for standard CAN or the fd, f_clock, f_clock_mhz, nom_brp, nom_tseg1, nom_tseg2, nom_sjw, data_brp, data_tseg1, data_tseg2, and data_sjw parameters for CAN FD. Note that the f_clock value of the timing instance must be 8_000_000 for standard CAN or any of the following values for CAN FD: 20_000_000, 24_000_000, 30_000_000, 40_000_000, 60_000_000, 80_000_000.

  • bitrate (int) – Bitrate of channel in bit/s. Default is 500 kbit/s. Ignored if using CanFD.

  • receive_own_messages (bool) – Enable self-reception of sent messages.

  • listen_only (bool | None) – Open the channel in listen-only mode. In listen-only mode, state is BusState.PASSIVE and send() raises CanOperationError. The mode can be switched at runtime with listen_only. If None (the default), the listen-only mode of the channel is left unchanged. If listen-only mode cannot be set, CanInitializationError is raised; if it cannot be switched off, e.g. on PCAN-LAN devices, the channel opens anyway and a warning is logged.

  • fd (bool) – Should the Bus be initialized in CAN-FD mode.

  • f_clock (int) – Clock rate in Hz. Any of the following: 20000000, 24000000, 30000000, 40000000, 60000000, 80000000. Ignored if not using CAN-FD. Pass either f_clock or f_clock_mhz.

  • f_clock_mhz (int) – Clock rate in MHz. Any of the following: 20, 24, 30, 40, 60, 80. Ignored if not using CAN-FD. Pass either f_clock or f_clock_mhz.

  • nom_brp (int) – Clock prescaler for nominal time quantum. In the range (1..1024) Ignored if not using CAN-FD.

  • nom_tseg1 (int) – Time segment 1 for nominal bit rate, that is, the number of quanta from (but not including) the Sync Segment to the sampling point. In the range (1..256). Ignored if not using CAN-FD.

  • nom_tseg2 (int) – Time segment 2 for nominal bit rate, that is, the number of quanta from the sampling point to the end of the bit. In the range (1..128). Ignored if not using CAN-FD.

  • nom_sjw (int) – Synchronization Jump Width for nominal bit rate. Decides the maximum number of time quanta that the controller can resynchronize every bit. In the range (1..128). Ignored if not using CAN-FD.

  • data_brp (int) – Clock prescaler for fast data time quantum. In the range (1..1024) Ignored if not using CAN-FD.

  • data_tseg1 (int) – Time segment 1 for fast data bit rate, that is, the number of quanta from (but not including) the Sync Segment to the sampling point. In the range (1..32). Ignored if not using CAN-FD.

  • data_tseg2 (int) – Time segment 2 for fast data bit rate, that is, the number of quanta from the sampling point to the end of the bit. In the range (1..16). Ignored if not using CAN-FD.

  • data_sjw (int) – Synchronization Jump Width for fast data bit rate. Decides the maximum number of time quanta that the controller can resynchronize every bit. In the range (1..16). Ignored if not using CAN-FD.

  • auto_reset (bool) – Enable automatic recovery in bus off scenario. Resetting the driver takes ~500ms during which it will not be responsive.

  • kwargs (Any)

flash(flash)[source]

Turn on or off flashing of the device’s LED for physical identification purposes.

get_device_number()[source]

Return the PCAN device number.

Return type:

int

Returns:

PCAN device number

property listen_only: bool

Whether this bus instance is in listen-only mode.

Interfaces that do not support listen-only mode return False. If the bus was created with listen_only=None, this property reports False, meaning that listen-only mode was not requested; the mode of the channel is not read back from the driver.

Interfaces that can switch the mode at runtime allow setting this property to True or False.

Raises:

NotImplementedError – When set, if the interface cannot switch listen-only mode at runtime.

reset()[source]

Command the PCAN driver to reset the bus after an error.

send(msg, timeout=None)[source]

Transmit a message to the CAN bus.

Override this method to enable the transmit path.

Parameters:
  • msg (Message) – A message object.

  • timeout – If > 0, wait up to this many seconds for message to be ACK’ed or for transmit queue to be ready depending on driver implementation. If timeout is exceeded, an exception will be raised. Might not be supported by all interfaces. None blocks indefinitely.

Raises:

CanOperationError – If an error occurred while sending, or if the bus was opened in listen-only mode without local loopback

set_device_number(device_number)[source]

Set the PCAN device number.

Parameters:

device_number – new PCAN device number

Return type:

bool

Returns:

True if device number set successfully

shutdown()[source]

Called to carry out any interface specific cleanup required in shutting down a bus.

This method can be safely called multiple times.

property state

Return the current state of the channel.

Deprecated since version 4.7.0: Setting the state to switch listen-only mode is deprecated. Use listen_only instead.

status()[source]

Query the PCAN bus status.

Return type:

int

Returns:

The status code. See values in basic.PCAN_ERROR_

status_is_ok()[source]

Convenience method to check that the bus status is OK

status_string()[source]

Query the PCAN bus status.

Returns:

The status description, if any was found.

Return type:

str | None