Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4816

Python • Re: [SOLVED] Script execution speed, RFID reader system

$
0
0
Hi. This project is of interest to me. Can you share more about what you are doing and perhaps some code examples? Do you have a GIT repo maybe?
Hi, I have good performance reading one tag, it's just a loop until you get good result, next step will be storing multiple tags.
I will use it at work to scan our items, so I don't have repo, here's the most important part of the code:
ps you can ignore time and threading, didn't use it for now, I open port at start and keep it open, if therre will be multiple instances then I would need to close the port after reading.

Code:

import timeimport serialimport threadingREPETITONS = 10DELAY = 0.05class RfidService:    def __init__(self, port="/dev/ttyUSB0"):        self.port = port        self.ser = None          self.thread = None          self.open_port()    # Open the serial port    def open_port(self):        try:            self.ser = serial.Serial(                port=self.port,                baudrate=115200,                parity=serial.PARITY_NONE,                stopbits=serial.STOPBITS_ONE,                bytesize=serial.EIGHTBITS,                timeout=0.05            )        except Exception as e:            print("Error opening serial port:", str(e))    # Send a command and receive the response    def send_command(self, command):        if not self.ser:            print("Serial port is not open.")            return None        try:            while True:                self.ser.write(bytes.fromhex(command))                  #response = self.ser.read(1024)                 response = self.ser.read_until(expected='\x7e')                print(f"Response: {response}")                if response.startswith(b"\xBB\x02\x22"):                    return response                #return response        except Exception as e:            print("Error sending command:", str(e))            return None    # Read a single tag    def read_one_tag(self):        try:            print("Scanning tag...")            response = self.send_command("BB 00 22 00 00 22 7E")            if response.startswith(b"\xBB\x02\x22"):                tag_data = response[10:-4]                  hex_string = tag_data.hex()                if len(hex_string) == 20:                    print("RFID tag data:", hex_string)                    return ["OK", hex_string]            else:                print("No RFID tag detected.")                return ["error", "No RFID tag detected"]        except Exception as e:            print("Error:", str(e))            return ["error", str(e)]        

Statistics: Posted by ivanknezevic — Tue May 21, 2024 7:30 pm



Viewing all articles
Browse latest Browse all 4816

Trending Articles