Skip to content

Instantly share code, notes, and snippets.

@crycode-de
Last active March 18, 2021 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crycode-de/4d924b5234d1cf3b37a1386aef308059 to your computer and use it in GitHub Desktop.
Save crycode-de/4d924b5234d1cf3b37a1386aef308059 to your computer and use it in GitHub Desktop.
Example to send a can message every 100ms using node.js and socketcan.
/**
* Example to send a can message every 100ms using node.js and socketcan.
*
* Run `npm i socketcan` once to install the node module.
*
* License: CC-BY-NC-SA 4.0
* (c) 2021 Peter Müller <peter@crycode.de> (https://crycode.de)
*/
const socketcan = require('socketcan');
const channel = socketcan.createRawChannel('can0');
channel.start();
function sendData () {
channel.send({
id: 0x042, // the can id
ext: false, // true for extendes frames
rtr: false,
data: Buffer.from([0x01, 0x02, 0x03]), // the data to send
});
}
setInterval(sendData, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment