Channel

openChannel()

canlib.canlib.openChannel(channel, flags=0, bitrate=None, data_bitrate=None)[source]

Open CAN channel

Retrieves a Channel object for the given CANlib channel number using the supplied flags.

Example usage:

>>> bitrate = canlib.busparams.BusParamsTq(
... tq=40,
... phase1=5,
... phase2=6,
... sjw=4,
... prescaler=2,
... prop=28,
... )
>>> data_bitrate = canlib.busparams.BusParamsTq(
... tq=40,
... phase1=31,
... phase2=8,
... sjw=2,
... prescaler=2,
... )
>>> ch = canlib.openChannel(
... channel=0,
... flags=canlib.Open.CAN_FD,
... bitrate=bitrate,
... data_bitrate=data_bitrate,
... )

Note

If both bitrate and data_bitrate is given, both must be of the same type, i.e. either BusParamsTq or BITRATE flags. It is not supported to mix the two.

Parameters
  • channel (int) – CANlib channel number

  • flags (int) – Flags, a combination of the Open flag values. Default is zero, i.e. no flags.

  • bitrate (BusParamsTq or Bitrate or BitrateFD) – The desired bitrate. If the bitrate is not a BusParamsTq, the predefined Bitrate values are used for classic CAN and BitrateFD values are used for CAN FD, e.g. canlib.Bitrate.BITRATE_1M and canlib.BitrateFD.BITRATE_1M_80P. For CAN FD, this parameter gives the arbitration bitrate.

  • data_bitrate (BusParamsTq or BitrateFD) – The desired data bitrate for CAN FD. If not BusParamsTq is given, the predefined BitrateFD values are used, e.g. canlib.BitrateFD.BITRATE_500K_80p. This parameter is only used when opening a CAN FD channel.

Returns

A Channel object created with channel and flags

New in version 1.6: The bitrate and data_bitrate arguments was added.

Changed in version 1.16: The bitrate and data_bitrate arguments now accept BusParamsTq objects.

Changed in version 1.17: Added support for Bitrate and BitrateFD enumerations.

ErrorCounters

class canlib.canlib.channel.ErrorCounters(tx, rx, overrun)

Error counters returned by Channel.read_error_counters.

property overrun

Alias for field number 2

property rx

Alias for field number 1

property tx

Alias for field number 0

Channel

class canlib.canlib.Channel(channel_number, flags=0)[source]

Helper class that represents a CANlib channel.

This class wraps the canlib class and tries to implement a more Pythonic interface to CANlib.

Channels are automatically closed on garbage collection, and can also be used as context managers in which case they close as soon as the context exits.

Variables

envvar (EnvVar) – Used to access t program environment variables

announceIdentityEx(item, value)[source]

AnnounceIdentityEx function is used by certain OEM applications.

New in version 1.17.

bitrate_to_BusParamsTq(freq_a, freq_d=None)[source]

Calculate bus parameters based on predefined frequencies.

This function uses the Bitrate and BitrateFD values to create bus parameters for use with the new bus parameter API.

Parameters
  • freq_a (Bitrate or BitrateFD) – The bitrate for classic CAN channels specified as a Bitrate, or the arbitration bitrate for CAN FD channels specified as a BitrateFD.

  • freq_d (BitrateFD) – Data bitrate for CAN FD channels.

Returns

(nominal, None) for classic CAN, where nominal is a BusParamsTq object.

(nominal, data) for CAN FD, where both nominal and data are BusParamsTq objects, representing the arbitration bitrate and the data bitrate, respectively.

New in version 1.17.

busOff()[source]

Takes the specified channel off-bus.

Closes the channel associated with the handle. If no other threads are using the CAN circuit, it is taken off bus. The handle can not be used for further references to the channel.

busOn()[source]

Takes the specified channel on-bus.

If you are using multiple handles to the same physical channel, for example if you are implementing a multithreaded application, you must call busOn() once for each handle.

canAccept(envelope, flag)[source]

