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.
SocketcanCtypes_Bus
(channel='vcan0', receive_own_messages=False, *args, **kwargs)[source]¶ Bases:
can.bus.BusABC
An implementation of the
can.bus.BusABC
for SocketCAN usingctypes
.Parameters: channel (str) – The can interface name with which to create this bus. An example channel would be ‘vcan0’. -
set_filters
(can_filters=None)[source]¶ Apply filtering to all messages received by this Bus.
Calling without passing any filters will reset the applied filters.
Parameters: can_filters (list) – A list of dictionaries each containing a “can_id” and a “can_mask”.
>>> [{"can_id": 0x11, "can_mask": 0x21}]
A filter matches, when
<received_can_id> & can_mask == can_id & can_mask
-
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.socketcan_ctypes.
CyclicSendTask
(channel, message, period)[source]¶ Bases:
can.interfaces.socketcan.socketcan_ctypes.SocketCanCtypesBCMBase
,can.broadcastmanager.RestartableCyclicTaskABC
,can.broadcastmanager.ModifiableCyclicTaskABC
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.
Internals¶
createSocket¶
-
can.interfaces.socketcan.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.socketcan_ctypes.
bindSocket
(socketID, channel_name)[source]¶ Binds the given socket to the given interface.
Parameters: Returns: The error code from the bind call.
0 protocol invalid -1 socket creation unsuccessful
connectSocket