Skip to content

Commit 554f89b

Browse files
committed
update with latest tweaks from EspruinoWebTools uart.js
1 parent 2b601e0 commit 554f89b

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

core/serial.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = {};

core/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ while (d!==undefined) {console.log(btoa(d));d=f.read(${CHUNKSIZE});}
955955
case "true" : tok = lex.next(); return true;
956956
case "false" : tok = lex.next(); return false;
957957
case "null" : tok = lex.next(); return null;
958+
case "NaN" : tok = lex.next(); return NaN;
958959
}
959960
if (tok.str == "[") {
960961
tok = lex.next();

0 commit comments

Comments
 (0)