Set acceptance filters mask or code.

This routine sets the message acceptance filters on a CAN channel.

Setting flag to AcceptFilterFlag.NULL_MASK (0) removes the filter.

Note that not all CAN boards support different masks for standard and extended CAN identifiers.

Parameters
canSetAcceptanceFilter(code, mask, is_extended=False)[source]

Set message acceptance filter.

This routine sets the message acceptance filters on a CAN channel. The message is accepted if id AND mask == code (this is actually imlepemented as if ((code XOR id) AND mask) == 0).

Using standard 11-bit CAN identifiers and setting

  • mask = 0x7f0,

  • code = 0x080

accepts CAN messages with standard id 0x080 to 0x08f.

Setting the mask to canFILTER_NULL_MASK (0) removes the filter.

Note that not all CAN boards support different masks for standard and extended CAN identifiers.

Parameters
  • mask (int) – A bit mask that indicates relevant bits with ‘1’.

  • code (int) – The expected state of the masked bits.

  • is_extended (bool) – If True, both mask and code applies to 29-bit CAN identifiers.

property channel_data

canGetHandleData helper object for this channel

See the documentation for ChannelData/HandleData for how it can be used to perform all functionality of the C function canGetHandleData.

New in version 1.13.

Type

HandleData

close()[source]

Close CANlib channel

Closes the channel associated with the handle. If no other threads are using the CAN circuit, it is taken off bus.

Note

It is normally not necessary to call this function directly, as the internal handle is automatically closed when the Channel object is garbage collected.

device()[source]

Get a Device object from the current channel

Returns

Device – Device used by this channel,

New in version 1.16.

fileCopyFromDevice(deviceFileName, hostFileName=None)[source]

Copy an arbitrary file from the device to the host.

Parameters
  • deviceFileName (str) – The device file name.

  • hostFileName (str, optional) – The target host file name. Defaults to deviceFileName.

fileCopyToDevice(hostFileName, deviceFileName=None)[source]

Copy an arbitrary file from the host to the () isdevice.

The filename must adhere to the FAT ‘8.3’ naming standard, max 8 characters - a dot - max 3 characters.

Parameters
  • hostFileName (str) – The target host file name.

  • deviceFileName (str, optional) – The device file name. Defaults to the same as hostFileName.

fileDelete(deviceFileName)[source]

Delete file from device.

fileDiskFormat()[source]

Format the disk on the device, not supported by all devices.

New in version 1.11.

fileGetCount()[source]

Get the number of files on the device.

Returns

int – The number of files.

fileGetName(fileNo)[source]

Get the name of the file with the supplied number.

Parameters

fileNo (int) – The number of the file.

Returns

str – The name of the file.

flashLeds(action, timeout_ms)[source]

Turn Leds on or off.

Parameters
  • action (int) – One of LEDAction, defining which LED to turn on or off.

  • timeout_ms (int) – Specifies the time, in milliseconds, during which the action is to be carried out. When the timeout expires, the LED(s) will return to its ordinary function.

getBusOutputControl()[source]

Get driver type

This function retrieves the current CAN controller driver type. This corresponds loosely to the bus output control register in the CAN controller, hence the name of this function.

Note

Not all CAN driver types are supported on all cards.

Returns

drivertype (canlib.Driver) – Driver type to set.

New in version 1.11.

getBusParams()[source]

Get bus timing parameters for classic CAN.

This function retrieves the current bus parameters for the specified channel.

Returns: A tuple containing:

freq: Bitrate in bit/s.

tseg1: Number of quanta from but not including the Sync Segment to the sampling point.

tseg2: Number of quanta from the sampling point to the end of the bit.

sjw: The Synchronization Jump Width, can be 1, 2, 3, or 4.

noSamp: The number of sampling points, only 1 is supported.

syncmode: Unsupported, always read as zero.

getBusParamsFd()[source]

Get bus timing parameters for BRS in CAN FD.

