AnnaBanana
AnnaBanana

Reputation: 131

Socketcan+ Express.js +Node.js get / send Can-Bus messages

I installed socketcan with

    npm install socketcan

link without any errors on my Raspberry Pi 2 B. I can use the Pican adapter with candump/cansend on the terminal. So that works fine. But I don't understand how to use Socketcan. I want to use can0.

This is the help from Socketcan:

    var can = require('socketcan');

    var channel = can.createRawChannel("vcan0", true);

   // Log any message 
  channel.addListener("onMessage", function(msg) { console.log(msg); } );

   // Reply any message 
   channel.addListener("onMessage", channel.send, channel);

   channel.start();

Working with message and signals:

var can = require('socketcan');
var fs = require('fs');

// Parse database 
var network =  can.parseNetworkDescription("samples/can_definition_sample.kcd");
var channel = can.createRawChannel("vcan0");
var db      = new can.DatabaseService(channel, network.buses["Motor"]);

channel.start();

// Register a listener to get any value changes 
db.messages["CruiseControlStatus"].signals["SpeedKm"].onChange(function(s) {
console.log("SpeedKm " + s.value);
});

// Update tank temperature           
db.messages["TankController"].signals["TankTemperature"].update(80);

// Trigger sending this message 
db.send("TankController");

But in which file do I put these commands, app.js, index.html or bin/www file? The things I tried didn't work. And how can I set the baudrate/bitrate? Or is there an alternative, which I could use to log / send some messages?

I just started with JavaScript / HTML5 / CSS.

Upvotes: 2

Views: 4430

Answers (3)

Darko Lukić
Darko Lukić

Reputation: 196

You can also use child_process to set up CAN interface from Node.js:

const exec = require('child_process').execSync;

// ...

function initializeCAN(device, bitrate) {
    let result;

    // Turn off CAN bus if it is alive
    exec('sudo ip link set ' + device + ' down type can');

    // Turn on CAN bus
    result = exec('sudo ip link set ' + device + ' up type can bitrate ' + bitrate);
    if (result.toString()) {
        throw Error('CAN bus can\'t be initialized');
    }
}

// ...

initializeCAN('can0', 12500);

Upvotes: 1

brirus
brirus

Reputation: 81

You can set up can0 and set the bit rate with the ip command.

from the command line:

ip link set can0 type can bitrate 100000 up

To make the settings persist you can configure your interface in the /etc/network/interfaces file.

allow-hotplug can0
iface can0 inet manual
    pre-up /sbin/ip link set $IFACE type can txqlen 512 bitrate 100000 sample-point 0.75
    up /sbin/ifconfig $IFACE up
    down /sbin/ifconfig $IFACE down

complete documentation is here: http://elinux.org/Bringing_CAN_interface_up

Upvotes: 4

Mathew Comeau
Mathew Comeau

Reputation: 21

A few things: These commands want to live in a .js file

You must set the baudrate with ip, something like: ip link set can0 type can bitrate 1000000 triple-sampling on

I found this site very helpful to get the interface working, you'll need to edit your /etc/networks/interfaces file to get it to persist http://www.embeddedhobbyist.com/2015/09/linux-can-development/

What brought me to this page at first proved to be so tricky that I made this account just to come back here and explain: The TankController message isn't in the Motor bus, examine samples/can_definition_sample.kcd to see what I mean. https://github.com/sebi2k1/node-can

Also, when I was testing socketcan with require('socketcan') it keeps returning undefined, which is apparently ok.

Good luck folks

Upvotes: 2

Related Questions