MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / parse

Function parse

app/rcc/scripts/parser/js/nmea_2000.js:195–223  ·  view source on GitHub ↗

* Parses an NMEA 2000 CAN frame into the predefined array structure. * * NMEA 2000 frame structure (assuming preprocessed CAN frame): * Bytes 0-3: CAN ID (29-bit extended, contains PGN) * Byte 4: Data length (0-8) * Bytes 5-12: Data payload (up to 8 bytes) * * How it works: * 1. E

(frame)

Source from the content-addressed store, hash-verified

193 * @returns {array} Array of values mapped according to pgnToIndexMap
194 */
195function parse(frame) {
196 if (frame.length < 6)
197 return parsedValues;
198
199 var canId = frame[0] | (frame[1] << 8) | (frame[2] << 16) | (frame[3] << 24);
200 var dataLength = Math.min(frame[4], 8);
201
202 var data = [];
203 for (var i = 0; i < dataLength && (5 + i) < frame.length; i++)
204 data.push(frame[5 + i]);
205
206 var pgn = extractPGN(canId);
207
208 if (!pgnToIndexMap.hasOwnProperty(pgn))
209 return parsedValues;
210
211 var config = pgnToIndexMap[pgn];
212
213 try {
214 var values = config.parser(data);
215 for (var i = 0; i < values.length && i < config.indices.length; i++)
216 parsedValues[config.indices[i]] = values[i];
217 }
218 catch (e) {
219 console.error("NMEA 2000 parsing error:", e.message);
220 }
221
222 return parsedValues;
223}

Callers

nothing calls this directly

Calls 3

extractPGNFunction · 0.85
pushMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected