pcprox icon indicating copy to clipboard operation
pcprox copied to clipboard

HEX Format

Open desmphil opened this issue 4 years ago • 1 comments

I run multiple turnstile with USB and Serial RFID reader of many brands. Serial RFID are easy to handle but the RFIDEA HID reader always forced me to work in the console session.

I was using this in P3 which is not really flexible

if readerUSB == True: #(PYTHON 3 NO ORD REQUIRED - See archives functions.)
  print("******** WAITING FOR CARD ACCESS with USB **************")
  try:
    while True:
      while not done:			## Get the character from the HID
        buffer = fp.read(8)
        for c in buffer:
          if c > 0:	##  40 is carriage return which signifies we are done looking for characters
            if int(c) == 40:
              done = True
              break;	##  If we are shifted then we have to  use the hid2 characters.
            if shift: 	## If it is a '2' then it is the shift key
              if int(c) == 2 :
                shift = True
              else:	# if not a 2, lookup mapping
                rfid_number += hid2[ int(c) ]
                shift = False
            else:		# if not shifted, use the hid characters
              if int(c) == 2 :	# if 2, then it is the shift key
                shift = True
              else:	# If not a 2, lookup mapping
                rfid_number += hid[ int(c) ]

With your code I was able to read USB data off console. However, the HEX data returned is reversed to my requirements

Data Return: Tag data: 35 88 8a 67 50 01 04 e0 Data required: E0040150678A8835

I played with the pcprox.py to reverse the HEX data and i have to create a loop but getting there.

def _format_hex(i: bytes) -> Text:
    return ''.join(reversed(['%02x' % c for c in i]))

Have you had any requirements as such?

desmphil avatar Aug 31 '21 16:08 desmphil

It sounds like the previous code you had there was reading from the PCProx as a keyboard input.

This library doesn't try to use the settings to change how get_tag() works; it just gives you the raw data that the reader sent. I think your reader may be configured with bRevBytes set differently to my reader (I don't have my reader handy to check); but in "SDK mode" there is some processing the reader doesn't do, so that setting and iIDBitCnt are ignored.

_format_hex is an internal function that's designed for display purposes: changing it won't change the outputs that this library gives you.

This library gives back raw bytes from the device; if you want to reverse it and display it with uppercase letters like that, you want something like:

import base64

# removed other setup code (see usbtest.py)
tag = dev.get_tag()
if tag is None:
  print('tag not found')
  return

tag_id, bit_length = tag
# Reverse the tag ID
tag_id = tag_id[::-1]
# Hex-encode the tag ID in upper case
tag_id = base64.b16encode(tag_id)
# (do something with this data)...

micolous avatar Feb 20 '22 02:02 micolous