This function retrieves the bus current timing parameters used in BRS (Bit rate switch) mode for the current CANlib channel.

The library provides default values for tseg1_brs, tseg2_brs and sjw_brs when freq is a BitrateFD value.

If freq is any other value, no default values are supplied by the library.

For finding out whether a channel is opened using CAN_FD or CAN_FD_NONISO flags, see is_can_fd()

Returns: A tuple containing:

freq_brs: Bitrate in bit/s.

tseg1_brs: Number of quanta from (but not including) the Sync Segment to the sampling point.

tseg2_brs: Number of quanta from the sampling point to the end of the bit.

sjw_brs: The Synchronization Jump Width.

getChannelData_CardNumber()[source]

Deprecated function

Deprecated since version 1.5: Use ChannelData; ChannelData.card_number

getChannelData_Chan_No_On_Card()[source]

Deprecated function

Deprecated since version 1.5: Use ChannelData; ChannelData.chan_no_on_card

getChannelData_Cust_Name()[source]

Deprecated function

Deprecated since version 1.5: Use ChannelData; ChannelData.custom_name

getChannelData_DriverName()[source]

Deprecated function

Deprecated since version 1.5: Use ChannelData; ChannelData.driver_name

getChannelData_EAN()[source]

Deprecated function

Deprecated since version 1.5: Use ChannelData; ChannelData.card_upc_no

getChannelData_EAN_short()[source]
getChannelData_Firmware()[source]

Deprecated function

Deprecated since version 1.5: Use ChannelData; ChannelData.card_firmware_rev

getChannelData_Name()[source]

Deprecated function

Deprecated since version 1.5: Use ChannelData; ChannelData.device_name

getChannelData_Serial()[source]

Deprecated function

Deprecated since version 1.5: Use ChannelData; ChannelData.card_serial_no

get_bus_params_tq()[source]

Get bus timing parameters, in time quanta

This function retrieves the current bus parameters for the specified channel. Only the returned nominal parameter is valid when classic CAN is in use.

Returns: A tuple containing:

nominal: (BusParamsTq) Nominal bus timing parameters, also used in classic CAN.

data: (BusParamsTq) Bus timing parameters for data rate in CAN FD.

New in version 1.16.

get_bus_statistics()[source]

Return bus statistics.

Returns

structures.CanBusStatistics

get_io_pin(index)[source]

Return I/O pin using index

Returns

iopin.IoPin – io pin object for index (Any of iopin.AnalogIn, iopin.DigitalOut etc)

New in version 1.8.

ioCtl_flush_rx_buffer()[source]

Deprecated function

Deprecated since version 1.5: Use IOControl; Channel.iocontrol.flush_rx_buffer().

ioCtl_get_report_access_errors()[source]

Deprecated function

Deprecated since version 1.5: Use IOControl; Channel.iocontrol.report_access_errors

ioCtl_set_report_access_errors(on=0)[source]

Deprecated function

Deprecated since version 1.5: Use IOControl; Channel.iocontrol.report_access_errors = on

ioCtl_set_timer_scale(scale)[source]

Deprecated function

Deprecated since version 1.5: Use IOControl; Channel.iocontrol.timer_scale = scale

io_confirm_config()[source]

Confirm current I/O configuration

It is required to confirm a configuration by calling this function before accessing any I/O pins value.

New in version 1.8.

io_pins()[source]

Generator that returns all I/O pins one by one

Returns object depending on pin type and direction: iopin.AnalogIn, iopin.AnalogOut, iopin.DigitalIn, iopin.DigitalOut or iopin.Relay.

New in version 1.8.

property iocontrol

canIoCtl helper object for this channel

See the documentation for IOControl for how it can be used to perform all functionality of the C function canIoCtl.

Example usage:

>>> from canlib import canlib
>>> ch = canlib.openChannel(channel=0)
>>> ch.iocontrol.timer_scale
1000
>>> ch.iocontrol.local_txecho = False
Type

IOControl

