簡體   English   中英

Python 韋根 34 位 - 忽略最后 2 位

[英]Python Wiegand 34bits - ignore last 2 bits

我正在嘗試使用 python 從 QR 閱讀器讀取代碼。使用了此處的 pigpio 示例http://abyz.me.uk/rpi/pigpio/examples.html#Python_wiegand_py

當我掃描 QR 時,它返回bits=34 value=12835750879這太長了,代碼應該只有 10 位。 QR 發送 34 位,安裝它的人說我需要忽略最后 2 位,只取 0-31,問題是我不太了解代碼,我來自 PHP,這無關緊要帶位和 memory。

這是處理讀取的代碼部分,我認為在_cb function 的某處。

有人可以幫助我更好地理解代碼,以便我可以 go 進入正確的方向嗎?

   def __init__(self, pi, gpio_0, gpio_1, callback, bit_timeout=5):

      """
      Instantiate with the pi, gpio for 0 (green wire), the gpio for 1
      (white wire), the callback function, and the bit timeout in
      milliseconds which indicates the end of a code.

      The callback is passed the code length in bits and the value.
      """

      self.pi = pi
      self.gpio_0 = gpio_0
      self.gpio_1 = gpio_1

      self.callback = callback

      self.bit_timeout = bit_timeout

      self.in_code = False

      self.pi.set_mode(gpio_0, pigpio.INPUT)
      self.pi.set_mode(gpio_1, pigpio.INPUT)

      self.pi.set_pull_up_down(gpio_0, pigpio.PUD_UP)
      self.pi.set_pull_up_down(gpio_1, pigpio.PUD_UP)

      self.cb_0 = self.pi.callback(gpio_0, pigpio.FALLING_EDGE, self._cb)
      self.cb_1 = self.pi.callback(gpio_1, pigpio.FALLING_EDGE, self._cb)

   def _cb(self, gpio, level, tick):

      """
      Accumulate bits until both gpios 0 and 1 timeout.
      """

      if level < pigpio.TIMEOUT:

         if self.in_code == False:
            self.bits = 1
            self.num = 0

            self.in_code = True
            self.code_timeout = 0
            self.pi.set_watchdog(self.gpio_0, self.bit_timeout)
            self.pi.set_watchdog(self.gpio_1, self.bit_timeout)
         else:
            self.bits += 1
            self.num = self.num << 1

         if gpio == self.gpio_0:
            self.code_timeout = self.code_timeout & 2 # clear gpio 0 timeout
         else:
            self.code_timeout = self.code_timeout & 1 # clear gpio 1 timeout
            self.num = self.num | 1

      else:

         if self.in_code:

            if gpio == self.gpio_0:
               self.code_timeout = self.code_timeout | 1 # timeout gpio 0
            else:
               self.code_timeout = self.code_timeout | 2 # timeout gpio 1

            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)

要刪除最后兩位,您可以使用bin()將 integer self.num轉換為二進制字符串,去掉兩個字符,然后轉換回 integer。

這是一個例子:

num = 2**34 - 1 # 17179869183
num_b = bin(num) # '0b1111111111111111111111111111111111'
num = int(num_b[:-2], base=2) # 4294967295

所以你想在代碼中的某個地方使用int(bin(self.num)[:-2], 2)來檢索完整的二維碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM