Skip to content

Commit

Permalink
add async in finger module
Browse files Browse the repository at this point in the history
  • Loading branch information
RainGather committed May 4, 2018
1 parent 4a5dcc2 commit bad237a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
19 changes: 18 additions & 1 deletion docs/sensors/微雪(waveshare)指纹传感器.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,21 @@

匹配指纹::

f.match() # 如果当前指纹和已保存的指纹有相符的,会返回保存指纹的ID(ID必定>0),否则返回0或False
f.match_finger() # 如果当前指纹和已保存的指纹有相符的,会返回保存指纹的ID(ID必定>0),否则返回0或False

由于指纹匹配时,处于阻塞状态,无法进行任何其它操作,故而可以人工异步匹配::

import time

f = FINGER()
f.ready() # 指纹模块进入识别状态
while True:
result = f.match()
if result == 0:
print('Finger Error!') # 匹配错误,指纹不是已录入指纹
f.ready() # 重新进入识别状态进行比对
if result is None:
print('Waiting for Finger!') # 指纹还处于识别状态,还没有手指放上去让识别
if result:
print('Right!')
time.sleep(0.1)
57 changes: 41 additions & 16 deletions ezmpy/WAVESHARE.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def __init__(self, ser, timeout=False):
self.ser = ser
self.add = self.add_finger
self.delete = self.del_all_fingers
self.match = self.match_finger
self.count = self.get_user_count
self.timeout = timeout

Expand All @@ -29,20 +28,20 @@ def add_finger(self, user_id=None):
print('Please Put Your Finger On Sensor...')
result = self.send_cmd(add_finger_cmd)
if not result:
return False
return None
# 由于该模块要求指纹添加3次,故而需要判断3次
if result == b'\xf5\x01\x00\x00\x00\x00\x01\xf5':
add_finger_cmd[1] += 1
print('Please Put Your Finger On Sensor Again...')
result = self.send_cmd(add_finger_cmd)
if not result:
return False
return None
if result == b'\xf5\x02\x00\x00\x00\x00\x02\xf5':
add_finger_cmd[1] += 1
print('Please Put Your Finger On Sensor Again Again...')
result = self.send_cmd(add_finger_cmd)
if not result:
return False
return None
if result == b'\xf5\x03\x00\x00\x00\x00\x03\xf5':
return True
return False
Expand All @@ -55,8 +54,6 @@ def del_all_fingers(self):
cmd = self.gen_chk(cmd)
# 发送命令并获取返回值
result = self.send_cmd(cmd)
if not result:
return False
return result

# 获取已登记的指纹数量
Expand All @@ -68,7 +65,7 @@ def get_user_count(self):
# 发送命令并获取返回值
result = self.send_cmd(cmd)
if not result:
return False
return None
return result[2] * (16 ** 2) + result[3]

# 指纹匹配,如果正确返回user_id(从1开始),否则返回False
Expand All @@ -80,7 +77,7 @@ def match_finger(self):
# 发送命令并获取返回值
result = self.send_cmd(match_cmd)
if not result:
return False
return None
user_id = result[2] * (16 ** 2) + result[3]
return user_id

Expand All @@ -91,34 +88,62 @@ def gen_chk(self, cmd):
for i in range(3, 6):
cmd[6] = cmd[6] ^ cmd[i] # 或运算为^
return cmd

def ready(self):
# 从指纹模块手册中查找到的指纹匹配的命令
match_cmd = [0xF5, 0x0C, 0, 0, 0, 0, 0, 0xF5]
# 生成检测位
match_cmd = self.gen_chk(match_cmd)
# 发送命令并获取返回值
self.send_cmd(match_cmd, timeout=0.001)
return True

def match(self):
result = self.read_cmd()
if result:
user_id = result[2] * (16 ** 2) + result[3]
return user_id
else:
return None

def read_cmd(self):
if self.ser.any():
result = self.ser.read(8)
else:
result = None
return result

# 发送命令
def send_cmd(self, cmd):
def send_cmd(self, cmd, timeout=False):
if not timeout:
timeout = self.timeout
cmd = self.gen_chk(cmd)
# 将命令从16进制转换成字节
msg = bytes(cmd)
print('sending: {}'.format(msg))
# 发送到串口
self.ser.write(msg)
# 如果串口没有返回信息就一直等待
if self.timeout:
if timeout:
run_time = time.time()
try:
while not self.ser.any():
if self.timeout:
if time.time() - run_time > self.timeout:
return False
if timeout:
if time.time() - run_time > timeout:
return None
time.sleep(0.1)
except Exception as e:
while not self.ser.readable():
if self.timeout:
if time.time() - run_time > self.timeout:
return False
if timeout:
if time.time() - run_time > timeout:
return None
time.sleep(0.1)
# time.sleep(2)
# 返回串口返回的8位信息
if self.ser.any():
result = self.ser.read(8)
else:
return False
print(result)
return result

Expand Down

0 comments on commit bad237a

Please sign in to comment.