Skip to main content

A Python package for CAN bus communication

Project description

Canvect

Canvect is a package

My CAN Package

A Python package for handling CAN bus communication to easily handle the CAN bus communication for vehicle control. It also includes a RingBuffer implementation to store CAN messages efficiently.

Installation

pip install .

How to use package

  1. yoU can modify parameters like arbitration_id, bitrate, and channel to suit your needs
from my_can_package import continuous_acceleration_send

continuous_acceleration_send(
    arbitration_id=0x200,  # Custom CAN ID
    channel='PCAN_USBBUS1',  # Custom CAN channel
    bitrate=250000  # Custom bitrate
)

Example

from my_can_package import RingBuffer

# Create a RingBuffer with a capacity of 5 items
buffer = RingBuffer(capacity=5)

# Append items to the buffer
for i in range(10):
    buffer.append(f"message_{i}")
    print(f"Buffer after appending message_{i}: {buffer}")

# Access items by index
for i in range(len(buffer)):
    print(f"Item at index {i}: {buffer[i]}")
  1. The buffer can hold up to 5 items. Once it reaches the maximum capacity, new items will overwrite the oldest ones.

  2. You can access items using their index. If you try to access an index that is out of range, an IndexError will be raised.

Note: Here refers to above example

Sending CAN Messages

  1. Overview: The package provides functionality to send acceleration messages or any custom CAN message using the python-can library. You can customize the arbitration_id (CAN ID), data (8-byte payload), and CAN bus parameters such as bitrate and channel.

  2. Example Code for Sending CAN Messages

  3. Setup:

    • Initialize the CAN bus using your desired channel, interface, and bitrate.

    • 3.2 Send messages with the specified arbitration_id (CAN ID) and data payload (8 bytes)

from my_can_package import continuous_acceleration_send

# Example for continuously sending acceleration messages
continuous_acceleration_send(
    arbitration_id=0x130,  # Custom CAN ID
    channel='PCAN_USBBUS2',  # Custom CAN channel
    bitrate=500000  # Custom bitrate
)
  1. Explanation
  • The function continuous_acceleration_send sends acceleration messages continuously to the CAN bus.

  • You can modify the arbitration_id (CAN ID) as needed, as well as adjust the channel and bitrate to match your specific CAN bus setup.

  • Messages are sent at an interval of 10ms with dynamic byte values (like the seventh_byte) incrementing automatically

Customizing CAN Parameters

You have full control over the following parameters when sending CAN messages:

  • arbitration_id: This is the CAN ID used for sending the message.
  • channel: Specify the CAN interface/channel you are using. (e.g., PCAN_USBBUS2, can0).
  • bitrate: Define the CAN bus bitrate, typically 500000 for high-speed CAN.
from my_can_package import CANBusHandler, send_acceleration_message

# Initialize CAN bus
bus_handler = CANBusHandler(channel='PCAN_USBBUS2', interface='pcan', bitrate=500000)

# Define custom CAN data (8 bytes)
data = [0x11, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00]

# Send CAN message with a custom CAN ID and data
send_acceleration_message(bus_handler.bus, arbitration_id=0x200, data=data)

# Shutdown the bus after sending
bus_handler.shutdown()

Explanation:

  • You can use CANBusHandler to set up the CAN bus and * send_acceleration_message to send custom messages.
  • This example sends a message with a CAN ID of 0x200 and an 8-byte payload.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

canvect-0.1.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

canvect-0.1.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file canvect-0.1.1.tar.gz.

File metadata

  • Download URL: canvect-0.1.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.5

File hashes

Hashes for canvect-0.1.1.tar.gz
Algorithm Hash digest
SHA256 000944e56fd0d27ed62a3d6b0b33aa44ecd1f8573bdc2eab9cc8a20d1ba8c4f6
MD5 9ee35d4175641622b2601e550d1a04f4
BLAKE2b-256 9e09b669396d4ff4882bf454aec9fc194c6cb1ad6613d205b29226acd7052057

See more details on using hashes here.

File details

Details for the file canvect-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: canvect-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.5

File hashes

Hashes for canvect-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 718f80f10ba76c160c5771598370bb728a28b96abd4cf26c1cf6344a986cda8e
MD5 e3e53abc8e4f90e7ad152b56d9c22e53
BLAKE2b-256 020ac2d28c53afc85a1345b9682a165db712a9bf1c3487e9cb684b3c83399cf1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page