2

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.

AnnaBanana
  • 131
  • 2
  • 6

3 Answers3

4

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

brirus
  • 81
  • 6
2

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

  • Please put essential informations in your answer. Link may be outdated someday. – jogo Dec 17 '15 at 20:57
  • I think I made the right decision as to what to link and what to include. I answered the direct questions directly. I believe AnnaBanana was working from the same tutorial as I, so then I pointed her to the places that helped me through it. It wouldn't be fair to steal the content from embeddedhobbyist – Mathew Comeau Dec 18 '15 at 21:17
  • Just saw the answear. I got it running. Not exactly how I wanted it to be, but it works now. One problem was that I didn't knew that I need socket.io and another that the version of Node and socketcan wasn't compatible to each other. I will post my answear the next weeks, after my exams. so Long have fun and thanks for the help. – AnnaBanana Jan 07 '16 at 09:09
1

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);
Darko Lukić
  • 161
  • 2
  • 6