[Python] RFID Scanner in Python Returns Incomplete UID and Byte Data

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using a real RFID scanner with Python to read the unique ID (UID) of RFID tags, but the output is incomplete and sometimes appears in raw byte form instead of the full UID.

    Expected Behavior: The scanner should return the full UID in a readable format.

    Actual Behavior: The UID is cut off or incomplete. Sometimes, I receive raw byte data instead of a proper UID.

    My Setup: Connection Type: [RS232 to USB adaptor] Library Used: [pyserial]

    My Code:

    import serial

    def parse_epc(packet):
    if len(packet) < 23:
    return None
    epc_bytes = packet[3:15]
    epc = "".join("{:02X}".format(byte) for byte in epc_bytes)
    return epc

    ser = serial.Serial(
    port='COM3',
    baudrate=9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
    )

    if not ser.is_open:
    ser.open()

    print("Listening for RFID packets on COM4...")

    try:
    while True:
    packet = ser.read(23)
    if packet:
    epc = parse_epc(packet)
    if epc:
    print("Tag EPC:", epc)
    else:
    print("Incomplete data")
    print(packet)
    except KeyboardInterrupt:
    print("Exiting...")
    finally:
    ser.close()

    Continue reading...

Compartilhe esta Página