Giter VIP home page Giter VIP logo

php-canbus's Introduction

PHP-CanBus Extension

CodeFactor

PHP-canbus is THE extension for PHP on Linux that allows PHP code to interface efficiently with a Controller Area Network (CAN bus) 2.0A / 2.0B.

Due to the CAN Frames lightweight nature (they consist only of 11 or 29 bit identifier, 0 to 8 data bytes and flags) using methods like parsing shell_exec('candump vcan0 -n 1') output has huge drawbacks - it supports only blocking mode and is really really slow. Performance is often a key aspect as most use-cases of CAN-Bus is in embedded systems. With this extension you can efficiently control your car, elevator or even airplane.

Development and testing was done on Ubuntu 20.04 with VCAN, real world tests performed on Raspberry Pi 4B + MCP2515+TJA1050 module.

Table of content

Basics

Classes and methods

class CanBus {

    /**
     * CanBus constructor.
     *
     * @param string $interface non-empty string
     */
    public function __construct(string $interface) {}

    /**
     * Initializes CanBus interface and connects to unix sockets
     * If socket was initialized before, it closes previous connection
     *
     * @param bool $blocking Whether interface should be blocking or not
     * @return bool success/failure
     */
    public function init(bool $blocking = true): bool {}

    /**
     * Attempts to read single CanFrame.
     *
     * @return CanFrame|false CanFrame on success, false on failure
     */
    public function read(): CanFrame|false {}
    
    /**
     * Attempts to send single CanFrame.
     *
     * @return bool success/failure
     */
    public function send(CanFrame $frame): bool {}

    /**
     * Generates random CanFrame
     * ID: 0 to 0x7FF
     * Data: 0 to 8 bytes of values in range of 0 to 0xFF (0-255)
     *
     * @return CanFrame
     */
    public function generateRandomFrame(): CanFrame {}
}

class CanFrame {

    /**
     * CanFrame constructor.
     *
     * @param int $index value in range o 0 to 0x7FFFFFFF (CAN 2.0B)
     * @param array $data 0 to 8 bytes of values in range of 0 to 0xFF (0-255)
     */
    public function __construct(int $index, array $data) {}
}

Examples

Listening:

<?php

// Create new CanBus interface object
$canBus = new CanBus('vcan0');

// Initialize interface (connect to unix socket)
if($canBus->init() === false) {
    // Handle error
}

// Read loop
while(true) {
    $frame = $canBus->read();
    if($frame === false) continue;
    
    // Do something with the frame
}

Sending:

<?php

// Create new CanBus interface object
$canBus = new CanBus('vcan0');

// Initialize interface (connect to unix socket)
if($canBus->init() === false) {
    // Handle error
}

// Create new frame
$canFrame = new CanFrame(
    0x204,
    [0x00, 0x02, 0x01]
);

// Send new frame
if($canBus->send($canFrame) === false) {
    // Handle error
}

Testing:

<?php

// Create new CanBus interface object
$canBus = new CanBus('vcan0');

// Generate random frame
$randomCanFrame = $canBus->generateRandomFrame();
var_dump($randomCanFrame);

// Create new frame
$canFrame = new CanFrame(
    0x204, // Id
    [0x00, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08] // Data
);
var_dump($canFrame);

Building

Download latest php-dev package (as the time of writing: sudo apt install php8.2-dev).

Then run following commands inside repo directory:

phpize              # configures extension for your PHP version
./configure         # configures extension details
make                # builds extension
make install        # installs extension in your system

Installation

  • Find out path to your php.ini file by running php -i | grep "Configuration File (php.ini) Path"
    • It should be something like /etc/php/8.0/cli
  • Open php.ini file it in text editor (ex. sudo nano /etc/php/8.0/cli/php.ini)
    • As it might be in write-restricted path, use sudo
  • Find Dynamic Extensions section (around line 900)
  • If you previously used make install, add line extension=canbus
    • Otherwise, add extension=/home/pi/canbus.so where /home/pi/canbus.so is an extension path

TODOs

  • Extension skeleton
  • Connecting to unix socket
  • Listening
    • Blocking
    • Non-blocking
    • Socket-based filtering
  • Sending
  • More detailed tests
  • Ensure no memory leaks
  • CAN-FD Support
  • CI for different PHP versions

Links

php-canbus's People

Contributors

adamczykpiotr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

php-canbus's Issues

Can-FD

Hey, such a good approach!
Do you plan to add can-fd and long payload support in nearest future? Thank you!

Read Timeout?

Anyway, can you get a timeout on the "read"? For example, if the data on the network stops (for whatever reason), the process sits at the "read command" waiting for data. When used as a service with a watchdog, the watchdog needs to be pinged every 30 seconds, which can not happen when the code is stuck at the ->read function. please help!

Timestamp?

the candump utility provide a timestamp with the NMEa message. Any way to get this timestamp?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.