Skip to content

Commit c927517

Browse files
author
Ritika Mishra
committed
improved logic for reconnection
1 parent 6b8d789 commit c927517

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

src/components/Connection.tsx

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,6 @@ const Connection: React.FC<ConnectionProps> = ({
384384
[]
385385
);
386386

387-
388-
389387
const handleClick = () => {
390388
// Function to handle toggle for connect/disconnect button
391389
if (isConnected) {
@@ -424,28 +422,29 @@ const Connection: React.FC<ConnectionProps> = ({
424422
);
425423
}) || null;
426424
}
427-
428425
// Request a new port if no saved port matches
429-
port = await navigator.serial.requestPort();
430-
const newPortInfo = await port.getInfo();
431-
const usbVendorId = newPortInfo.usbVendorId ?? 0;
432-
const usbProductId = newPortInfo.usbProductId ?? 0;
433-
434-
// Match the board from BoardsList
435-
const board = BoardsList.find(
436-
(b) => parseInt(b.field_pid, 10) === usbProductId
437-
);
438-
439-
let baudRate = board ? parseInt(board.baud_Rate, 10) : 0;
440-
let serialTimeout = board ? parseInt(board.serial_timeout, 10) : 0;
426+
// port = await navigator.serial.requestPort();
441427

428+
let baudRate;
429+
let serialTimeout
442430

443431
if (!port) {
444432
port = await navigator.serial.requestPort();
433+
// const newPortInfo = await port.getInfo();
445434
const newPortInfo = await port.getInfo();
435+
// const usbVendorId = newPortInfo.usbVendorId ?? 0;
436+
const usbProductId = newPortInfo.usbProductId ?? 0;
437+
438+
// Match the board from BoardsList
439+
const board = BoardsList.find(
440+
(b) => parseInt(b.field_pid, 10) === usbProductId
441+
);
442+
443+
baudRate = board ? parseInt(board.baud_Rate, 10) : 0;
444+
serialTimeout = board ? parseInt(board.serial_timeout, 10) : 0;
446445

447446
const usbVendorId = newPortInfo.usbVendorId ?? 0;
448-
const usbProductId = newPortInfo.usbProductId ?? 0;
447+
// const usbProductId = newPortInfo.usbProductId ?? 0;
449448

450449
const existingDevice = savedPorts.find(
451450
(saved: SavedDevice) =>
@@ -454,14 +453,21 @@ const Connection: React.FC<ConnectionProps> = ({
454453
);
455454

456455
if (!existingDevice) {
457-
savedPorts.push({ usbVendorId, usbProductId, baudRate });
456+
savedPorts.push({ usbVendorId, usbProductId, baudRate, serialTimeout });
458457
localStorage.setItem('savedDevices', JSON.stringify(savedPorts));
459458
}
460459

461460
await port.open({ baudRate });
462461
} else {
463-
const newPortInfo = port.getInfo();
464-
const usbProductId = newPortInfo.usbProductId ?? 0;
462+
const info = port.getInfo();
463+
464+
const savedDevice = savedPorts.find(
465+
(saved: SavedDevice) =>
466+
saved.usbVendorId === (info.usbVendorId ?? 0) &&
467+
saved.usbProductId === (info.usbProductId ?? 0)
468+
);
469+
const baudRate = savedDevice.baudRate || 'default_baud_rate'; // Use baudRate from saved device info
470+
serialTimeout = savedDevice.serialTimeout;
465471

466472
await port.open({ baudRate });
467473
}
@@ -477,7 +483,6 @@ const Connection: React.FC<ConnectionProps> = ({
477483
const whoAreYouMessage = new TextEncoder().encode("WHORU\n");
478484

479485
setTimeout(() => writer.write(whoAreYouMessage), serialTimeout);
480-
481486
let buffer = "";
482487
while (true) {
483488
const { value, done } = await reader.read();

src/components/boards.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const BoardsList = Object.freeze([
77
channel_count: "6",
88
sampling_rate: "250",
99
serial_timeout: "2000",
10-
baud_Rate: "115200"
10+
baud_Rate: "230400"
1111
},
1212
{
1313
chords_id: "NANO-CLONE",
@@ -27,7 +27,7 @@ export const BoardsList = Object.freeze([
2727
channel_count: "6",
2828
sampling_rate: "250",
2929
serial_timeout: "2000",
30-
baud_Rate: "115200"
30+
baud_Rate: "230400"
3131
},
3232
{
3333
chords_id: "UNO-R4",

0 commit comments

Comments
 (0)