Listen to data with Parser in serialport.io

Internet of Things IoT Frameworks 2 years ago

0 0 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_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?

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (0)

Post Answer

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links