is_can_fd()[source]

Return True if the channel has been opened with the CAN_FD or CAN_FD_NONISO flags.

Returns

True if CAN FD, False otherwise.

kvDeviceGetMode()[source]

Read the current device’s mode.

Note

The mode is device specific, which means that not all modes are implemented in all products.

Returns

int – One of DeviceMode, indicating which mode is in use.

kvDeviceSetMode(mode)[source]

Set the current device’s mode.

Note

The mode is device specific, which means that not all modes are implemented in all products.

Parameters

mode (int) – One of DeviceMode, defining which mode to use.

number_of_io_pins()[source]

Return the total number of available I/O pins

New in version 1.8.

read(timeout=0)[source]

Read a CAN message and metadata.

Reads a message from the receive buffer. If no message is available, the function waits until a message arrives or a timeout occurs.

Note

If you are using the same channel via multiple handles, the default behaviour is that the different handles will “hear” each other just as if each handle referred to a channel of its own. If you open, say, channel 0 from thread A and thread B and then send a message from thread A, it will be “received” by thread B.

This behaviour can be changed by setting local_txecho to False (using IOControl):

>>> from canlib import canlib
>>> ch = canlib.openChannel(channel=0)
>>> ch.iocontrol.local_txecho = False
Parameters

timeout (int) – Timeout in milliseconds, -1 gives an infinite timeout.

Returns

canlib.Frame

Raises

CanNoMsg – No CAN message is currently available.

readDeviceCustomerData(userNumber=100, itemNumber=0)[source]

Read customer data stored in device

readSpecificSkip(id_)[source]

Read a message with specified identifier

Reads a message with a specified identifier from the receive buffer. Any preceding message not matching the specified identifier will be removed in the receive buffer. If no message with the specified identifier is available, the function returns immediately with an error code.

Returns

canlib.Frame

readStatus()[source]

Return status for the current channel

Returns the latest status reported by the hardware in a combination of the flags Stat (bus on/error passive + status etc).

Returns

canlib.canlib.Stat

readSyncSpecific(id_, timeout=0)[source]

Wait until the receive queue contains a message with the specified id

readTimer()[source]

Read the hardware clock on the specified device

Returns the time value.

read_error_counters()[source]

Read the error counters of the CAN controller

Returns the latest known values of the error counters in the specified circuit. If the error counters change values precisely when read_error_counters is called, it may not be reflected in the returned result.

Use clear_error_counters via Channel.iocontrol to clear the counters.

Returns

The returned tuple is a (rx, tx, overrun) named tuple of –

  1. rx (int): Receive error counter

  2. tx (int): Transmit error counter

  3. overrun (int): Number of overrun errors.

New in version 1.11.

requestChipStatus()[source]

Request chip status messages

Requests that the hardware report the chip status (bus on/error passive status etc.) to the driver. The chip status can later be retrieved using canlib.Channel.readStatus.

Note

The requestChipStatus function is asynchronous, that is, it completes before the answer is returned from the hardware. The time between a call to requestChipStatus and the point in time where the chip status is actually available via a call to Channel.readStatus is not defined. The Channel.readStatus always returns the latest data reported by the hardware.

scriptEnvvarClose(envHandle)[source]

Low level function to close an Envvar

This should normally not be used directly, instead opening and closing of an envvar is automatically done when accessing via the EnvVar class through Channel.envvar

scriptEnvvarGetData(envHandle, envSize)[source]
scriptEnvvarGetFloat(envHandle)[source]

Low level function to read an Envvar of type float

This should normally not be used directly, instead set and get the value of an envvar using the EnvVar class through Channel.envvar

scriptEnvvarGetInt(envHandle)[source]

Low level function to read an Envvar of type int

This should normally not be used directly, instead set and get the value of an envvar using the EnvVar class through Channel.envvar

scriptEnvvarOpen(name)[source]

Low level function to open an Envvar

