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

Request: timeout on wait_for_tag #44

Open
tlongeri opened this issue Jul 20, 2018 · 2 comments
Open

Request: timeout on wait_for_tag #44

tlongeri opened this issue Jul 20, 2018 · 2 comments

Comments

@tlongeri
Copy link

It would be nice to have a timeout argument on the wait_for_tag function. I'm using it to listen for tags on a looping thread that checks on a 'global' exit flag at each loop, but since wait_for_tag can never exit I have no nice way of killing it so I was forced to make my own modified version.

It should be a simple change, like this:

def wait_for_tag(self, timeout):
	# enable IRQ on detect
	start_time = time.time()
	rdr.init()
	rdr.irq.clear()
	rdr.dev_write(0x04, 0x00)
	rdr.dev_write(0x02, 0xA0)
	# wait for it
	waiting = True
	while waiting and time.time() - startTime < timeout:
		rdr.dev_write(0x09, 0x26)
		rdr.dev_write(0x01, 0x0C)
		rdr.dev_write(0x0D, 0x87)
		waiting = not rdr.irq.wait(0.1)
	rdr.irq.clear()
	self.init()

It's just a little detail that would improve usability.

@bluepuma77
Copy link

Just wanted to open a new feature request for rdr.wait_for_tag(timeout=10).

So GitHub "similar issues" works 😃

jo-me added a commit to jo-me/pi-rc522 that referenced this issue Jun 17, 2020
Based on @tlongeri 's suggestion in ondryaso#44
I addded the timeout functionality to the wait_for_tag method so that it will return after a given number of seconds. 
The timeout param is optional with default value of 0 (=no timeout) so that existing users of the library will not have any change in functionality unless they provide the new param.

This functionality is useful for detecting that the reader could not find a RFID tag, e.g. the card was removed.
@topermaper
Copy link

topermaper commented Jul 19, 2024

Hi @ondryaso, @jo-me ,

wait_for_tag now returns after timeout seconds but unfortunatelly there is no way to know whether it returned normally or because the time out. In case there is a timeout when i call self._reader.request() I would get an error.

Would be possible to use a Timeout Exception so you can handle the timeout in the caller method. Something like this:

try:
    self._reader.wait_for_tag(timeout=1)
except TimeoutError:
    return None

error, tag_type = self._reader.request()

if error:
    raise RfidReadException("Error reading rfid tag")
def wait_for_tag(self, timeout=None):
    if self.pin_irq is None:
        raise NotImplementedError('Waiting not implemented if IRQ is not used')

    # enable IRQ on detect
    self.init()
    self.irq.clear()
    self.dev_write(0x04, 0x00)
    self.dev_write(0x02, 0xA0)
    # wait for it
    start_time = time.time()
    waiting = True

    while waiting:
        if timeout and (time.time() - start_time) >= timeout:
            raise TimeoutError

        self.init()
        self.dev_write(0x04, 0x00)
        self.dev_write(0x02, 0xA0)

        self.dev_write(0x09, 0x26)
        self.dev_write(0x01, 0x0C)
        self.dev_write(0x0D, 0x87)
        waiting = not self.irq.wait(0.1)

    self.irq.clear()
    self.init()

Should i raise a new issue or could this be reopened ?

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

3 participants