adafruit / Adafruit_CircuitPython_Fingerprint

CircuitPython library for talking to UART-based Fingerprint sensors
MIT License
54 stars 52 forks source link

struct.error: unpack requires a buffer of 2 bytes #41

Closed decentralMind closed 2 years ago

decentralMind commented 2 years ago

Hi, I am using r307 fingerprint sensor. I'm trying to compare fingerprint template by uploading it to the fingerprint sensor flash storage.

I'm trying to run this example code.

Here is the piece of code from the example which I failed to run. I've already saved the fingerprint template('template0.dat') file before executing this code.


# pylint: disable=too-many-branches
def fingerprint_check_file():
    """Compares a new fingerprint template to an existing template stored in a file
    This is useful when templates are stored centrally (i.e. in a database)"""
    print("Waiting for finger print...")
    set_led_local(color=3, mode=1)
    while finger.get_image() != adafruit_fingerprint.OK:
        pass
    print("Templating...")
    if finger.image_2_tz(1) != adafruit_fingerprint.OK:
        return False

    print("Loading file template...", end="", flush=True)
    with open("template0.dat", "rb") as file:
        data = file.read()
    finger.send_fpdata(list(data), "char", 2)

    i = finger.compare_templates()
    if i == adafruit_fingerprint.OK:
        set_led_local(color=2, speed=150, mode=6)
        print("Fingerprint match template in file.")
        return True
    if i == adafruit_fingerprint.NOMATCH:
        set_led_local(color=1, mode=2, speed=20, cycles=10)
        print("Templates do not match!")
    else:
        print("Other error!")
    return False

I got following error:

_Waiting for finger print... Templating... Loading file template...Traceback (most recent call last): File "/home/mockupsystem/Adafruit_CircuitPython_Fingerprint/examples/fingerprint_template_file_compare.py", line 203, in fingerprint_check_file() File "/home/mockupsystem/Adafruit_CircuitPython_Fingerprint/examples/fingerprint_template_file_compare.py", line 73, in fingerprint_check_file i = finger.compare_templates() File "/home/mockupsystem/python-env/some/lib/python3.10/site-packages/adafruit_fingerprint.py", line 323, in comparetemplates self.confidence = struct.unpack(">H", bytes(r[1:3])) struct.error: unpack requires a buffer of 2 bytes

tekktrik commented 2 years ago

Adafruit_Fingerprint has the ability to print out debug text if you set it's _debug flag:

...

finger = adafruit_fingerprint.Adafruit_Fingerprint(uart)
finger._debug = True

...

What does the console read when that is done?

ajiekurniawansaputra commented 2 years ago

Hi guys, I have solved the problem, it's in the _send_data function, the checksum should concatenate ENDDATAPACKET on the last send. I have request pull on the main branch, I hope they accept my request.

tekktrik commented 2 years ago

Nice! I'm working on getting my Pi set up to test this in the coming days, hopefully someone can get to it faster!