adafruit / Adafruit_CircuitPython_Fingerprint

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

Getting incorrect packet data when uploading image #29

Closed xavirp closed 1 year ago

xavirp commented 3 years ago

Hi, I've tested the PR and I'm receiving this error when downloading template. The last debug lines are: Downloading template... DEBUG ==> _send_packet length: 13 DEBUG ==> _send_packet data: ['ef', '01', 'ff', 'ff', 'ff', 'ff', '01', '00', '04', '08', '01', '00', '0e'] DEBUG ==> _get_packet received data: ['ef', '01', 'ff', 'ff', 'ff', 'ff', '07', '00', '03', '00', '00', '0a'] DEBUG ==> _get_packet reply: ['00'] DEBUG ==> _get_data received data: ['ef', '01', 'ff', 'ff', 'ff', 'ff', '02', '00', '82'] DEBUG ==> _get_data received start pos: 0xef01 DEBUG ==> _get_data received address: [255, 255, 255, 255] DEBUG ==> _get_data received packet_type: 0x2 DEBUG ==> _get_data received length: 0x82 DEBUG ==> _get_data received checksum: (20956,) DEBUG ==> _get_data received data: ['36', 'ab', '14', '65', '0f', 'a7', '05', '1f', '2e'] DEBUG ==> _get_data received start pos: 0x36ab Traceback (most recent call last): File "adafruit_template_file_compare.py", line 196, in enroll_save_to_file() File "adafruit_template_file_compare.py", line 147, in enroll_save_to_file data = finger.get_fpdata("char", 1) File "/home/pi/huellas/adafruit.py", line 222, in get_fpdata res = self._get_data(9) File "/home/pi/huellas/adafruit.py", line 405, in _get_data reply += self._get_data(9) File "/home/pi/huellas/adafruit.py", line 382, in _get_data raise RuntimeError("Incorrect packet data") RuntimeError: Incorrect packet data

The data received length is 130 (I showed it in hex). For some reason there's no next complete parcel, don't know why. Any clue? Thx

xavirp commented 3 years ago

Sorry, my (your) _get_data function is:

def _get_data(self, expected):
        """Gets packet from serial and checks structure for _DATAPACKET
        and _ENDDATAPACKET.  Alternate method for getting data such
        as fingerprint image, etc.  Returns the data payload."""
        res = self._uart.read(expected)
        self._print_debug("_get_data received data:", res, data_type="hex")
        if (not res) or (len(res) != expected):
            raise RuntimeError("Failed to read data from sensor")

        # first two bytes are start code
        start = struct.unpack(">H", res[0:2])[0]
        self._print_debug("_get_data received start pos:", str(hex(start)))
        if start != _STARTCODE:
            raise RuntimeError("Incorrect packet data")
        # next 4 bytes are address
        addr = list(i for i in res[2:6])
        self._print_debug("_get_data received address:", addr)
        if addr != self.address:
            raise RuntimeError("Incorrect address")

        packet_type, length = struct.unpack(">BH", res[6:9])
        self._print_debug("_get_data received packet_type:", str(hex(packet_type)))
        self._print_debug("_get_data received length:", str(hex(length)))

        # todo: check checksum

        if packet_type != _DATAPACKET:
            if packet_type != _ENDDATAPACKET:
                raise RuntimeError("Incorrect packet data")

        if packet_type == _DATAPACKET:
            res = self._uart.read(length - 2)
            # todo: we should really inspect the headers and checksum
            **reply = list(i for i in res[0:length])**
            received_checksum = struct.unpack(">H", self._uart.read(2))
            self._print_debug("_get_data received checksum:", received_checksum)
            reply += self._get_data(9)

        elif packet_type == _ENDDATAPACKET:
            res = self._uart.read(length - 2)
            # todo: we should really inspect the headers and checksum
            reply = list(i for i in res[0:length])
            received_checksum = struct.unpack(">H", self._uart.read(2))
            self._print_debug("_get_data received checksum:", received_checksum)

        self._print_debug("_get_data reply length:", len(reply))
        self._print_debug("_get_data reply:", reply, data_type="hex")
        return reply

Please, look at the line between XXXXX Your putting the content bytes into a list, but shouldn't it be res[0:length-2] ?

admiralmaggie commented 3 years ago

Hello @xavirp, Which sensor are you testing with?

Cheers, AR

xavirp commented 3 years ago

Hi Ar, It's a R503

admiralmaggie commented 3 years ago

@xavirp I'm unable to replicate this on my end. One option is to download Eltima's serial port monitor and view the raw packets between your PC and the sensor. It might provide some clue as to why you are not getting complete packets.

jaiguptanick commented 3 years ago

Is this issue resolved? I am also getting similar error, I have same sensor using with ItsyBitsy M0 express

tekktrik commented 1 year ago

@ajiekurniawansaputra did #47 resolve this for you? I'm wondering if this can be closed now.

ajiekurniawansaputra commented 1 year ago

yess