我在做什么? 我有使用PyQt构建的GUI界面,它通过验证指纹来授予用户访问权限。
我正在使用指纹传感器R305,用于我的模块。
我的问题? 我在GIT上提供了我的代码,它将数据保存在特定的ID中。 但它无法搜索指定的指纹。
注册指纹 *
ser = serial.Serial(port='COM4', baudrate=57600)
pack = [0xef01, 0xffffffff, 0x1]
def printx(l):
for i in l:
print (i)
print ('')
def readPacket():
time.sleep(1)
w = ser.inWaiting()
ret = []
if w >= 9:
s = ser.read(9) #partial read to get length
ret.extend(struct.unpack('!HIBH', s))
ln = ret[-1]
time.sleep(1)
w = ser.inWaiting()
if w >= ln:
s = ser.read(ln)
form = '!' + 'B' * (ln - 2) + 'H'
ret.extend(struct.unpack(form, s))
return ret
def writePacket(data):
pack2 = pack + [(len(data) + 2)]
a = sum(pack2[-2:] + data)
pack_str = '!HIBH' + 'B' * len(data) + 'H'
l = pack2 + data + [a]
s = struct.pack(pack_str, *l)
ser.write(s)
def verifyFinger():
data = [0x13, 0x0, 0, 0, 0]
writePacket(data)
s = readPacket()
return s[4]
def genImg():
data = [0x1]
writePacket(data)
s = readPacket()
return s[4]
def img2Tz(buf):
data = [0x2, buf]
writePacket(data)
s = readPacket()
return s[4]
def regModel():
data = [0x5]
writePacket(data)
s = readPacket()
return s[4]
def store(id):
data = [0x6, 0x1, 0x0, id]
writePacket(data)
s = readPacket()
return s[4]
if verifyFinger():
print ('Verification Error')
sys.exit(0)
print ('Put finger')
sys.stdout.flush()
time.sleep(1)
while genImg():
time.sleep(0.1)
print ('.')
sys.stdout.flush()
print ('')
sys.stdout.flush()
if img2Tz(1):
print ('Conversion Error')
sys.exit(0)
print ('Put finger again')
sys.stdout.flush()
time.sleep(1)
while genImg():
time.sleep(0.1)
print ('.')
sys.stdout.flush()
print ('')
sys.stdout.flush()
if img2Tz(2):
print ('Conversion Error')
sys.exit(0)
if regModel():
print ('Template Error')
sys.exit(0)
id = 1
if store(id):
print ('Store Error')
sys.exit(0)
print ("Enrolled successfully at id %d"%id)
*
搜索指纹 *
ser = serial.Serial('COM4',57600)
pack = [0xef01, 0xffffffff, 0x1]
def printx():
for i in l:
print(i)
print( '')
def readPacket():
time.sleep(1)
w = ser.inWaiting()
ret = []
if w >= 9:
s = ser.read(9) #partial read to get length
ret.extend(struct.unpack('!HIBH', s))
ln = ret[-1]
time.sleep(1)
w = ser.inWaiting()
if w >= ln:
s = ser.read(ln)
form = '!' + 'B' * (ln - 2) + 'H'
ret.extend(struct.unpack(form, s))
return ret
def writePacket(data):
pack2 = pack + [(len(data) + 2)]
a = sum(pack2[-2:] + data)
pack_str = '!HIBH' + 'B' * len(data) + 'H'
l = pack2 + data + [a]
s = struct.pack(pack_str, *l)
ser.write(s)
def verifyFinger():
data = [0x13, 0x0, 0, 0, 0]
writePacket(data)
s = readPacket()
return s[4]
def genImg():
data = [0x1]
writePacket(data)
s = readPacket()
return s[4]
def img2Tz(buf):
data = [0x2, buf]
writePacket(data)
s = readPacket()
return s[4]
def regModel():
data = [0x5]
writePacket(data)
s = readPacket()
return s[4]
def search():
data = [0x4, 0x1, 0x0, 0x0, 0x0, 0x5]
writePacket(data)
s = readPacket()
return s[4:-1]
def mainfuncn():
if verifyFinger():
print( 'Verification Error')
sys.exit(-1)
print('Put finger')
sys.stdout.flush()
time.sleep(1)
for _ in range(5):
g = genImg()
if g == 0:
break
#time.sleep(1)
print( '.')
sys.stdout.flush()
print( '')
sys.stdout.flush()
if g != 0:
sys.exit(-1)
if img2Tz(1):
print('Conversion Error')
sys.exit(-1)
r = search()
print('Search result', r)
if r[0] == 0 and r[2] in [0,1]:
print('Successful')
sys.exit(0)
else:
print('Unsuccessful')
sys.exit(1)
mainfuncn()
*
GUI代码: *
from PyQt4 import QtCore, QtGui
import serial, time, datetime, struct
import sys
import random
newarray=[]
distarray=[]
data1=""
m=""
k=0
ser = serial.Serial('COM4',57600)
pack = [0xef01, 0xffffffff, 0x1]
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(400, 300)
self.verticalLayoutWidget = QtGui.QWidget(Dialog)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 381, 281))
self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.pushButton = QtGui.QPushButton(self.verticalLayoutWidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.verticalLayout.addWidget(self.pushButton)
self.pushButton.clicked.connect(self.finger_call_main)
self.pushButton1 = QtGui.QPushButton(self.verticalLayoutWidget)
self.pushButton1.setObjectName(_fromUtf8("pushButton1"))
self.verticalLayout.addWidget(self.pushButton1)
self.pushButton1.clicked.connect(self.mainfuncn)
self.lineEdit = QtGui.QLineEdit(self.verticalLayoutWidget)
self.lineEdit.setText(_fromUtf8(""))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.verticalLayout.addWidget(self.lineEdit)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def printx(self,l):
for i in l:
print (i)
print ('')
def readpacket(self):
time.sleep(1)
w = ser.inWaiting()
ret = []
if w >= 9:
s = ser.read(9) #partial read to get length
ret.extend(struct.unpack('!HIBH', s))
ln = ret[-1]
time.sleep(1)
w = ser.inWaiting()
if w >= ln:
s = ser.read(ln)
form = '!' + 'B' * (ln - 2) + 'H'
ret.extend(struct.unpack(form, s))
return ret
def writePacket(self,data):
pack2 = pack + [(len(data) + 2)]
a = sum(pack2[-2:] + data)
pack_str = '!HIBH' + 'B' * len(data) + 'H'
l = pack2 + data + [a]
s = struct.pack(pack_str, *l)
#print("Writepacket S value: " +s)
ser.write(s)
def verifyFinger(self):
data = [0x13, 0x0, 0, 0, 0]
self.writePacket(data)
s = self.readpacket()
return s[4]
def genImg(self):
data = [0x1]
self.writePacket(data)
s = self.readpacket()
return s[4]
def img2Tz(self,buf):
data = [0x2, buf]
self.writePacket(data)
s = self.readpacket()
return s[4]
def regModel(self):
data = [0x5]
self.writePacket(data)
s = self.readpacket()
return s[4]
def store(self,id):
data = [0x6, 0x1, 0x0, id]
self.writePacket(data)
s = self.readpacket()
return s[4]
def new(self):
m=random.randrange(1,250)
if m in distarray:
print("hii")
# newarray.append(m)
self.new()
else:
print("else")
distarray.append(m)
id1=m
print(id1)
return id1
#for x in distarray:
#print(x)
def search(self):
data = [0x4, 0x1, 0x0, 0x0, 0x0, 0x5]
self.writePacket(data)
s = self.readpacket()
return s[4:-1]
def finger_call_main(self):
if self.verifyFinger():
print ('Verification Error1')
sys.exit(0)
print ('Put finger')
sys.stdout.flush()
time.sleep(1)
while self.genImg():
time.sleep(0.1)
print ('.')
sys.stdout.flush()
print ('')
sys.stdout.flush()
if self.img2Tz(1):
print ('Conversion Error')
sys.exit(0)
print ('Put finger again')
sys.stdout.flush()
time.sleep(1)
while self.genImg():
time.sleep(0.1)
print ('.')
sys.stdout.flush()
print ('')
sys.stdout.flush()
if self.img2Tz(2):
print ('Conversion Error')
sys.exit(0)
if self.regModel():
print ('Template Error')
sys.exit(0)
id=self.new()
if self.store(id):
print ('Store Error')
sys.exit(0)
self.lineEdit.setText("Enrolled successfully at id %d"%id)
print ("Enrolled successfully at id %d"%id)
def mainfuncn(self):
if self.verifyFinger():
print( 'Verification Error')
sys.exit(-1)
print('Put finger')
sys.stdout.flush()
time.sleep(1)
for _ in range(5):
g = self.genImg()
if g == 0:
break
#time.sleep(1)
print( '.')
sys.stdout.flush()
print( '')
sys.stdout.flush()
if g != 0:
sys.exit(-1)
if self.img2Tz(1):
print('Conversion Error')
sys.exit(-1)
r = self.search()
#print('Search result', r)
if r[0] == 0:
print('Successful')
sys.exit(0)
else:
print('Unsuccessful')
sys.exit(1)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.pushButton.setText(_translate("Dialog", "PushButton", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
*
答案 0 :(得分:1)
在完成代码之后,我在您的GUI代码中使用new()来生成用于保存指纹模板数据的随机ID值。但是在你的搜索代码中,你正在检查数组[0,1]中r [2]的值,它实际上是你当前存储的id列表。这就是为什么您的搜索程序无法找到您的指纹 要在python中生成id的动态值,可以使用动态数组或循环。 将其替换为GUI代码中的new():
lst=[]
def new(self):
print("new")
def main1(self):
for z in range(0,256):
if z in lst:
z=z+1
self.new()
else:
lst.append(z)
a=z
z=z+1
break
return a
通过调用main1()来获取id的值:
id=self.main1()
在搜索代码中进行以下更改:
if r[0]==0 and r[2] in lst: