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)[source]

Bases: can.bus.BusABC

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

Parameters:channel (str) – The can interface name with which to create this bus. An example channel would be ‘vcan0’.

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)[source]

Bases: can.interfaces.socketcan_ctypes.SocketCanCtypesBCMBase, can.broadcastmanager.CyclicSendTaskABC

Parameters:
  • channel – The name of the CAN channel to connect to.
  • message – The message to be sent periodically.
  • period – The rate in seconds at which to send the message.
modify_data(message)[source]

Update the contents of this periodically sent message.

stop()[source]

Send a TX_DELETE message to cancel this task.

This will delete the entry for the transmission of the CAN-message with the specified can_id CAN identifier. The message length for the command TX_DELETE is {[bcm_msg_head]} (only the header).

Internals

createSocket

can.interfaces.socketcan_ctypes.createSocket(protocol=1)[source]

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)[source]

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)[source]

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)[source]

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)