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

Not able to re-open a closed port #18

Closed
Ssercon opened this issue Jun 12, 2022 · 2 comments
Closed

Not able to re-open a closed port #18

Ssercon opened this issue Jun 12, 2022 · 2 comments

Comments

@Ssercon
Copy link

Ssercon commented Jun 12, 2022

I have the package working pretty solidly for reading out data from a serial port. However when I try to re-open a port after closing it I am not able to do so and the SerialPort() function does not throw any exceptions.

I use this to connect to a serial port:

    try {
      port = SerialPort(dropdownvalue, openNow: true, ByteSize: 8);
    } on Exception catch (_) {
      print("can't open port, exiting...");
      return;
    }

The first time it works like a charm. Then I disconnect the port by calling:

opened_port.closeOnListen(
      onListen: () => print(opened_port.isOpened),
    )
      ..onError((err) {
        print(err);
      })
      ..onDone(() {
        print("${opened_port.portName} is closed");
        print(opened_port.isOpened);
      });

The port is closed properly because I am able to open it with other software but when I try to connect to it again with SerialPort() it doesn't fail but the COM port is not opened. Not sure if this is a bug in the close function or that I am missing something. Help would be appriciated, thanks!

@FengChendian
Copy link
Owner

FengChendian commented Jun 12, 2022

Do you mean you use port = SerialPort(dropdownvalue, openNow: true, ByteSize: 8); to connect again?

I think the bug is due to Singleton mode.
The last SerialPort hasn't been recycled by dart VM. So you re-create the same instance as the last. That means you fetch an old instance from the cache rather than init a new class. The old instance doesn't run the init method and using parameters that you have edited.

Maybe using open() method to force open can resolve this problem.

@Ssercon
Copy link
Author

Ssercon commented Jun 12, 2022

I can confirm that switching the first code snippet to:

      try {
      port = SerialPort(dropdownvalue, openNow: false, ByteSize: 8);
      port.open();
    } on Exception catch (_) {
      print("can't open port, exiting...");
      return;
    }

And thus using port.open() instead of opening the port during initalisation fixes the problem. Closing the issue, thanks!

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

2 participants