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

Closing and reopening the serial port during runtime #45

Open
PatcherMustermann opened this issue Nov 20, 2024 · 5 comments
Open

Closing and reopening the serial port during runtime #45

PatcherMustermann opened this issue Nov 20, 2024 · 5 comments

Comments

@PatcherMustermann
Copy link

PatcherMustermann commented Nov 20, 2024

Before HomeKey, I used my own system (read tag with security feature). I would like to continue using this system in parallel to HomeKey and would therefore like to switch the comport to my software when one of these tags is recognized. To do this, however, I would need to close the comport in HomeKey during runtime and open it again after my software has been processed.

Example in the service.py:

    def _read_homekey(self):
        start = time.monotonic()

        remote_target = self.clf.sense(
            RemoteTarget("106A"),
            broadcast=ECP.home(
                identifier=self.repository.get_reader_group_identifier(),
                flag_2=self.express,
            ).pack(),
        )
        if remote_target is None:
            return

        target = activate(self.clf, remote_target)
        if target is None:
            return

        if not isinstance(target, ISODEPTag):
            log.info(
                f"Found non-ISODEP Tag with UID: {target.identifier.hex().upper()}"
            )
----------> Close serial port/connection
            requests.post('http...
----------> Reopen serial port/connection
            while self.clf.sense(RemoteTarget("106A")) is not None:
                log.info("Waiting for target to leave the field...")
                time.sleep(0.5)
            return

What would the syntax have to be to accomplish this?

Thank you very much for your support!

Greetings
Patcher

@kormax
Copy link
Owner

kormax commented Nov 20, 2024

You can raise an exception, which will cause the code to re-initialize the connection to the CLF after some delay. In the meanwhile, you can grab access to that port in exclusive fashion from your other app, which should prevent this one from getting it back until you release it there.

@PatcherMustermann
Copy link
Author

PatcherMustermann commented Nov 20, 2024

Okay. But how can I trigger such an exception that the comport is released again? Do I simply kill the HomeKey script? Just for your information, I run the whole thing under Windows 11.

@kormax
Copy link
Owner

kormax commented Nov 20, 2024

Just do raise Exception("Gimme port")or something like that in the part of the code you want this app to release access to the PN532. It will unwind the stack up to the runner, which will wait for 5 seconds before attempting to initialize PN532 again.

@PatcherMustermann
Copy link
Author

Sorry, but I'm not really deeply versed in python. What is a raise Exception(“Gimme port”)? What should the command for this look like?

@kormax
Copy link
Owner

kormax commented Nov 25, 2024

Instead of raising an exception, you can also do something like this (right in the same part of the code you've suggested adding that logic to begin with):

# Close connection to PN532 to allow other application to connect to it
self.clf.close()
# Do your HTTP call here
# Re-open the connection back in order to gain back control of PN532
self.clf.open(self.clf.path)

For HTTP call, you can use requests library, there are tons of examples on the web on how to use it.

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