@@ -58,6 +58,7 @@ To add a new serial device, you must add an object to
5858 // on("open", () => ... ) connection opened
5959 // on("close", () => ... ) connection closed
6060 // 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
6162 // on("packet", (type,data) => ... ) when a packet is received (if .parsePackets=true)
6263 // on("ack", () => ... ) when an ACK is received (if .parsePackets=true)
6364 // 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
7677 rxDataHandlerLastCh = 0 ; // used by rxDataHandler - last received character
7778 rxDataHandlerPacket = undefined ; // used by rxDataHandler - used for parsing
7879 rxDataHandlerTimeout = undefined ; // timeout for unfinished packet
80+ rxLine = "" ; // current partial line for on("line" event
7981 progressAmt = 0 ; // When sending a file, how many bytes through are we?
8082 progressMax = 0 ; // When sending a file, how long is it in bytes? 0 if not sending a file
8183
@@ -154,6 +156,13 @@ To add a new serial device, you must add an object to
154156 // forward any data
155157 if ( this . cb ) this . cb ( data ) ;
156158 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 ) ;
157166 }
158167 }
159168
@@ -167,6 +176,7 @@ To add a new serial device, you must add an object to
167176 this . hadData = false ;
168177 this . flowControlWait = 0 ;
169178 this . rxDataHandlerLastCh = 0 ;
179+ this . rxLine = "" ;
170180 if ( ! this . isOpen ) {
171181 this . isOpen = true ;
172182 this . emit ( "open" ) ;
@@ -177,14 +187,15 @@ To add a new serial device, you must add an object to
177187
178188 /** Called when the connection is closed - resets any stored info/rejects promises */
179189 closeHandler ( ) {
180- log ( 1 , "Disconnected" ) ;
181190 this . isOpening = false ;
182191 this . txInProgress = false ;
183192 this . txDataQueue = [ ] ;
184193 this . hadData = false ;
185194 if ( this . isOpen ) {
195+ log ( 1 , "Disconnected" ) ;
186196 this . isOpen = false ;
187197 this . emit ( "close" ) ;
198+ connection = undefined ;
188199 }
189200 }
190201
@@ -549,7 +560,7 @@ To add a new serial device, you must add an object to
549560 /**
550561 * List ports available over all configured devices.
551562 * `shouldCallAgain` mean that more devices may appear later on (eg. Bluetooth LE)
552- * @param {(ports, shouldCallAgain) => void } callback
563+ * @param {(ports, shouldCallAgain) => void } callback
553564 */
554565 var getPorts = function ( callback ) {
555566 var newPortToDevice = { } ;
0 commit comments