sohotcall

FPM10A Python3 Linux/Windows

Jul 27th, 2018
183
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import serial
  2. import time
  3.  
  4. class FPM10A:
  5.     def __init__(self, port):
  6.         self._ser = serial.Serial(port, baudrate=57600)
  7.         self._addr = b"\xFF\xFF\xFF\xFF"
  8.         self._lengthLimit = 128
  9.         self.DEBUG = False
  10.    
  11.     def _HexByte(x):
  12.         chars = (0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F')
  13.         print(chars[x >> 4], end="")
  14.         print(chars[x & 15], end="")
  15.         print(" ", end="")
  16.    
  17.     def sendPkg(self, pid, data, sleep=0.5):
  18.         time.sleep(sleep)
  19.         if type(pid) == bytes:
  20.             pid = pid[0]
  21.         length = len(data) + 2
  22.         sum = pid + (length >> 8) + (length & 255)
  23.         for d in data:
  24.             sum = sum + d
  25.         pkg = (b"\xEF\x01" + self._addr + bytes([pid, length >> 8,  length & 255])
  26.             + data + bytes([sum >> 8, sum & 255])
  27.             )
  28.         self._ser.write(pkg)
  29.         if self.DEBUG:
  30.             print("ME: ", end="")
  31.             for j in pkg:
  32.                 FPM10A._HexByte(j)
  33.             print("")
  34.    
  35.     def sendData(self, data):
  36.         debug = self.DEBUG
  37.         self.DEBUG = False
  38.         time.sleep(0.01)
  39.         while len(data):
  40.             if len(data) > self._lengthLimit:
  41.                 self.sendPkg(2, data[0:self._lengthLimit], 0)
  42.                 data = data[self._lengthLimit:]
  43.             else:
  44.                 self.sendPkg(8, data)
  45.                 break
  46.         self.DEBUG = debug
  47.         self.getData()
  48.    
  49.     def getPkg(self, sleep=1):
  50.         if sleep:
  51.             time.sleep(sleep)
  52.         mode = 0
  53.         (pid, data) = (0, b"")
  54.         length = 0
  55.         if self.DEBUG:
  56.             print("FPM10A: ", end="")
  57.         breakFree = False
  58.         while not breakFree and self._ser.inWaiting() > 0:
  59.             while not breakFree and self._ser.inWaiting() > 0:
  60.                 c = self._ser.read(1)[0]
  61.                 if self.DEBUG:
  62.                     FPM10A._HexByte(c)
  63.                 if length > 2:
  64.                     data = data + bytes([c])
  65.                     length = length - 1
  66.                 elif length > 0:
  67.                     length = length - 1
  68.                 elif (mode == 7) or (mode == 8):
  69.                     length = (length << 8) | c
  70.                     mode = mode + 1
  71.                 elif (mode == 6):
  72.                     pid = c
  73.                     mode = mode + 1
  74.                 elif ((mode == 0 and c == 0xEF)
  75.                         or (mode == 1 and c == 0x01)
  76.                         or (mode == 2 and c == self._addr[0])
  77.                         or (mode == 3 and c == self._addr[1])
  78.                         or (mode == 4 and c == self._addr[2])
  79.                         or (mode == 5 and c == self._addr[3])
  80.                         ):
  81.                     mode = mode + 1
  82.                 else:
  83.                     mode = 0
  84.                 if mode > 8 and length == 0:
  85.                     breakFree = True
  86.             time.sleep(sleep)
  87.         if self.DEBUG:
  88.             print("")
  89.         return (pid, data)
  90.        
  91.     def getData(self):
  92.         debug = self.DEBUG
  93.         self.DEBUG = False
  94.         tmp = ""
  95.         rdata = b""
  96.         while True:
  97.             (pid, data) = self.getPkg(0.01)
  98.             if pid in(2, 8):
  99.                 rdata = rdata + data
  100.                 if pid == 8:
  101.                     self.DEBUG = debug
  102.                     return rdata
  103.             else:
  104.                 self.DEBUG = debug
  105.                 return b""
  106.    
  107.     def ReadSysPara(self):
  108.         self.sendPkg(1, b"\x0F")
  109.         (pid, data) = self.getPkg()
  110.         if pid == 7 and data[0] == 0:
  111.             conf = data[0]
  112.             result = {
  113.                 'statusRegister': data[1:3],
  114.                 'systemVerifyCode': data[3:5],
  115.                 'fingerprintLibrarySize': (data[5] << 8) | data[6],
  116.                 'securityLevel': (data[7] << 8) | data[8],
  117.                 'addr': data[9:13],
  118.                 'lengthLimit': (data[13] << 8) | data[14],
  119.                 'baudrateControl': (data[15] << 8) | data[16]
  120.                 }
  121.             return ("OK", result)
  122.         else:
  123.             return ("ERR", pid, data)
  124.    
  125.     def _confirm(self, command):
  126.         self.sendPkg(1, command)
  127.         (pid, data) = self.getPkg()
  128.         if pid == 7 and data[0] == 0:
  129.             return ("OK",)
  130.         else:
  131.             return ("ERR", pid, data)
  132.    
  133.     def _ChrBufferPageQty(bufferId, pageId, qty):
  134.         if type(bufferId) == int:
  135.             bufferId = bytes([bufferId])
  136.         if type(pageId) == int:
  137.             pageId = bytes([pageId >> 8, pageId & 255])
  138.         if type(qty) == int:
  139.             qty = bytes([qty >> 8, qty & 255])
  140.         return (bufferId, pageId, qty)
  141.            
  142.     def GenImg(self):
  143.         self.sendPkg(1, b"\x01", 0)
  144.         (pid, data) = self.getPkg(1)
  145.         if pid == 7 and data[0] in b"\x00\x02":
  146.             return ("OK", data[0])
  147.         else:
  148.             return ("ERR", pid, data)
  149.    
  150.     def UpImage(self):
  151.         confirm = self._confirm(b"\x0A")
  152.         if confirm[0] == "OK":
  153.             data = self.getData()
  154.             return ("OK", data)
  155.         else:
  156.             return confirm
  157.    
  158.     def DownImage(self, data):
  159.         confirm = self._confirm(b"\x0B")
  160.         if confirm[0] == "OK":
  161.             data = self.sendData(data)
  162.             return ("OK",)
  163.         else:
  164.             return confirm
  165.    
  166.     def Img2Tz(self, bufferId):
  167.         (bufferId, _, _) = FPM10A._ChrBufferPageQty(bufferId, False, False)
  168.         return self._confirm(b"\x02" + bufferId)
  169.    
  170.     def RegModel(self):
  171.         return self._confirm(b"\x05")
  172.    
  173.     def Store(self, bufferId, pageId):
  174.         (bufferId, pageId, _) = FPM10A._ChrBufferPageQty(bufferId, pageId, False)
  175.         return self._confirm(b"\x06" + bufferId + pageId)
  176.    
  177.     def LoadChar(self, bufferId, pageId):
  178.         (bufferId, pageId, _) = FPM10A._ChrBufferPageQty(bufferId, pageId, False)
  179.         return self._confirm(b"\x07" + bufferId + pageId)
  180.    
  181.     def UpChar(self, bufferId):
  182.         (bufferId, _, _) = FPM10A._ChrBufferPageQty(bufferId, False, False)
  183.         confirm = self._confirm(b"\x08" + bufferId)
  184.         if confirm[0] == "OK":
  185.             data = self.getData()
  186.             return ("OK", data)
  187.         else:
  188.             return confirm
  189.    
  190.     def DownChar(self, bufferId, data):
  191.         (bufferId, _, _) = FPM10A._ChrBufferPageQty(bufferId, False, False)
  192.         confirm = self._confirm(b"\x09" + bufferId)
  193.         if confirm[0] == "OK":
  194.             data = self.sendData(data)
  195.             return ("OK",)
  196.         else:
  197.             return confirm
  198.    
  199.     def DeletChar(self, pageId, qty=1):
  200.         (_, pageId, qty) = FPM10A._ChrBufferPageQty(False, pageId, qty)
  201.         return self._confirm(b"\x0C" + pageId + qty)
  202.    
  203.     def Empty(self):
  204.         return self._confirm(b"\x0D")
  205.    
  206.     def Match(self):
  207.         self.sendPkg(1, b"\x03")
  208.         (pid, data) = self.getPkg()
  209.         if pid == 7 and data[0] in b"\x00\x08":
  210.             match = {0:True, 8:False}
  211.             score = data[1] << 8 | data[2]
  212.             return ("OK", match[data[0]], score)
  213.         else:
  214.             return ("ERR", pid, data)
  215.    
  216.     def Search(self, bufferId, pageId, qty):
  217.         (bufferId, pageId, qty) = FPM10A._ChrBufferPageQty(bufferId, pageId, qty)
  218.         self.sendPkg(1, b"\x04" + bufferId + pageId + qty)
  219.         (pid, data) = self.getPkg()
  220.         if pid == 7 and data[0] in b"\x00\x09":
  221.             found = {0:True, 9:False}
  222.             pageId = data[1] << 8 | data[2]
  223.             score = data[3] << 8 | data[4]
  224.             return ("OK", found[data[0]], pageId, score)
  225.         else:
  226.             return ("ERR", pid, data)
  227.    
  228.     def promptScan(self, message, timeout=5):
  229.         last = 0
  230.         timeout = time.time() + timeout
  231.         while True:
  232.             if time.time() > timeout:
  233.                 return ("ERR","TIMEOUT")
  234.             elif int(timeout - time.time()) + 1 != last:
  235.                 last = int(timeout - time.time()) + 1
  236.                 print(message % (last + 1))
  237.             results = self.GenImg()
  238.             if results[0] == "OK" and results[1] == 0:
  239.                 return ("OK",)
  240.             elif results[0] == "ERR":
  241.                 return results
  242.             else:
  243.                 time.sleep(0.1)
  244.  
  245. if __name__ == "__main__":
  246.     def main():
  247.         fpm10a = FPM10A("/dev/ttyUSB0")
  248.         fpm10a.DEBUG = True
  249.        
  250.         print("> Read System Parameters")
  251.         results = fpm10a.ReadSysPara()
  252.         if results[0] != "OK": return results
  253.         print(results[1])
  254.         fingerprintLibrarySize = results[1]['fingerprintLibrarySize']
  255.        
  256.         print("> Scan your finger!")
  257.         results = fpm10a.promptScan("Scan your finger (%d)")
  258.         if results[0] != "OK": return results
  259.        
  260.         #print("> Uploading your finger image data..")
  261.         #results = fpm10a.UpImage()
  262.         #if results[0] != "OK": return results
  263.         #data = results[1]
  264.        
  265.         #print("> Downloading your finger image data..")
  266.         #results = fpm10a.DownImage(data=data)
  267.         #if results[0] != "OK": return results
  268.        
  269.         print("> Getting feature data into buffer 1..")
  270.         results = fpm10a.Img2Tz(1)
  271.         if results[0] != "OK": return results
  272.        
  273.         print("> Scan your finger again!")
  274.         results = fpm10a.promptScan("Scan your finger again (%d)")
  275.         if results[0] != "OK": return results
  276.        
  277.         print("> Getting feature data into buffer 2..")
  278.         results = fpm10a.Img2Tz(2)
  279.         if results[0] != "OK": return results
  280.        
  281.         print("> Generating model..")
  282.         results = fpm10a.RegModel()
  283.         if results[0] != "OK": return results
  284.        
  285.         #print("> Uploading model data from buffer 2..")
  286.         #results = fpm10a.UpChar(2)
  287.         #if results[0] != "OK": return results
  288.         #data = results[1]
  289.        
  290.         #print("> Downloading model data into buffer 2..")
  291.         #results = fpm10a.DownChar(2, data=data)
  292.         #if results[0] != "OK": return results
  293.        
  294.         print("> Scan your finger again!")
  295.         results = fpm10a.promptScan("Scan your finger again (%d)")
  296.         if results[0] != "OK": return results
  297.        
  298.         print("> Getting feature data into buffer 1..")
  299.         results = fpm10a.Img2Tz(1)
  300.         if results[0] != "OK": return results
  301.        
  302.         print("> Matching feature (in buffer 1) with model (in buffer 2)..")
  303.         results = fpm10a.Match()
  304.         if results[0] != "OK": return results
  305.         print("M A T C H:%d Score:%d" % (results[1], results[2]))
  306.        
  307.         print("> Saving model from buffer 2 into page 0")
  308.         results = fpm10a.Store(2, 0)
  309.         if results[0] != "OK": return results
  310.        
  311.         print("> Load model into buffer 2 from page 0")
  312.         results = fpm10a.LoadChar(2, 0)
  313.         if results[0] != "OK": return results
  314.        
  315.         print("> Search feature in buffer 1 on page 0..")
  316.         results = fpm10a.Search(1, 0, qty=1)
  317.         if results[0] != "OK": return results
  318.         print("F O U N D:%d Page:%d Score:%d" % (results[1], results[2], results[3]))
  319.        
  320.         print("> Scan other finger!")
  321.         results = fpm10a.promptScan("Scan other finger (%d)")
  322.         if results[0] != "OK": return results
  323.        
  324.         print("> Getting feature data into buffer 1..")
  325.         results = fpm10a.Img2Tz(1)
  326.         if results[0] != "OK": return results
  327.        
  328.         print("> Matching feature (in buffer 1) with model (in buffer 2)..")
  329.         results = fpm10a.Match()
  330.         if results[0] != "OK": return results
  331.         print("M A T C H:%d Score:%d" % (results[1], results[2]))
  332.        
  333.         print("> Search feature in buffer 1 on page 0..")
  334.         results = fpm10a.Search(1, 0, qty=1)
  335.         if results[0] != "OK": return results
  336.         print("F O U N D:%d Page:%d Score:%d" % (results[1], results[2], results[3]))
  337.        
  338.         return str(fingerprintLibrarySize)+" NOT ERROR"
  339.     print("ERROR: " + str(main()))
RAW Paste Data