Listen to data with Parser in serialport.io

Internet of Things IoT Frameworks 3 years ago

2.44K 0 0 0 0

_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?

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

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.

Similar Forum


Q

Azure IoT hub and sending messages with mosquitto_pub

_x000D_ _x000D_ I'm trying to send some simple message with mosquitto_pub to Azure IoT HUB but faced...
Q

How to generate AWS bootstrap certificates with java SDK?

_x000D_ _x000D_ According to this docs I need to generate so-called bootstrap certificates for my Io...
Q

is it possible to do 3way handshake only one time with mqtt communication?

_x000D_ _x000D_ I am using mosquitto_pub to publish the data with TLS using a topic. I am using mosq...

Important Internet of Things Links