This should normally not be used directly, instead opening and closing of an envvar is automatically done when accessing via the EnvVar class through Channel.envvar

scriptEnvvarSetData(envHandle, value, envSize)[source]
scriptEnvvarSetFloat(envHandle, value)[source]

Low level function to set an Envvar of type float

This should normally not be used directly, instead set and get the value of an envvar using the EnvVar class through Channel.envvar

scriptEnvvarSetInt(envHandle, value)[source]

Low level function to set an Envvar of type int

This should normally not be used directly, instead set and get the value of an envvar using the EnvVar class through Channel.envvar

scriptGetText()[source]

Read text from subscribed script slots

Text-subscriptions must first be set up with Channel.scriptRequestText.

Returns

ScriptText

Raises

CanNoMsg – No more text is currently available.

New in version 1.7.

scriptLoadFile(slot, filePathOnPC)[source]

Load compiled script file from host(PC)

Loads a compiled script file (.txe) stored on the host (PC) into a script slot on the device. The scripts default channel will be the same channel used when this Channel object was created.

Parameters
  • slot (int) – slot containing the running script we want to stop.

  • filePathOnPC (str) – Path to compiled script (.txe) to load.

scriptLoadFileOnDevice(slot, localFile)[source]

Load compiled, locally stored, script file

Loads a compiled script file (.txe) stored locally on the device (SD card) into a script slot on the device. The scripts default channel will be the same channel used when this Channel object was created.

Parameters
  • slot (int) – slot containing the running script we want to stop.

  • localFile (str) – Name of compiled script (.txe) to load.

scriptRequestText(slot, request=ScriptRequest.SUBSCRIBE)[source]

Set up a printf subscription to a selected script slot

Parameters
  • slot (int) – The script slot to subscribe/unsubscribe from.

  • request (ScriptRequest) – Whether to subscribe or unsubscribe.

Text printed with printf() by a t-program that you are subscribed to is saved and can be retrieved with Channel.scriptGetText.

New in version 1.7.

scriptSendEvent(slotNo=0, eventType=1, eventNo=None, data=0)[source]

Send specified event to specified t script

Send an event with given type, event number, and associated data to a script running in a specific slot.

scriptStart(slot)[source]

Start previously loaded script in specified slot

scriptStatus(slot)[source]

Retreives t program status for selected slot

Parameters

slot (int) – Slot number to be queried

Returns

canlib.ScriptStatus

New in version 1.6.

scriptStop(slot, mode=ScriptStop.NORMAL)[source]

Stop script running in specified slot

Parameters
  • slot (int) – slot containing the running script we want to stop.

  • mode (canlib.ScriptStop) – Default mode is canlib.ScriptStop.NORMAL.

scriptUnload(slot)[source]

Unload previously stopped script in specified slot

script_envvar_get_data(envHandle, len, start=0)[source]

Low level function to read a slice of an Envvar of type char

This should normally not be used directly, instead set and get the value of an envvar using the EnvVar class through Channel.envvar

New in version 1.10.

script_envvar_set_data(envHandle, value, len, start=0)[source]

Low level function to write a slice of an Envvar of type char

This should normally not be used directly, instead set and get the value of an envvar using the EnvVar class through Channel.envvar

value needs to be a bytes-like object or list

New in version 1.10.

setBusOutputControl(drivertype=Driver.NORMAL)[source]

Set driver type

This function sets the driver type for a CAN controller to e.g. silent mode. This corresponds loosely to the bus output control register in the CAN controller, hence the name of this function.

Note

Not all CAN driver types are supported on all cards.

Parameters

drivertype (canlib.Driver) – Driver type to set.

setBusParams(freq, tseg1=0, tseg2=0, sjw=0, noSamp=0, syncmode=0)[source]

Set bus timing parameters for classic CAN

This function sets the bus timing parameters for the specified CAN controller.

The library provides default values for tseg1, tseg2, sjw and noSamp when freq is a Bitrate, e.g. Bitrate.BITRATE_1M.

