Search code examples
pythonhexprotocolspyserialrfid

Convert Hex to (RFID) tag ID


I have purchased simple active RFID Reader which is connected through RS232 Serial Port to my Raspberry Pi 3 using RS232 Shield. I want to read the tag id using the reader. I wrote a piece of code which meant to read the data from serial device. When I run it it is just waiting for something, (no error message). How do I read the tags using this reader with python code or shell script?

import serial                           

ser = serial.Serial ("/dev/ttyAMA0")    
ser.baudrate = 9600                     
data = ser.read(8)                       
print (data)                            

UPDATE

I was able to read the tag using:

ser = serial.Serial("/dev/ttyS0") 

as the ttyAMA0 is Bluetooth in Raspberry Pi 3 (this only works on python 3.4. when I try python 2.7 the string is blank). My problem is that I have one tag and the ID of it is 02160323 but when I run this code the output is: b'\xec, b'\xfe, b\xf6, b\xfc and similar (not sure what those values are). How do I recognise the actual id?

UPDATE 2

I was able to convert the text into HEX and then convert it to numeric values.

import serial

ser = serial.Serial("dev/ttyS0", 9600)
data = ser.read()
hexo = data.encode('hex')
i = ord(data)

print(i)
print(hexo)

The full ID is 8 integers long, and given output has not much to do with the ID on the card. How do I convert it to display 02160323?

UPDATE 3

My baundrate was wrong all this time, it should be 115200. Now I receive value: 4000021603237440 and as clearly visible I have ID within it. Now I have to retrieve the data writing appropriate algorithm. Can someone help me with that?


Solution

  • Solved it by using 7 bytes not 8 and then the hexadecimal value will be immutable each time you run it. If you just want to validate for your ID then ignore first 4 string characters and pick the remaining 8. (Python 2.x)

    import serial
    
    while True:
        ser = serial.Serial('/dev/ttyS0', 115200)
        data = ser.read(7)
        hexo = str(data.encode('hex'))
        tagID = hexo[4:]
        tagID = tagID[:8]
        print(tagID)