Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How to continue after error on serial port? #252

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

Closed
Artyrm opened this issue Aug 17, 2021 · 3 comments
Closed

How to continue after error on serial port? #252

Artyrm opened this issue Aug 17, 2021 · 3 comments

Comments

@Artyrm
Copy link

Artyrm commented Aug 17, 2021

If serial port being accessed by another application, or if it was briefly disconnected, device.reader gives an error, closes port and stops _packet_listener. But I want to reconnect after that. How to do it?

device.close() and device.open() sequence takes too much time (~1min) to use it.

@tatianaleon
Copy link
Contributor

Hi @Artyrm,

The serial port were an XBee is connected must only be used by one application. In this case, by the one using the library. There is no other mechanism to open/close the local connection with an XBee.

Best Regards.

@Artyrm
Copy link
Author

Artyrm commented Aug 18, 2021

Hi @tatianaleon

Thank you for your reply.
Well, I believe since on any problems accessing serial reader (more precisely, packet_listener) is stopped (02febb6), there should be a way to restart it. It is critical, to say, on a device that run 24/7 unattended.

For now I came up with that:

   def run_reader(device):
      device._packet_listener.run()
      return True             
...
              if not device._packet_listener.is_running():
                    screen.print_at("Reader is down. See log for more info. Trying to rerun... ", 0, screen.height-1)
                    if not device.serial_port.is_open:
                        device.serial_port.open()
                    time.sleep(0.2)
                    if device.serial_port.is_open:
                        print("Serial port opened. ", end='')
                        screen.refresh()
                        task = threading.Thread(target=run_reader, args=[device], name="Run_reader", daemon=True)
                        screen.refresh()
                        task.start()
                        time.sleep(0.2)
                        if device._packet_listener.is_running():
                            print("Reader run again.", end='')
                            screen.refresh()

Note the threaded run of __packet_listener that I'm doing from the other thread, because it is blocking. Maybe I'll add check for thread existence later, to prevent occasional threads multiplication. For now it behaves like OK solution, reconnects in less than a second.

@Artyrm
Copy link
Author

Artyrm commented Aug 19, 2021

Well, #252 (comment) seems suitable.
If somebody will come up with better idea, please, feel free.

@Artyrm Artyrm closed this as completed Aug 19, 2021
tatianaleon added a commit that referenced this issue Aug 25, 2021
This commit configures exclusive access for the provided XBee serial port by
default. This can be overridden in the constructor (although not recommended)

#222
#252

Signed-off-by: Tatiana Leon <[email protected]>
tatianaleon added a commit that referenced this issue Aug 25, 2021
tatianaleon added a commit that referenced this issue Sep 2, 2021
This commit configures exclusive access for the provided XBee serial port by
default. This can be overridden in the constructor (although not recommended)

#222
#252

Signed-off-by: Tatiana Leon <[email protected]>
tatianaleon added a commit that referenced this issue Sep 2, 2021
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