Skip to content

Commit 677e786

Browse files
committed
Tidying up handling of saved webserial ports
1 parent 50591e5 commit 677e786

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

core/serial_web_serial.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,23 @@
158158
});
159159
readLoop();
160160
Espruino.Core.Status.setStatus("Serial connected. Receiving data...");
161-
if (!pairedDevices.includes(serialPort))
162-
pairedDevices.push(serialPort);
161+
let devicePath = getSerialDeviceInfo(serialPort).path;
162+
// remove any existing devices with the same USB ID
163+
pairedDevices = pairedDevices.filter(dev=>getSerialDeviceInfo(dev).path != devicePath);
164+
// Check there aren't too many devices
165+
while (pairedDevices.length>2) pairedDevices.pop();
166+
// add the current device
167+
pairedDevices.unshift(serialPort); // put this new serial port at the top
163168

164169
openCallback({ portName : getSerialDeviceInfo(serialPort).path });
165170
}).catch(function(error) {
166-
console.log('Serial> ERROR: ' + error);
167-
pairedDevices = pairedDevices.filter(dev=>getSerialDeviceInfo(dev).path != path); // error connecting, remove from paired devices
171+
console.log('Serial> ERROR: ' + error);
172+
if (serialPort) {
173+
pairedDevices = pairedDevices.filter(dev=>dev != serialPort); // error connecting, remove from paired devices
174+
closeSerial();
175+
}
168176
if (disconnectCallback) disconnectCallback();
169-
disconnectCallback = undefined;
177+
disconnectCallback = undefined;
170178
});
171179
}
172180

0 commit comments

Comments
 (0)