Skip to content

Commit

Permalink
WebSerial improvements (correctly shut down on disconnect so we don't…
Browse files Browse the repository at this point in the history
… hog the port)
  • Loading branch information
gfwilliams committed Sep 13, 2024
1 parent a155e58 commit 31bd1c0
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions core/serial_web_serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

var serialPort;
var serialPortReader;
var connectionDisconnectCallback;

function init() {
Espruino.Core.Config.add("WEB_SERIAL", {
Expand Down Expand Up @@ -101,8 +100,8 @@
Espruino.Core.Status.setStatus("Connecting to serial port");
serialPort = port;
var br = parseInt(Espruino.Config.BAUD_RATE);
return port.open({
baudrate: br/*old*/,
return port.open({
baudrate: br/*old*/,
baudRate: br/*new*/,
dataBits: 8, databits: 8,
stopBits: 1, stopbits: 8,
Expand All @@ -114,22 +113,35 @@
serialPortReader = serialPort.readable.getReader();
serialPortReader.read().then(function ({ value, done }) {
serialPortReader.releaseLock();
serialPortReader = undefined;
if (value) {
receiveCallback(value.buffer);
}
if (done) {
disconnectCallback();
console.log("Serial> serialPortReader done");
if (serialPort) {
console.log("Serial> serialPort.close()");
serialPort.close();
serialPort = undefined;
if (disconnectCallback) disconnectCallback();
disconnectCallback = undefined;
}
} else {
readLoop();
}
}).catch(function() {
serialPortReader.releaseLock();
console.log("Serial> serialPortReader cancelled");
}).catch(function(e) {
serialPortReader.releaseLock();
console.log("Serial> serialPortReader rejected", e);
});
}
serialPort.addEventListener("disconnect", (event) => {
console.log("Serial> Port disconnected");
disconnectCallback();
console.log("Serial> Port disconnected", event);
if (serialPort) {
serialPort.close();
serialPort = undefined;
}
if (disconnectCallback) disconnectCallback();
disconnectCallback = undefined;
});
readLoop();
Espruino.Core.Status.setStatus("Serial connected. Receiving data...");
Expand All @@ -139,22 +151,17 @@
openCallback({ portName : getSerialDeviceInfo(serialPort).path });
}).catch(function(error) {
console.log('Serial> ERROR: ' + error);
disconnectCallback();
pairedDevices = pairedDevices.filter(dev=>getSerialDeviceInfo(dev).path != path); // error connecting, remove from paired devices
if (disconnectCallback) disconnectCallback();
disconnectCallback = undefined;
});
}

function closeSerial() {
if (serialPort) {
serialPortReader.cancel().then(function() {
serialPort.close();
serialPort = undefined;
serialPortReader = undefined;
});
}
if (connectionDisconnectCallback) {
connectionDisconnectCallback();
connectionDisconnectCallback = undefined;
}
if (serialPortReader)
serialPortReader.cancel();
/* serialPortReader will handle tidying up
and calling disconnect */
}

function writeSerial(data, callback) {
Expand All @@ -166,7 +173,7 @@
writer.releaseLock();
console.log('Serial> SEND ERROR: ' + error);
closeSerial();
});
});
}

// ----------------------------------------------------------
Expand Down

0 comments on commit 31bd1c0

Please sign in to comment.