@bencevans/ve.direct
Version:
Victron VE.Direct Parser
41 lines (34 loc) • 1.72 kB
JavaScript
const test = require("tape");
const SerialPort = require("serialport");
const VEDirectParser = require("./parser");
const validMessage = Buffer.from([
0x0d, 0x0a, 0x50, 0x49, 0x44, 0x09, 0x30, 0x78, 0x32, 0x30, 0x33, 0x0d, 0x0a, 0x56, 0x09, 0x32, // |..PID.0x203..V.2|
0x36, 0x32, 0x30, 0x31, 0x0d, 0x0a, 0x49, 0x09, 0x30, 0x0d, 0x0a, 0x50, 0x09, 0x30, 0x0d, 0x0a, // |6201..I.0..P.0..|
0x43, 0x45, 0x09, 0x30, 0x0d, 0x0a, 0x53, 0x4f, 0x43, 0x09, 0x31, 0x30, 0x30, 0x30, 0x0d, 0x0a, // |CE.0..SOC.1000..|
0x54, 0x54, 0x47, 0x09, 0x2d, 0x31, 0x0d, 0x0a, 0x41, 0x6c, 0x61, 0x72, 0x6d, 0x09, 0x4f, 0x46, // |TTG.-1..Alarm.OF|
0x46, 0x0d, 0x0a, 0x52, 0x65, 0x6c, 0x61, 0x79, 0x09, 0x4f, 0x46, 0x46, 0x0d, 0x0a, 0x41, 0x52, // |F..Relay.OFF..AR|
0x09, 0x30, 0x0d, 0x0a, 0x42, 0x4d, 0x56, 0x09, 0x37, 0x30, 0x30, 0x0d, 0x0a, 0x46, 0x57, 0x09, // |.0..BMV.700..FW.|
0x30, 0x33, 0x30, 0x37, 0x0d, 0x0a, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x73, 0x75, 0x6d, 0x09, 0xd8, // |0307..Checksum..|
// From https://www.victronenergy.com/live/vedirect_protocol:faq
])
test('parser.checksum', t => {
t.plan(2)
t.equal(VEDirectParser.checksum(validMessage), 0)
t.notEqual(VEDirectParser.checksum(Buffer.from([0x34, 0x54])), 0)
})
test("parser", (t) => {
const rl = new SerialPort.parsers.Delimiter({
delimiter: Buffer.from([0x0d, 0x0a], 'hex'),
includeDelimiter: false
})
const parser = new VEDirectParser()
parser.on('data', data => {
console.log(data)
t.equal(data['Alarm'], 'OFF')
t.equal(data['Relay'], 'OFF')
t.end()
})
rl.pipe(parser)
rl.write(validMessage)
rl.write(Buffer.from([0x0d, 0x0a]))
});