Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSerial driver attempts to open the serial port twice #193

Open
alexrudd2 opened this issue Mar 25, 2025 · 0 comments
Open

WebSerial driver attempts to open the serial port twice #193

alexrudd2 opened this issue Mar 25, 2025 · 0 comments

Comments

@alexrudd2
Copy link
Owner

Image

(I've added a few print statements). Notice how the code tries to open an already-opened serial port. First the onClick() handler connects, and then useEffect() attempts again.

saxi/src/ui.tsx

Lines 1137 to 1170 in 80cfa1b

function PortSelector({ driver, setDriver, hardware }: PortSelectorProps) {
const [initializing, setInitializing] = useState(false);
useEffect(() => {
(async() => {
try {
const ports = await navigator.serial.getPorts();
const port = ports[0];
if (port) {
console.log('connecting to', port);
// get the first
setDriver(await WebSerialDriver.connect(port, hardware));
}
} finally {
setInitializing(false);
}
})();
});
return <>
{driver ? `Connected: ${driver.name()}` : null}
{!driver ?
<button
type="button"
disabled={initializing}
onClick={async() => {
try {
const port = await navigator.serial.requestPort({ filters: [{ usbVendorId: 0x04D8, usbProductId: 0xFD92 }] });
// TODO: describe why we close if we already checked that driver is null
// await driver?.close()
setDriver(await WebSerialDriver.connect(port, hardware));
} catch (e) {
alert(`Failed to connect to serial device: ${e.message}`);
console.error(e);
}
}}

@jedahan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant