5 minutes
Cannes - a wrapper for CAN written in Elixir
“Shanghai interchange” by Denys Nevozhai
CAN BUS tools
Cannes (named after the city in France 🇫🇷) is an elixir WIP-wrapper around cantools which is written in python. The wrapper makes it easier to handle CAN messages in Elixir projects. Individual functionalities of cantools
are implemented using Porcelain. Porcelain implements a saner approach to launching and communicating with external OS processes from Elixir. Built on top of Erlang’s ports, it provides richer functionality and simpler API.
Why a wrapper for CAN?
As part of my master thesis (A Man-In-The-Middle Approach Towards Dataflow Management for OBD-II Interfaces in Vehicles
) I had to deal with a variety of CAN messages. Since I wanted to develop my approach using Elixir, I needed a simple way to encode and decode different CAN messages in Elixir.
INFO:
Cannes
is still under development and by far not complete. Feel free to contribute.
See General Informations for more about this project.
Prerequisites
- Install Python v3.x
- Install Elixir v1.11+
- Setup a virtualenv and activate it
- Run
pip install -r requirements.txt
to install the python dependencies - Run
mix deps.get
to install the Hex dependencies
Installation
To use Cannes in your Mix projects, first add Cannes as a dependency.
|
|
Components of the library
“Brief general overview of the internal components.“
Dumper.ex
The dumper
module is for communicating with linux can sockets over candump.
In order to spawns a candump process on the given interface you can use the following snippet.
|
|
There is also a function for stopping a running
candump
process carefully and for checking if the candump
process is still running.If you want to get a stream of formatted can messages you can do this by calling the following function.
|
|
get_stream
function.This raw stream can also be formatted afterwards by passing the stored binary to format_candump_string
.
|
|
Compostor.ex
This module is the counterpart to the Cannes.Dumper
. With the help of the included functions it is possible to send CAN messages.
Mock.ex
The Cannes.Mock
module provices some useful functions for simulating simple CAN-messages. You can get an infinite stream of formatted can messages by calling the file_stream
function. Currently we use a simple logging file as blueprints for the messages.
|
|
Tools.ex
Cannes.Tools
facilitates a usefull wrapper for the python library called cantool
.
Using the decode_message
function it is possible to decode given signal data as a message with given frame ID or given name frame_id_or_name
. The function returns a dictionary of signal name value entries. If decode_choices
is false
, scaled values are not converted to selection strings (if any). If the scaling is false
, no scaling of signals is performed.
|
|
The counterpart to this is the encode_message
function. It encodes given signal data as a message with the given frame ID or name frame_id_or_name
. The return value is a dictionary of signal name value entries. If scaling is false
, no scaling of the signals is performed. If padding is true
, unused bits are encoded as 1. If strict is true
, all signal values must be within their allowed ranges or an exception will be thrown.
|
|