Internet of Things IoT Frameworks . 1 year ago

Listen to data with Parser in serialport.io

Listen to data with Parser in serialport.io

0 views   0   0 likes   0 shares Tuteehub forum manpreet 0 answers
_x000D_ _x000D_ I am using serialport.io node package for listening a message on serial port. Below is my code snippet. import SerialPort from 'serialport'; import Readline from '@serialport/parser-readline'; export default class MySerialPort { static sharedInstance = MySerialPort.sharedInstance == null ? new MySerialPort() : this.sharedInstance; serialPort; parser; getPort = () => { if (this.serialPort == null) { console.log('creating port'); this.openPort(); this.parser = this.serialPort.pipe(new Readline({ delimiter: '\n' })); } return this.serialPort; }; /** * */ readParsedMessage= (callback: fn) => { if (!this.parser) { this.getPort(); } this.parser.on('data', callback); }; readMessage = (callback :fn) =>{ this.getPort().on('data', callback) } /** * Open port with given configuration. If serial port object exist, return its instance * else create new */ openPort = (address: string = '/dev/ttyUSB0', baudRate: number = 115200) => { this.serialPort = new SerialPort(address, { baudRate }); }; } In above snippet when I register a listener on readMessage function, I get byte array whenever data is received on serial port. However a listener on readParsedMessage returns me parsed data only once (after hitting a delimiter). It dies after first delimiter reception. Is it an expected behaviour? If yes, how can I set it in continuous listening mode? If no, where exactly my code is going wrong?
tuteehub_quiz
Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

profilepic.png

Tuteehub forum answer Answers (0)


Related Tags