Skip to main content

i2c_can

Python library to communicate with the Longan Labs I2C CAN Bus Module

Also sold as the SEEED STUDIO 113020111 I2C CAN-BUS Module

This library is nowhere near as safe, good, advanced, robust OR SAFE as https://github.com/commaai/panda so don't use it to drive a car.

Overview

add text

Features

  • tbc

Installation

Install the TPMS library using pip:

pip install i2c_can

Usage

TL;DR

add text

Example Code

add examples

TLDR

pip install i2c_can
from i2c_can import I2C_CAN, CAN_500KBPS, CAN_OK, CAN_MSGAVAIL
import time

def initialize_can():
    i2c_can = I2C_CAN(0x25)  # Replace with the actual I2C address
    i2c_can.begin()
    
    print("begin to init can")
    
    while True:
        if i2c_can.begin_CAN(CAN_500KBPS) == CAN_OK:
            break
        print("CAN BUS FAIL!")
        time.sleep(0.1)
    print("CAN BUS OK!")
    return i2c_can

def send_obd2_request(i2c_can, pid):
    request = [0x02, 0x01, pid, 0x00, 0x00, 0x00, 0x00, 0x00]
    i2c_can.send_msg_buf(0x7DF, 0, 0, 8, request)
    print(f"Sent OBD2 request for PID {pid:02X}")

def receive_obd2_response(i2c_can):
    if i2c_can.check_receive() == CAN_MSGAVAIL:
        length, buf = i2c_can.read_msg_buf()
        if length:
            can_id = i2c_can.get_can_id()
            print(f"Received CAN ID: {can_id:X}")
            print("Data: ", ' '.join(f"{byte:02X}" for byte in buf))
            return buf
    return None

def parse_response(pid, response):
    if pid == 0x0C:  # Engine RPM
        rpm = ((response[3] * 256) + response[4]) / 4
        print(f"Engine RPM: {rpm}")
    elif pid == 0x0D:  # Vehicle Speed
        speed = response[3]
        print(f"Vehicle Speed: {speed} km/h")
    elif pid == 0x05:  # Engine Coolant Temperature
        temp = response[3] - 40
        print(f"Engine Coolant Temperature: {temp} °C")

def main():
    i2c_can = initialize_can()

    # List of PIDs to monitor
    pids = [0x0C, 0x0D, 0x05]  # Engine RPM, Vehicle Speed, Engine Coolant Temperature

    while True:
        for pid in pids:
            send_obd2_request(i2c_can, pid)
            response = receive_obd2_response(i2c_can)
            if response:
                if response[2] == pid:
                    parse_response(pid, response)
            time.sleep(1)  # Delay between requests for different PIDs

if __name__ == "__main__":
    main()

TODO

Check it actually works...

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for i2c-can 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for i2c-can 0.0.2
File Size Uploaded
i2c_can-0.0.2.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for i2c-can 0.0.2
File Interpreter ABI Platform
i2c_can-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.3 kB

Release files / i2c_can-0.0.2.tar.gz

Download URL i2c_can-0.0.2.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
afa11588807a820a32b923614c39a3a8bfcd0779f99bd3fe3beda4120563360a
BLAKE2b-256 checksum
How to use checksums
9b7ff02fe635033aafb1a92758f2bf7e987b567ff9565b53199b298e9224b6b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.6

Release files / i2c_can-0.0.2-py3-none-any.whl

Download URL i2c_can-0.0.2-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
616addb55adc3cbb9ce0a81d754270c5a2e51f6dbeaa7ed849a6af9191696212
BLAKE2b-256 checksum
How to use checksums
01b896989fbc82a150cbdb80be3ce38a0dc9d801c840f9f0a39712ea03844b88
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.6

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page