Created
June 4, 2018 16:26
-
-
Save transducer/c479a4e70c0e09aba23c481f55caebcb to your computer and use it in GitHub Desktop.
Read P1 port
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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