Skip to content

Commit

Permalink
Fixed problem with remote frame and dlc != 0
Browse files Browse the repository at this point in the history
  • Loading branch information
grodansparadis committed Jun 10, 2022
1 parent c37781e commit 3c70e28
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "node-red-contrib-socketcan",
"version": "1.2.7",
"version": "1.2.8",
"description": "node-red nodes for socketcan",
"dependencies": {
"bcrypt": ">=5.0.1",
"minimist": ">=1.2.6",
"node-red": "^2.2.2",
"socketcan": ">=3.0.0",
"socketcan": "^3.0.0",
"util": ">=0.12.4"
},
"homepage": "https://github.com/grodansparadis/node-red-contrib-socketcan",
Expand Down
40 changes: 25 additions & 15 deletions src/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,21 @@ module.exports = function(RED) {
frame.rtr = msg.payload.rtr || false;
frame.id = msg.payload.canid || 0;
frame.id |= (msg.payload.ext ? 1 : 0) << 31;
frame.dlc = 0;
frame.dlc = msg.payload.dlc || 0;
frame.data = Buffer.from('');
frame.canfd = msg.payload.canfd || false;

// remote transmission request does not have any data
if ( !msg.payload.rtr ) {

frame.dlc = msg.payload.dlc || 0;

if (!frame.canfd){
if ( frame.dlc > 8 ) {
if (done) {
// Node-RED 1.0 compatible
done("Invalid CAN frame length " +
frame.dlc);
} else {
}
else {
// Node-RED 0.x compatible
node.error("Invalid CAN frame length " +
frame.dlc, msg);
Expand All @@ -128,7 +127,7 @@ module.exports = function(RED) {
// Node-RED 0.x compatible
node.error("Invalid CAN FD frame length " + frame.dlc, msg);
}
}
}
}


Expand All @@ -147,7 +146,8 @@ module.exports = function(RED) {
if (done) {
// Node-RED 1.0 compatible
done("Error: CAN data has unknown format.");
} else {
}
else {
// Node-RED 0.x compatible
node.error("Error: CAN data has unknown format.", msg);
}
Expand Down Expand Up @@ -176,16 +176,17 @@ module.exports = function(RED) {
frame.dlc = frame.data.length;
if ( frame.dlc > 64 ) {
if (done) {
// Node-RED 1.0 compatible
done("Invalid CAN FD frame length " + frame.dlc);
// Node-RED 1.0 compatible
done("Invalid CAN FD frame length " + frame.dlc);
}
else {
// Node-RED 0.x compatible
node.error("Invalid CAN FD frame length " + frame.dlc, msg);
// Node-RED 0.x compatible
node.error("Invalid CAN FD frame length " + frame.dlc, msg);
}
}
}
else if( msg.payload && (msg.payload.indexOf("#") != -1 ) ) {

debuglog("CAN Frame");
frame.canfd = false;
let id = msg.payload.split("#")[0];
Expand Down Expand Up @@ -213,12 +214,14 @@ module.exports = function(RED) {
}
}
else {
// Remote transmission request
debuglog("Remote transmission request");
// Remote transmission request id#R{len}
debuglog("Remote transmission request ", msg.payload);
let dlc = msg.payload.split("#R")[1];
debuglog("dlc = ", dlc);
frame.dlc = parseInt(dlc,10);
frame.canfd = false;
frame.dlc = 0;
frame.rtr = true;
frame.data = Buffer.from('');
frame.rtr = true;
frame.data = Buffer.from('');
}
}
else {
Expand All @@ -238,6 +241,13 @@ module.exports = function(RED) {
debuglog("Message format = ??????");
}

// node socketcan lib need data to send dlc other than zero for frame
// so we create some data.
// Does not matter what the buffer holds as it is not sent
if (frame.rtr && (frame.dlc > 0) ) {
frame.data = Buffer.alloc(frame.dlc,0);
}

debuglog("canid:" + frame.id +
" dlc:" + frame.dlc +
" data:" + Array.prototype.slice.call(frame.data,0) +
Expand Down
22 changes: 22 additions & 0 deletions test/node/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var can = require('socketcan');

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

// var frame={};
// frame.ext = false;
// frame.rtr = false;
// frame.id = 113;
// frame.dlc = 3;
// frame.data = Buffer.from([1,2,3]);
// sock.send(frame);

var frame_rtr={};
frame_rtr.ext = false;
frame_rtr.rtr = true;
frame_rtr.id = 123;
frame_rtr.dlc = 3;
frame_rtr.data = Buffer.from('123');
sock.send(frame_rtr);

sock.stop();

0 comments on commit 3c70e28

Please sign in to comment.