5. cc_pcan_can

5.1. Can Communication Channel using PCAN hardware

module

cc_pcan_can

synopsis

CChannel implementation for CAN(fd) using PCAN API from python-can

class pykiso.lib.connectors.cc_pcan_can.CCPCanCan(interface='pcan', channel='PCAN_USBBUS1', state='ACTIVE', trace_path='', trace_size=10, bitrate=500000, is_fd=True, enable_brs=False, f_clock_mhz=80, nom_brp=2, nom_tseg1=63, nom_tseg2=16, nom_sjw=16, data_brp=4, data_tseg1=7, data_tseg2=2, data_sjw=2, is_extended_id=False, remote_id=None, can_filters=None, logging_activated=True, bus_error_warning_filter=False, **kwargs)[source]

CAN FD channel-adapter.

Initialize can channel settings.

Parameters
  • interface (str) – python-can interface modules used

  • channel (str) – the can interface name

  • state (str) – BusState of the channel

  • trace_path (str) – path to write the trace

  • trace_size (int) – maximum size of the trace (in MB)

  • bitrate (int) – Bitrate of channel in bit/s,ignored if using CanFD

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

  • enable_brs (bool) – sets the bitrate_switch flag to use higher transmission speed

  • f_clock_mhz (int) – Clock rate in MHz

  • nom_brp (int) – Clock prescaler for nominal time quantum

  • nom_tseg1 (int) – Time segment 1 for nominal bit rate, that is, the number of quanta from the Sync Segment to the sampling point

  • 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

  • nom_sjw (int) – Synchronization Jump Width for nominal bit rate. Decides the maximum number of time quanta that the controller can resynchronize every bit

  • data_brp (int) – Clock prescaler for fast data time quantum

  • data_tseg1 (int) – Time segment 1 for fast data bit rate, that is, the number of quanta from the Sync Segment to the sampling point

  • 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)

  • data_sjw (int) – Synchronization Jump Width for fast data bit rate

  • is_extended_id (bool) – This flag controls the size of the arbitration_id field

  • remote_id (Optional[int]) – id used for transmission

  • can_filters (Optional[list]) – iterable used to filter can id on reception

  • logging_activated (bool) – boolean used to disable logfile creation

:param bus_error_warning_filter : if True filter the logging message (‘Bus error: an error counter’)

_cc_close()[source]

Close the current can bus channel and uninitialize PCAN handle.

Return type

None

_cc_open()[source]

Open a can bus channel, set filters for reception and activate PCAN log.

Return type

None

_cc_receive(timeout=0.0001, raw=False)[source]

Receive a can message using configured filters.

If raw parameter is set to True return received message as it is (bytes) otherwise test entity protocol format is used and Message class type is returned.

Parameters
  • timeout (float) – timeout applied on reception

  • raw (bool) – boolean use to select test entity protocol format

Return type

Dict[str, Union[Message, bytes, int]]

Returns

the received data and the source can id

_cc_send(msg, raw=False, **kwargs)[source]

Send a CAN message at the configured id.

If remote_id parameter is not given take configured ones, in addition if raw is set to True take the msg parameter as it is otherwise parse it using test entity protocol format.

Parameters
  • msg (Union[Message, bytes]) – data to send

  • raw (bool) – boolean use to select test entity protocol format

  • kwargs – named arguments

Return type

None

_pcan_configure_trace()[source]

Configure PCAN dongle to create a trace file.

If self.logging_path is set, this path will be created, if it does not exist and the logfile will be placed there. Otherwise it will be logged to the current working directory if a default filename, which will be overwritten in successive calls. If an error occurs, the trace will not be started and the error logged. No exception is thrown in this case.

Return type

None

_pcan_set_value(channel, parameter, buffer)[source]

Set a value in the PCAN api.

If this is not successful, a RuntimeError is returned, as well as the PCAN error text is logged, if possible.

Parameters
  • channel – Channel for PCANBasic.SetValue

  • parameter – Parameter for PCANBasic.SetValue

  • buffer – Buffer for PCANBasic.SetValue

Raises

RuntimeError – Raised if the function is not successful

Return type

None

timeout

Extract the base logging directory from the logging module, so we can create our logging folder in the correct place. logging_path will be set to the parent directory of the logfile which is set in the logging module + /raw/PCAN If an AttributeError occurs, the logging module is not set to log into a file. In this case we set the logging_path to None and will just log into a generic logfile in the current working directory, which will be overwritten every time, a log is initiated.

class pykiso.lib.connectors.cc_pcan_can.PcanFilter(name='')[source]

Filter specific pcan logging messages

Initialize a filter.

Initialize with the name of the logger which, together with its children, will have its events allowed through the filter. If no name is specified, allow every event.

filter(record)[source]

Determine if the specified record is to be logged. It will not if it is a pcan bus error message

Parameters

record (LogRecord) – record of the event to filter if it is a pcan bus error

Return type

bool

Returns

True if the record should be logged, or False otherwise.