Skip to content

Instantly share code, notes, and snippets.

@transducer
Created June 4, 2018 16:26
Show Gist options
  • Save transducer/c479a4e70c0e09aba23c481f55caebcb to your computer and use it in GitHub Desktop.
Save transducer/c479a4e70c0e09aba23c481f55caebcb to your computer and use it in GitHub Desktop.
Read P1 port
/**
* Starts serial port reader. Retries via {@link tryInitP1} when port is
* disconnected or on error.
*
* @function tryInitP1
* @param {function} messageHandler What to do with the message
* @returns {undefined}
*/
function initP1(messageHandler) {
logger.info(`Initializing P1 reader on serial port ${RASPBERRY_PI_USB_PORT}`);
const TELEGRAM_SEPARATOR = '!';
const serialPort = new SerialPort(RASPBERRY_PI_USB_PORT, {
...p1Config,
parser: SerialPort.parsers.readline(TELEGRAM_SEPARATOR),
});
serialPort.on('data', telegram => messageHandler(telegram));
serialPort.on('error', (err) => {
logger.error(`Error when reading port ${err}`);
tryInitP1(messageHandler);
});
serialPort.on('close', () => {
logger.info('Port closed');
tryInitP1(messageHandler);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment