CAN over Serial

A text based interface. For example use over serial ports like /dev/ttyS1 or /dev/ttyUSB0 on Linux machines or COM1 on Windows. Remote ports can be also used via a special URL. Both raw TCP sockets as also RFC2217 ports are supported: socket://192.168.254.254:5000 or rfc2217://192.168.254.254:5000. In addition a virtual loopback can be used via loop:// URL. The interface is a simple implementation that has been used for recording CAN traces.

Bus

class can.interfaces.serial.serial_can.SerialBus(*args, **kwargs)[source]

Enable basic can communication over a serial device.

Note

See _recv_internal() for some special semantics.

Parameters:
  • channel (str) – The serial device to open. For example “/dev/ttyS1” or “/dev/ttyUSB0” on Linux or “COM1” on Windows systems.

  • baudrate (int) –

    Baud rate of the serial device in bit/s (default 115200).

    Warning

    Some serial port implementations don’t care about the baudrate.

  • timeout (float) – Timeout for the serial device in seconds (default 0.1).

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

  • kwargs (Any)

Raises:
_recv_internal(timeout)[source]

Read a message from the serial device.

Parameters:

timeout (float | None) –

Warning

This parameter will be ignored. The timeout value of the channel is used.

Returns:

Received message and False (because no filtering as taken place).

Return type:

tuple[Message | None, bool]

Internals

The frames that will be sent and received over the serial interface consist of six parts. The start and the stop byte for the frame, the timestamp, DLC, arbitration ID and the payload. The payload has a variable length of between 0 and 8 bytes, the other parts are fixed. Both, the timestamp and the arbitration ID will be interpreted as 4 byte unsigned integers. The DLC is also an unsigned integer with a length of 1 byte. Extended (29-bit) identifiers are encoded by adding 0x80000000 to the ID. For example, a 29-bit CAN ID of 0x123 is encoded with an arbitration ID of 0x80000123. Remote frames set bit 30 (0x40000000) of the arbitration ID. They have no payload; their DLC holds the requested data length.

Serial frame format

Start of frame

Timestamp

DLC

Arbitration ID

Payload

End of frame

Length (Byte)

1

4

1

4

0 - 8

1

Data type

Byte

Unsigned 4 byte integer

Unsigned 1 byte integer

Unsigned 4 byte integer

Byte

Byte

Byte order

-

Little-Endian

Little-Endian

Little-Endian

-

-

Description

Must be 0xAA

Usually s, ms or µs since start of the device

Payload length; requested data length for remote frames

-

-

Must be 0xBB

Arbitration ID format

Data Bits

Bit 31

Bit 30

Bit 29

Bits 28:0

Description

Extended ID

Remote Frame

Error Frame

Message ID

Mask

0x80000000

0x40000000

0x20000000

0x1FFFFFFF for Extended ID

0x7FF for Standard ID

Examples of serial frames

CAN message with 8 byte payload

CAN message

Arbitration ID

Payload

1

0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88

Serial frame

Start of frame

Timestamp

DLC

Arbitration ID

Payload

End of frame

0xAA

0x66 0x73 0x00 0x00

0x08

0x01 0x00 0x00 0x00

0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88

0xBB

CAN message with 1 byte payload

CAN message

Arbitration ID

Payload

1

0x11

Serial frame

Start of frame

Timestamp

DLC

Arbitration ID

Payload

End of frame

0xAA

0x66 0x73 0x00 0x00

0x01

0x01 0x00 0x00 0x00

0x11

0xBB

CAN message with 0 byte payload

CAN message

Arbitration ID

Payload

1

None

Serial frame

Start of frame

Timestamp

DLC

Arbitration ID

End of frame

0xAA

0x66 0x73 0x00 0x00

0x00

0x01 0x00 0x00 0x00

0xBB

Extended Frame CAN message with 0 byte payload with an 29-bit CAN ID

CAN message

Arbitration ID

Payload

0x80000001 (1)

None

Serial frame

Start of frame

Timestamp

DLC

Arbitration ID

End of frame

0xAA

0x66 0x73 0x00 0x00

0x00

0x01 0x00 0x00 0x80

0xBB