drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

only read a certain length of a output

Mon Jul 15, 2019 3:43 pm

Hi, i have a python script which reads a wiegand proximity reader input, displays the card number and if the user has been allowed access or denied. the script i posted below does accept any card presented to it but whats im actually after is the shorten the output length.

at the moment when i present the keyfob i get the following

bits=89 value=321749267849309967093760
door released
door secure

where the value is i only want to read the first or last 8 digits of that output (dosent matter if its first or last)

this is using the pigpio repo

any help appreciated, thanks

Code: Select all

            if self.code_timeout == 3: # both gpios timed out
               self.pi.set_watchdog(self.gpio_0, 0)
               self.pi.set_watchdog(self.gpio_1, 0)
               self.in_code = False
               self.callback(self.bits, self.num)

   def cancel(self):

      """
      Cancel the Wiegand decoder.
      """

      self.cb_0.cancel()
      self.cb_1.cancel()

if __name__ == "__main__":

   import time
   import RPi.GPIO as GPIO
   import pigpio
   import wiegand

   GPIO.setmode(GPIO.BCM)
   GPIO.setwarnings(False)

   def callback(bits, value):
      print("bits={} value={}".format(bits, value))
      GPIO.setup(18,GPIO.OUT)
      print("door released")
      GPIO.output(18,GPIO.LOW)
      time.sleep(5)
      print("door secure")
      GPIO.output(18, GPIO.HIGH)

   pi = pigpio.pi()

   w = wiegand.decoder(pi, 14, 15, callback)



   while True:
      time.sleep(60)

   w.cancel()

   pi.stop()

User avatar
joan
Posts: 14196
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: only read a certain length of a output

Mon Jul 15, 2019 4:34 pm

You could take the modulus of 100000000.

var = value % 100000000

Are you sure that's what you need? Normally you would be extracting a series of bits from the Wiegand code rather than a series of decimal digits.

drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: only read a certain length of a output

Mon Jul 15, 2019 4:38 pm

hi, so are you saying read bits of it such as every 4th number or something ?

thanks

drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: only read a certain length of a output

Mon Jul 15, 2019 4:47 pm

also sorry to jump on you joan but could you help with this

(env) pi@raspberrypi:~/acu $ python main.py
Traceback (most recent call last):
File "main.py", line 1, in <module>
from acu.app import App
File "/home/pi/acu/acu/app.py", line 2, in <module>
import pigpio
ModuleNotFoundError: No module named 'pigpio'

User avatar
joan
Posts: 14196
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: only read a certain length of a output

Mon Jul 15, 2019 4:55 pm

A Wiegand code is a series of bits. The meaning of the bits is controlled by the code writer. I don't know what all those 89 bits mean, perhaps some are a checksum. You need to find the documentation to see how the bits are to be interpreted.

Module not found means the module has not been installed or has not been installed in the standard place. It has to be installed for every variant of Python being used.

drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: only read a certain length of a output

Mon Jul 15, 2019 5:02 pm

hi, thank you

i believe it is installed as it tells me i am on version 68 i believe

User avatar
joan
Posts: 14196
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: only read a certain length of a output

Mon Jul 15, 2019 5:11 pm

Try

apt-cache policy python-pigpio

and

apt-cache policy python3-pigpio

drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: only read a certain length of a output

Mon Jul 15, 2019 5:40 pm

pi@raspberrypi:~ $ apt-cache policy python3-pigpio
python3-pigpio:
Installed: 1.68-3+rpi1
Candidate: 1.68-3+rpi1
Version table:
*** 1.68-3+rpi1 500
500 http://raspbian.raspberrypi.org/raspbian buster/main armhf Packages
100 /var/lib/dpkg/status
pi@raspberrypi:~ $ apt-cache policy python-pigpio
python-pigpio:
Installed: 1.68-3+rpi1
Candidate: 1.68-3+rpi1
Version table:
*** 1.68-3+rpi1 500
500 http://raspbian.raspberrypi.org/raspbian buster/main armhf Packages
100 /var/lib/dpkg/status

hippy
Posts: 5790
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: only read a certain length of a output

Wed Jul 17, 2019 12:38 pm

Code: Select all

   def callback(bits, value):
      print("bits={} value={}".format(bits, value))
Given that reports, "bits=89 value=321749267849309967093760" it would seem that you could grab the card ID into a variable simply by using -

Code: Select all

   def callback(bits, value):
      print("bits={} value={}".format(bits, value))
      cardId = str(value)
You might have to left pad with zero characters to get a string of 24 digits, or you might want that as hexadecimal. I've no idea.

But once that's done you can then slice the string to use whatever parts of it you want.

The first 8 digits would be "... = cardId[:8]", the last 8 digits "... = cardId[-8:]".

You would probably be better off creating a hash of the full card ID of the length required rather than just pulling the first or last digits, just in case you encounter two cards where those are the same.

Though if the digits you use include the checksum digits of the ID that would be some mitigation if not using a hash.

I would also use hexadecimal or sixbit ASCII because that allows more unique card codes to be stored in the same number of characters.

drifterf
Posts: 134
Joined: Sat Dec 08, 2018 9:09 am
Location: Northants, England

Re: only read a certain length of a output

Wed Jul 17, 2019 4:30 pm

thank you for the advise :)

Return to “Python”