If freq is any other value, no default values are supplied by the library.

If you are using multiple handles to the same physical channel, for example if you are implementing a multithreaded application, you must call busOn() once for each handle. The same applies to busOff() - the physical channel will not go off bus until the last handle to the channel goes off bus.

Parameters
  • freq – Bitrate in bit/s.

  • tseg1 – Number of quanta from (but not including) the Sync Segment to the sampling point.

  • tseg2 – Number of quanta from the sampling point to the end of the bit.

  • sjw – The Synchronization Jump Width, can be 1, 2, 3, or 4.

  • nosamp – The number of sampling points, only 1 is supported.

  • syncMode – Unsupported and ignored.

Changed in version 1.17: Now accepts Bitrate enumerations.

setBusParamsFd(freq_brs, tseg1_brs=0, tseg2_brs=0, sjw_brs=0)[source]

Set bus timing parameters for BRS in CAN FD

This function sets the bus timing parameters used in BRS (Bit rate switch) mode for the current CANlib channel.

The library provides default values for tseg1_brs, tseg2_brs and sjw_brs when freq is a BitrateFD value, e.g. BitrateFD.BITRATE_1M_80P.

If freq is any other value, no default values are supplied by the library.

For finding out whether a channel is opened using CAN_FD or CAN_FD_NONISO flags, see is_can_fd

Parameters
  • freq_brs – Bitrate in bit/s.

  • tseg1_brs – Number of quanta from (but not including) the Sync Segment to the sampling point.

  • tseg2_brs – Number of quanta from the sampling point to the end of the bit.

  • sjw_brs – The Synchronization Jump Width.

Changed in version 1.17: Now accepts BitrateFD enumerations.

set_bus_params_tq(nominal, data=None)[source]

Set bus timing parameters, using time quanta

This function sets the bus timing parameters for the specified CAN controller. When setting bus parameters for CAN FD, both nominal and data must be given.

If you are using multiple handles to the same physical channel, for example if you are implementing a multithreaded application, you must call busOff() once for each handle. The physical channel will not go off bus until the last handle to the channel goes off bus. The same applies to busOn().

Parameters
  • nominal (BusParamsTq) – Nominal Bus timing parameters, also used for classic CAN

  • data (BusParamsTq) – Bus timing parameters for data rate in CAN FD.

New in version 1.16.

set_callback(function, event, context=None)[source]

Register callback function

This will register a callback function which is called when certain events occur. You can register at most one callback function per handle at any time.

Note:

The callback function is called in the context of a high-priority thread created by CANlib. You should take precaution not to do any time consuming tasks in the callback.

Small example of usage:

# Declare callback function
def callback_func(hnd, context, event):
    event = canlib.Notify(event)

    # The nonlocal statement causes the listed identifiers to refer
    # to previously bound variables in the nearest enclosing scope
    # excluding globals.
    nonlocal callback_has_been_called
    print("Callback called, context:{}, event:{!r}".format(context, event))
    # Notify the main program by setting the flag
    callback_has_been_called = True

# setup communication variable and callback
callback_has_been_called = False
callback = canlib.dll.KVCALLBACK_T(callback_func)

with canlib.openChannel(0) as ch:
ch.set_callback(callback, context=121, event=canlib.Notify.BUSONOFF)
# trigger the callback
ch.busOn()
# do something else
time.sleep(0.5)
# Verify that the callback was triggered
assert callback_has_been_called

Note:

It is very important to make sure that you keep a reference to the callback type (callback in the sample above) for as long as any C library might call it. If it gets deleted by the garbage collector, calling it from C is likely to either cause a segfault or maybe even interpret random memory as machine language.

Parameters
  • function (KVCALLBACK_T) – A ctypes wrapped Python function

  • event (Notify) – A combination of flags to indicate what events to trigger on

New in version 1.7.

write(frame=None, *args, **kwargs)[source]

Send a CAN message.

