Skip to content

Commit

Permalink
fix: not numerical variable length fields not encoding or decoding pr…
Browse files Browse the repository at this point in the history
…operly (#112)
  • Loading branch information
sbender9 authored Apr 19, 2020
1 parent 5630b72 commit b9ea3ee
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
18 changes: 11 additions & 7 deletions lib/fromPgn.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ function readField(options, runPostProcessor, pgn, field, bs) {
bs.readBits(bs.bitsLeft, false)
return
}
value = readValue(pgn, field, bs)
value = readValue(options, pgn, field, bs)
}

//console.log(`${field.Name} ${value} ${field.Resolution}`)
Expand All @@ -464,7 +464,7 @@ function readField(options, runPostProcessor, pgn, field, bs) {
if ( field.Offset ) {
value += field.Offset
}
if ( field.Resolution ) {
if ( field.Resolution && typeof value === 'number' ) {
var resolution = field.Resolution

value = (value * resolution)
Expand Down Expand Up @@ -507,13 +507,13 @@ function readField(options, runPostProcessor, pgn, field, bs) {
return value
}

function readValue(pgn, field, bs, bitLength) {
function readValue(options, pgn, field, bs, bitLength) {
var value
if ( _.isUndefined(bitLength) ) {
bitLength = field.BitLength
}
if ( bitLength == 0 ) {
value = readVariableLengthField(pgn, field, bs)
return readVariableLengthField(options, pgn, field, bs)
} else if (bitLength === 8) {
if ( field.Signed ) {
value = bs.readInt8()
Expand Down Expand Up @@ -571,7 +571,11 @@ function readValue(pgn, field, bs, bitLength) {
}
//console.log(`${field.Name} ${bitLength} ${value} ${bs.view.buffer[bs.byteIndex]} ${bs.index}`)
}


if ( value != null && typeof value !== 'undefined' && typeof value !== 'number' ) {
value = Number(value)
}

return value
}

Expand All @@ -590,7 +594,7 @@ function isMax(numBits, value, signed)
return signed ? (value & 1) == 0 : true
}

function readVariableLengthField(pgn, field, bs) {
function readVariableLengthField(options, pgn, field, bs) {
/* PGN 126208 contains variable field length.
* The field length can be derived from the PGN mentioned earlier in the message,
* plus the field number.
Expand All @@ -605,7 +609,7 @@ function readVariableLengthField(pgn, field, bs) {

if ( refField ) {
var bits = (refField.BitLength + 7) & ~7; // Round # of bits in field refField up to complete bytes: 1->8, 7->8, 8->8 etc.
return readValue(pgn, refField, bs, bits)
return readField(options, false, pgn, refField, bs, bits)
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/toPgn.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ function writeField(bs, pgn_number, field, data, value, bitLength) {
}
}

if (field.Resolution) {
if (field.Resolution && typeof value === 'number' ) {
value = (value / field.Resolution).toFixed(0);
}

if ( field.Type && fieldTypeWriters[field.Type] ) {
fieldTypeWriters[field.Type](pgn_number, field, value, bs)
} else {
if ( _.isString(value) ) {
if ( _.isString(value) && bitLength !== 0 ) {
value = Number(value)
}

Expand Down
4 changes: 2 additions & 2 deletions test/pgns/126208.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = [{
"expected": {"timestamp":"2017-04-15T14:57:58.471Z","prio":3,"src":204,"dst":172,"pgn":126208,"description":"NMEA - Request group function","fields":{"Function Code":"Request","PGN":126720,"# of Parameters":4,"list":[{"Parameter":1,"Value":1851},{"Parameter":3,"Value":4},{"Parameter":4,"Value":1372},{"Parameter":15}]}},
"input": "2017-04-15T14:57:58.471Z,3,126208,204,172,21,00,00,ef,01,ff,ff,ff,ff,ff,ff,04,01,3b,07,03,04,04,5c,05,0f,ff"
"expected": {"prio":2,"pgn":126208,"dst":67,"src":0,"timestamp":"2020-04-19T00:35:55.571Z","fields":{"Function Code":"Command","PGN":126998,"# of Parameters":1,"list":[{"Parameter":2,"Value":"YD:VOLUME 60"}]},"description":"NMEA - Command group function"},
"input": "2020-04-19T00:35:55.571Z,2,126208,0,67,21,01,16,f0,01,ff,01,02,0e,01,59,44,3a,56,4f,4c,55,4d,45,20,36,30"
}]

0 comments on commit b9ea3ee

Please sign in to comment.