@@ -58,6 +58,7 @@ To add a new serial device, you must add an object to
58
58
// on("open", () => ... ) connection opened
59
59
// on("close", () => ... ) connection closed
60
60
// on("data", (data) => ... ) when data is received (as string)
61
+ // on("line", (line) => ... ) when a line of data is received (as string), uses /r OR /n for lines
61
62
// on("packet", (type,data) => ... ) when a packet is received (if .parsePackets=true)
62
63
// on("ack", () => ... ) when an ACK is received (if .parsePackets=true)
63
64
// on("nak", () => ... ) when an ACK is received (if .parsePackets=true)
@@ -76,6 +77,7 @@ To add a new serial device, you must add an object to
76
77
rxDataHandlerLastCh = 0 ; // used by rxDataHandler - last received character
77
78
rxDataHandlerPacket = undefined ; // used by rxDataHandler - used for parsing
78
79
rxDataHandlerTimeout = undefined ; // timeout for unfinished packet
80
+ rxLine = "" ; // current partial line for on("line" event
79
81
progressAmt = 0 ; // When sending a file, how many bytes through are we?
80
82
progressMax = 0 ; // When sending a file, how long is it in bytes? 0 if not sending a file
81
83
@@ -154,6 +156,13 @@ To add a new serial device, you must add an object to
154
156
// forward any data
155
157
if ( this . cb ) this . cb ( data ) ;
156
158
this . emit ( 'data' , data ) ;
159
+ // look for newlines and send out a 'line' event
160
+ let lines = ( this . rxLine + data ) . split ( / \r \n / ) ;
161
+ while ( lines . length > 1 )
162
+ this . emit ( 'line' , lines . shift ( ) ) ;
163
+ this . rxLine = lines [ 0 ] ;
164
+ if ( this . rxLine . length > 10000 ) // only store last 10k characters
165
+ this . rxLine = this . rxLine . slice ( - 10000 ) ;
157
166
}
158
167
}
159
168
@@ -167,6 +176,7 @@ To add a new serial device, you must add an object to
167
176
this . hadData = false ;
168
177
this . flowControlWait = 0 ;
169
178
this . rxDataHandlerLastCh = 0 ;
179
+ this . rxLine = "" ;
170
180
if ( ! this . isOpen ) {
171
181
this . isOpen = true ;
172
182
this . emit ( "open" ) ;
@@ -177,14 +187,15 @@ To add a new serial device, you must add an object to
177
187
178
188
/** Called when the connection is closed - resets any stored info/rejects promises */
179
189
closeHandler ( ) {
180
- log ( 1 , "Disconnected" ) ;
181
190
this . isOpening = false ;
182
191
this . txInProgress = false ;
183
192
this . txDataQueue = [ ] ;
184
193
this . hadData = false ;
185
194
if ( this . isOpen ) {
195
+ log ( 1 , "Disconnected" ) ;
186
196
this . isOpen = false ;
187
197
this . emit ( "close" ) ;
198
+ connection = undefined ;
188
199
}
189
200
}
190
201
@@ -549,7 +560,7 @@ To add a new serial device, you must add an object to
549
560
/**
550
561
* List ports available over all configured devices.
551
562
* `shouldCallAgain` mean that more devices may appear later on (eg. Bluetooth LE)
552
- * @param {(ports, shouldCallAgain) => void } callback
563
+ * @param {(ports, shouldCallAgain) => void } callback
553
564
*/
554
565
var getPorts = function ( callback ) {
555
566
var newPortToDevice = { } ;
0 commit comments