Skip to content

Commit

Permalink
Merge pull request #40 from peuter/update-serialport
Browse files Browse the repository at this point in the history
Update serialport lib to recent v12
  • Loading branch information
ruudverheijden authored Mar 22, 2024
2 parents 166ec74 + 111fc05 commit 8a78cb0
Show file tree
Hide file tree
Showing 3 changed files with 333 additions and 1,136 deletions.
72 changes: 39 additions & 33 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const EventEmitter = require('events');
const util = require('util');
let SerialPort = require('serialport');
let { SerialPort } = require('serialport');
const { DelimiterParser } = require('@serialport/parser-delimiter');

let connectedToSmartMeter = false;
let constructor;
Expand Down Expand Up @@ -42,7 +43,8 @@ function _setupSerialConnection(port, baudRate, parity, dataBits, stopBits) {
debug.log('Trying to connect to Smart Meter via port: ' + port + ' (BaudRate: ' + baudRate + ', Parity: ' + parity + ', Databits: ' + dataBits + ', Stopbits: ' + stopBits + ')');

// Open serial port connection
const sp = new SerialPort(port, {
const sp = new SerialPort({
path: port,
baudRate: baudRate,
parity: parity,
dataBits: dataBits,
Expand All @@ -56,43 +58,42 @@ function _setupSerialConnection(port, baudRate, parity, dataBits, stopBits) {
}
}, 11000); // Set time for 11 seconds, since we should receive at least one message every 10 seconds from the Smart Meter

let received = '';
const parser = sp.pipe(new DelimiterParser({
delimiter: config.stopCharacter
}));

sp.on('open', () => {
debug.log('Serialport connection opened, trying to receive data from the Smart Meter...');

sp.on('data', data => {
received += data.toString();
parser.on('data', data => {
const received = data.toString();

const startCharPos = received.indexOf(config.startCharacter);
const endCharPos = received.indexOf(config.stopCharacter);

// Package is complete if the start- and stop character are received
if (startCharPos >= 0 && endCharPos >= 0) {
const packet = received.substr(startCharPos, endCharPos - startCharPos);
const parsedPacket = parsePacket(packet);

received = '';
const startCharPos = received.indexOf(config.startCharacter);
if (startCharPos === -1) {
debug.log('no message start found in', received);
return;
}
const packet = received.substring(startCharPos);

const parsedPacket = parsePacket(packet);
// Emit a 'connected' event when we have actually successfully parsed our first data
if (!connectedToSmartMeter && parsedPacket.timestamp !== null) {
debug.log('Connection with Smart Meter established');
constructor.emit('connected');
connectedToSmartMeter = true;
clearTimeout(connectionTimeout);
}

// Emit a 'connected' event when we have actually successfully parsed our first data
if (!connectedToSmartMeter && parsedPacket.timestamp !== null) {
debug.log('Connection with Smart Meter established');
constructor.emit('connected');
connectedToSmartMeter = true;
clearTimeout(connectionTimeout);
}
debug.writeToLogFile(packet, parsedPacket);

debug.writeToLogFile(packet, parsedPacket);
constructor.emit('reading-raw', packet);

constructor.emit('reading-raw', packet);
if (parsedPacket.timestamp !== null) {
constructor.emit('reading', parsedPacket);
} else {
constructor.emit('error', 'Invalid reading');
}
});

if (parsedPacket.timestamp !== null) {
constructor.emit('reading', parsedPacket);
} else {
constructor.emit('error', 'Invalid reading');
}
}
});
sp.on('open', () => {
debug.log('Serialport connection opened, trying to receive data from the Smart Meter...');
});

sp.on('error', (error) => {
Expand All @@ -104,4 +105,9 @@ function _setupSerialConnection(port, baudRate, parity, dataBits, stopBits) {
debug.log('Connection closed');
constructor.emit('close');
});

constructor.close = (cb) => {
debug.log('Closing connection');
sp.close(cb);
};
}
Loading

0 comments on commit 8a78cb0

Please sign in to comment.