This function sends a Frame object as a CAN message. Note that the message has been queued for transmission when this calls return. It has not necessarily been sent.

Note

If you are using the same channel via multiple handles, the default behaviour is that the different handles will “hear” each other just as if each handle referred to a channel of its own. If you open, say, channel 0 from thread A and thread B and then send a message from thread A, it will be “received” by thread B.

This behaviour can be changed by setting local_txecho to False (using IOControl):

>>> from canlib import canlib
>>> ch = canlib.openChannel(channel=0)
>>> ch.iocontrol.local_txecho = False

Also see Channel.write_raw for sending messages without constructing canlib.Frame objects.

Deprecated since version 1.5: Sending the canlib.Frame contents as separate arguments; this functionality has been taken over by write_raw.

Parameters

frame (canlib.Frame)

writeSync(timeout)[source]

Wait for queued messages to be sent

Waits until all CAN messages for the specified handle are sent, or the timeout period expires.

Parameters

timeout (int) – The timeout in milliseconds, None or 0xFFFFFFFF for an infinite timeout.

writeWait(frame, timeout, *args, **kwargs)[source]

Sends a CAN message and waits for it to be sent.

This function sends a CAN message. It returns when the message is sent, or the timeout expires. This is a convenience function that combines write() and writeSync().

Deprecated since version 1.5: Sending the Frame contents as separate arguments; this functionality has been taken over by writeWait_raw.

Parameters
  • frame (canlib.Frame) – Frame containing the CAN data to be sent

  • timeout (int) – The timeout, in milliseconds. 0xFFFFFFFF gives an infinite timeout.

writeWait_raw(id_, msg, flag=0, dlc=0, timeout=0)[source]

Sends a CAN message and waits for it to be sent.

This function sends a CAN message. It returns when the message is sent, or the timeout expires. This is a convenience function that combines write() and writeSync().

Parameters
  • id_ – The identifier of the CAN message to send.

  • msg – An array or bytearray of the message data

  • flag – A combination of canlib.canlib.MessageFlag. Use this parameter e.g. to send extended (29-bit) frames.

  • dlc – The length of the message in bytes. For Classic CAN dlc can be at most 8, unless canlib.canlib.Open.ACCEPT_LARGE_DLC is used. For CAN FD dlc can be one of the following 0-8, 12, 16, 20, 24, 32, 48, 64. Optional, if omitted, dlc is calculated from the msg array.

  • timeout – The timeout, in milliseconds. 0xFFFFFFFF gives an infinite timeout.

write_raw(id_, msg, flag=0, dlc=None)[source]

Send a CAN message

See docstring of Channel.write for general information about sending CAN messages.

The variable name id (as used by canlib) is a built-in function in Python, so the name id_ is used instead.

Parameters
  • id_ – The identifier of the CAN message to send.

  • msg – An array or bytearray of the message data

  • flag – A combination of MessageFlag. Use this parameter e.g. to send extended (29-bit) frames.

  • dlc – The length of the message in bytes. For Classic CAN dlc can be at most 8, unless Open.ACCEPT_LARGE_DLC is used. For CAN FD dlc can be one of the following 0-8, 12, 16, 20, 24, 32, 48, 64. Optional, if omitted, dlc is calculated from the msg array.

CanBusStatistics

class canlib.canlib.structures.CanBusStatistics[source]

Result from reading bus statistics using canlib.canlib.Channel.get_bus_statistics.

Variables
  • busLoad (int) – The bus load, expressed as an integer in the interval 0 - 10000 representing 0.00% - 100.00% bus load.

  • errFrame (int) – Number of error frames.

  • extData (int) – Number of received extended (29-bit identifiers) data frames.

  • extRemote (int) – Number of received extended (29-bit identifiers) remote frames.

  • overruns (int) – The number of overruns detected by the hardware, firmware or driver.

  • stdData (int) – Number of received standard (11-bit identifiers) data frames.

  • stdRemote (int) – Number of received standard (11-bit identifiers) remote frames.