You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Parsers are now transform streams! I'm not 100% convinced this is easier to use but it is more clear about the mechanics. I think that might make it worthwhile. It also opens up a clear example to how to make your own parser.
Parsers are [Transform streams](https://nodejs.org/api/stream.html#stream_class_stream_transform) that will parse data in a variety of ways and can be used to process incoming data.
550
+
551
+
**Kind**: static property of <code>[SerialPort](#exp_module_serialport--SerialPort)</code>
552
+
**Properties**
553
+
554
+
| Name | Type | Description |
555
+
| --- | --- | --- |
556
+
| ByteLength | <code>function</code> | is a transform stream that emits data each time a byte sequence is received. |
557
+
| Delimiter | <code>function</code> | is a transform stream that emits data as a buffer after a specific number of bytes are received. |
558
+
| ReadLine | <code>function</code> | is a transform stream that emits data after a newline delimiter is received. |
559
+
560
+
**Example**
561
+
To use any of the parsers you need to create them and then pipe the serialport to the parser. Be sure not to write to the parser.
562
+
```js
563
+
var SerialPort =require('serialport');
564
+
var port =newSerialPort('/dev/tty-usbserial1');
565
+
var parser =newSerialPort.parsers.ReadLine();
566
+
port.pipe(parser);
567
+
parser.on('data', console.log);
568
+
port.write('ROBOT PLEASE RESPOND\n');
569
+
570
+
// this can be shortened to
571
+
var SerialPort =require('serialport');
572
+
var parser =port.pipe(newSerialPort.parsers.ReadLine());
573
+
parser.on('data', console.log);
574
+
port.write('ROBOT PLEASE RESPOND\n');
575
+
```
576
+
577
+
To use the byte length parser, you must provide the length of the number of bytes:
578
+
```js
579
+
var SerialPort =require('serialport');
580
+
var port =newSerialPort('/dev/tty-usbserial1');
581
+
var parser =port.pipe(newSerialPort.parsers.ByteLength({length:8}));
582
+
port.pipe(parser);
583
+
parser.on('data', console.log);
584
+
```
585
+
586
+
To use the Delimiter parser you must specify, you must provide a delimiter as a string, buffer, or an array of bytes:
587
+
```js
588
+
var SerialPort =require('serialport');
589
+
var port =newSerialPort('/dev/tty-usbserial1');
590
+
var parser =newSerialPort.parsers.Delimiter({delimiter:newBuffer('EOL')});
591
+
port.pipe(parser);
592
+
parser.on('data', console.log);
593
+
```
594
+
595
+
To use the ReadLine parser, you must provide a delimiter as such:
596
+
```js
597
+
var SerialPort =require('serialport');
598
+
var SerialPort =require('serialport');
599
+
var parser =port.pipe(newSerialPort.parsers.ReadLine({delimiter:'\r\n'}));
| parser | <code>function</code> | <code>Parsers.raw</code> | The parser to transform read data, defaults to the `raw` parser that emits data as it's received. |
638
638
| platformOptions | <code>object</code> || sets platform specific options |
639
639
| platformOptions.vmin | <code>number</code> | <code>1</code> | see [`man termios`](http://linux.die.net/man/3/termios)|
640
640
| platformOptions.vtime | <code>number</code> | <code>0</code> | see [`man termios`](http://linux.die.net/man/3/termios)|
0 commit comments