我试图通过 RS-232串行控制(启用/禁用) UHF RFID阅读器(CT-I809)的蜂鸣器 使用 python 在树莓派中使用“ strong>端口”。我的应用程序可以很好地检测RFID标签,但是当我单击“停止阅读”按钮时,蜂鸣器仍在嗡嗡作响,我感到很烦。
下面是我的RFID阅读器的外观。
我找到了可以控制它的Windows应用程序,但是它在C#中。我尝试使用mono在树莓派(OS Raspbian)中打开它,但由于应用程序使用本机Windows dll SDK而失败。因此,我决定用python编写自己的应用程序,这是我的代码。
import tkinter as tk
import tkinter.ttk as ttk
import threading, serial, binascii
ser = serial.Serial()
ser.port = "COM1"
# ser.port = "/dev/ttyUSB0"
ser.baudrate = 57600
ser.timeout = None
header = "00ee00"
epc = []
count = []
index = 0
class AppBase(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent=parent
self.initialize_user_interface()
def initialize_user_interface(self):
self.parent.title("COMEL Reader")
self.parent.grid_rowconfigure(0, weight=1)
self.parent.grid_columnconfigure(0, weight=1)
title = ttk.Label(self.parent, text="COMEL Reader", font=("Arial 20 bold"))
title.grid(column=0, row=1, sticky=tk.W, padx=10, pady=10)
self.read_btn = tk.Button(self.parent, text="READ", state=tk.NORMAL, height=2, width=10,
command=self.switch)
self.read_btn.grid(row=1, column=1, sticky=tk.E, padx=10)
# Set the treeview
self.tree = ttk.Treeview(self.parent,
columns=('TagID', 'Count'))
self.tree.grid(padx=10, pady=10)
self.tree.heading('#0', text='No.')
self.tree.heading('#1', text='TagID')
self.tree.heading('#2', text='Count')
self.tree.column('#0', width=40, stretch=tk.YES)
self.tree.column('#1', width=200, stretch=tk.YES)
self.tree.column('#2', width=100, stretch=tk.YES, anchor=tk.CENTER)
self.tree.grid(row=4, columnspan=4, sticky='nsew')
vsb = ttk.Scrollbar(orient="vertical", command=self.tree.yview)
vsb.place(x=335, y=90, height=202)
self.tree.configure(yscrollcommand=vsb.set)
self.treeview = self.tree
self.i = 0
self.active = False
self.progressbar = ttk.Progressbar(self.parent, mode='indeterminate')
self.progressbar.grid(column=1, padx=10, pady=10, sticky=tk.E)
def switch(self):
global index
if self.active == True:
self.active = not self.active
self.read_btn["text"] = "READ"
ser.close()
else:
self.treeview.delete(*self.treeview.get_children())
index = 0
self.i = 0
epc.clear()
count.clear()
self.read_btn["text"] = "STOP"
self.active = not self.active
ser.open()
self.start_submit_thread(None)
def read_tag(self):
global index
global count
while self.active == True:
if self.active == False:
break
ser.flushInput()
read_result = ser.read(8)
hex_string = binascii.hexlify(read_result).decode('utf-8')
data = hex_string[hex_string.index(header) + len(header):].upper()
if "B0" not in data:
continue
else:
if index==0:
self.i = self.i + 1
epc.append(data)
count.append(1)
self.treeview.insert('', 'end', text=str(self.i),
values=(epc[index],
str(count[index])))
index = index + 1
else:
if data in epc:
data_index = epc.index(data)
count[data_index] = count[data_index]+1
x = self.treeview.get_children()[data_index]
self.treeview.item(x,
values=(epc[data_index],
str(count[data_index])))
else:
self.i = self.i + 1
epc.append(data)
count.append(1)
self.treeview.insert('', 'end', text=str(self.i),
values=(epc[index],
str(count[index])))
index = index + 1
def start_submit_thread(self,event):
self.submit_thread = threading.Thread(target=self.read_tag)
self.submit_thread.daemon = True
self.progressbar.start()
self.submit_thread.start()
root.after(20, self.check_submit_thread)
def check_submit_thread(self):
if self.submit_thread.is_alive():
root.after(20, self.check_submit_thread)
else:
self.progressbar.stop()
if __name__ == '__main__':
root=tk.Tk()
AppBase(root)
root.mainloop()
我也已经反编译了C#SDK,但对我来说太复杂了,下面是一些代码:
if(lstrcmpA(lpProcName,"Writebaud")==0)
return (FARPROC)UHFReader18_RVA(0x4570);
if(lstrcmpA(lpProcName,"SetPowerDbm")==0)
return (FARPROC)UHFReader18_RVA(0x4830);
if(lstrcmpA(lpProcName,"BuzzerAndLEDControl")==0)
return (FARPROC)UHFReader18_RVA(0x4920);
和来自头文件:
void* __stdcall UHFReader18_RVA(DWORD rvaAddr);
回到我的问题,是否可以通过python串行控制RFID阅读器的蜂鸣器?每个答案将不胜感激。谢谢。