CAN over Serial / SLCAN

A text based interface: compatible to slcan-interfaces (slcan ASCII protocol) should also support LAWICEL direct. These interfaces can also be used with socketcan and slcand with Linux. This driver directly uses either the local or remote serial port, it makes slcan-compatible interfaces usable with Windows also. Remote serial ports will be specified via special URL. Both raw TCP sockets as also RFC2217 ports are supported.

Usage: use port or URL[@baurate] to open the device. For example use /dev/ttyUSB0@115200 or COM4@9600 for local serial ports and socket://192.168.254.254:5000 or rfc2217://192.168.254.254:5000 for remote ports.

Supported devices

Todo

Document this.

Bus

class can.interfaces.slcan.slcanBus(channel, tty_baudrate=115200, bitrate=None, timing=None, sleep_after_open=2, rtscts=False, listen_only=False, timeout=0.001, **kwargs)[source]

slcan interface

Parameters:
  • channel (str) – port of underlying serial or usb device (e.g. /dev/ttyUSB0, COM8, …) Must not be empty. Can also end with @115200 (or similarly) to specify the baudrate.

  • tty_baudrate (int) – baudrate of underlying serial or usb device (Ignored if set via the channel parameter)

  • bitrate (int | None) – Bitrate in bit/s

  • timing (BitTiming | BitTimingFd | None) – Optional BitTiming instance to use for custom bit timing setting. If this argument is set then it overrides the bitrate and btr arguments. The f_clock value of the timing instance must be set to 8_000_000 (8MHz) for standard CAN. CAN FD and the BitTimingFd class are not supported.

  • poll_interval – Poll interval in seconds when reading messages

  • sleep_after_open (float) – Time to wait in seconds after opening serial connection

  • rtscts (bool) – turn hardware handshake (RTS/CTS) on and off

  • listen_only (bool) – If True, open interface/channel in listen mode with L command. Otherwise, the (default) O command is still used. See open method.

  • timeout (float) – Timeout for the serial or usb device in seconds (default 0.001)

  • kwargs (Any)

Raises:
get_serial_number(timeout)[source]

Get serial number of the slcan interface.

Parameters:

timeout (float | None) – seconds to wait for serial number or None to wait indefinitely

Returns:

None on timeout or a str object.

Return type:

str | None

get_version(timeout)[source]

Get HW and SW version of the slcan interface.

Parameters:

timeout (float | None) – seconds to wait for version or None to wait indefinitely

Returns:

tuple (hw_version, sw_version) WHERE int hw_version is the hardware version or None on timeout int sw_version is the software version or None on timeout

Return type:

Tuple[int | None, int | None]

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 (float | None) – 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

Return type:

None

set_bitrate(bitrate)[source]
Parameters:

bitrate (int) – Bitrate in bit/s

Raises:

ValueError – if bitrate is not among the possible values

Return type:

None

set_bitrate_reg(btr)[source]
Parameters:

btr (str) – BTR register value to set custom can speed as a string xxyy where xx is the BTR0 value in hex and yy is the BTR1 value in hex.

Return type:

None

shutdown()[source]

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

This method can be safely called multiple times.

Return type:

None

Internals

Todo

Document the internals of slcan interface.