Skip to content

Commit

Permalink
feature: support little endian $MXPGN (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbender9 authored Jul 25, 2024
1 parent edae1e1 commit 549edd0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
5 changes: 4 additions & 1 deletion bin/analyzerjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const argv = require('minimist')(process.argv.slice(2), {
alias: { h: 'help' },
boolean: ['n']
boolean: ['n'],
boolean: ['r']
})

if ( argv['help'] ) {
Expand All @@ -11,13 +12,15 @@ if ( argv['help'] ) {
Options:
-c don't check for invalid values
-n output null values
-r parse $MXPGN as little endian
-h, --help output usage information`)
process.exit(1)
}

const Parser = require('../index').FromPgn
var parser = new Parser( {
returnNulls: argv['n'] === true,
littleEndianMXPGN: argv['r'] === true,
checkForInvalidFields: argv['c'] !== true
})

Expand Down
2 changes: 1 addition & 1 deletion lib/fromPgn.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ class Parser extends EventEmitter {

parseString (pgn_data, cb) {
try {
const { coalesced, data, error, len, ...pgn } = parseN2kString(pgn_data)
const { coalesced, data, error, len, ...pgn } = parseN2kString(pgn_data, this.options)
if (error) {
cb && cb(error)
this.emit('error', pgn, error)
Expand Down
21 changes: 19 additions & 2 deletions lib/stringMsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,22 @@ exports.encodePCDIN = ({ prefix = '$PCDIN', pgn, data, dst = 255}) => {
return sentence + compute0183Checksum(sentence)
}

const changeEndianness = (string) => {
const result = [];
let len = string.length - 2;
while (len >= 0) {
result.push(string.substr(len, 2));
len -= 2;
}
return result.join('');
}

// $MXPGN,01F801,2801,C1308AC40C5DE343*19
exports.isMXPGN = (msg) => {
const sentence = get0183Sentence(msg)
return sentence.startsWith('$MXPGN,')
}
exports.parseMXPGN = (input) => {
exports.parseMXPGN = (input, options) => {
const sentence = get0183Sentence(input)
const [ prefix, pgn, attr_word, data ] = sentence.split(',')

Expand All @@ -172,11 +182,18 @@ exports.parseMXPGN = (input) => {
const len = parseInt(send_prio_len.substr(4,4), 2);
let src, dst;
send ? dst = addr: src = addr;

let reversed

if ( options && options.littleEndianMXPGN )
reversed = changeEndianness(rmChecksum(data))
else
reversed = data

return buildMsg(
buildCanId(0, parseInt(pgn, 16), 255, parseInt(src, 16)),
'MXPGN',
Buffer.from(rmChecksum(data), 'hex'),
Buffer.from(reversed, 'hex'),
{ coalesced: true, prefix },
)
}
Expand Down

0 comments on commit 549edd0

Please sign in to comment.