SocketCAN (ctypes)

socketcan_ctypes.py is a ctypes wrapper class around libc. It contains replications of constants and structures found in various linux header files. With Python 3.3, much of the functionality of this library is likely to be available natively in the Python socket module.

Bus

class can.interfaces.socketcan_ctypes.SocketcanCtypes_Bus(channel=0, receive_own_messages=False, *args, **kwargs)

An implementation of the can.bus.BusABC for SocketCAN using ctypes.

Broadcast-Manager

The socketcan_ctypes interface implements thin wrappers to the linux broadcast manager socket api. This allows the cyclic transmission of CAN messages at given intervals. The overhead for periodic message sending is extremely low as all the heavy lifting occurs within the linux kernel.

send_periodic()

An example that uses the send_periodic is included in python-can/examples/cyclic.py

The object returned can be used to halt, alter or cancel the periodic message task.

class can.interfaces.socketcan_ctypes.CyclicSendTask(channel, message, period)

Internals

createSocket

can.interfaces.socketcan_ctypes.createSocket(protocol=1)

This function creates a RAW CAN socket.

The socket returned needs to be bound to an interface by calling bindSocket().

Parameters:protocol (int) – The type of the socket to be bound. Valid values include CAN_RAW and CAN_BCM
Returns:
0 protocol invalid
-1 socket creation unsuccessful
socketID successful creation

bindSocket

can.interfaces.socketcan_ctypes.bindSocket(socketID, channel_name)

Binds the given socket to the given interface.

Parameters:
  • socketID (int) – The ID of the socket to be bound
  • channel_name (str) – The interface name to find and bind.
Returns:

The error code from the bind call.

0 protocol invalid
-1 socket creation unsuccessful

connectSocket

can.interfaces.socketcan_ctypes.connectSocket(socketID, channel_name)

Connects the given socket to the given interface.

Parameters:
  • socketID (int) – The ID of the socket to be bound
  • channel_name (str) – The interface name to find and bind.
Returns:

The error code from the bind call.

capturePacket

can.interfaces.socketcan_ctypes.capturePacket(socketID)

Captures a packet of data from the given socket.

Parameters:socketID (int) – The socket to read from
Returns:A dictionary with the following keys:
  • “CAN ID” (int)
  • “DLC” (int)
  • “Data” (list)
  • “Timestamp” (float)