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

Add a callback to the Serial class fire when its open #283

Open
mbcoder opened this issue Jul 4, 2023 · 3 comments
Open

Add a callback to the Serial class fire when its open #283

mbcoder opened this issue Jul 4, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@mbcoder
Copy link

mbcoder commented Jul 4, 2023

I've just starting using the Pi4J libs and have to say its a great API with solid documentation which got me going really quickly.

I've used Pi4J in this example GPS data logger blog and when I had opened a serial port I had the following code to wait until the port was open:

            while (!serial.isOpen()) {
                try {
                    Thread.sleep(250);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

I was thinking a nice enhancement to the API would be to add a listener to the serial class which would fire once the serial port is ready to read from. If this existed then the above code could be replaced with something like:

serial.addOpenedListener(() -> {//do something now the serial port is open});

If you think this is a worthwhile addition I'd be happy to make a PR for review.

@FDelporte
Copy link
Member

We had a similar idea in the past, see #118, with a reference to the implementation in V1.
Maybe we should indeed consider extending the serial implementation.

@FDelporte FDelporte added the enhancement New feature or request label Jul 5, 2023
@eitch
Copy link
Member

eitch commented Jul 6, 2023

The idea is great. Do you mind creating a PR? We gladly accept PRs.

@DieterHolz
Copy link

Wouldn't that more or less mean, that your whole application is inside the lambda expression of 'addOpenedListener'?

How about doing the 'wait' inside Pi4J instead.

When calling

pi4j.create(Serial.newConfigBuilder(pi4j)...

it does not return until 'Serial' is open and ready for being used.

If that kind of synchronous call is not acceptable then provide two methods: 'serial.open' will wait until serial is ready and 'serial.asynOpen(onFinishedHandler)' will do the opening asynchronously.

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

No branches or pull requests

4 participants