Skip to content

Auto-reconnecting guidance needed #156

Open
@dattasaurabh82

Description

As one might have guessed, it is not a bug report and an issue.

Also thanks a lot for putting such a good and useful API available to us.   🙏🏽

So I was looking into:

// -- Handle Events for physical re-connection of serial port by users
navigator.serial.addEventListener("connect", (event) => {
    // TODO: Automatically open event.target or warn user a port is available.
});

navigator.serial.addEventListener("disconnect", (event) => {
    // TODO: Remove |event.target| from the UI.
    // If the serial port was opened, a stream error would be observed as well.
});

My intention:

  • User connects to a serial port by some click event
  • User on physically disconnecting, handle the "proper disconnection".
    1. what do you mean by Remove event.target...? I have just set my serial port object to null;
    2. What else is needed here to handle?
  • The user physically connects the HW device back and the serial port opens again.
    1. The intention is that the old serial port object shall now not remain null anymore.
    2. And should open again.

So this is what I was doing:

// -- Handle Events for physical re-connection of serial port by users
navigator.serial.addEventListener("connect", (event) => {
        // Old serial port object which was used to open the specific serial port re-assigned again, 
        // is now assigned as the event target. 
        port = await event.target || event.port;  

       try {
            await port.open({ baudRate: 9600});
            // after that do other things if needed
            // set up encode stream etc. 
       } catch (e) {
            console.log(e);
       }
});

But when I physically disconnect and reconnect back, I get:
DOMException: Failed to open serial port.

But the Serial port is not null (I checked).

I would highly appreciate if you guys can fill this section again in : https://web.dev/serial/ and the Explainer

Meanwhile it would be very kind and helpful, if someone can show me how this part should be properly handled.
Something to do with bubbles ?

Also these events are always firing twice !🤔

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions