groupgets / pylepton

Quick and dirty pure python library for interfacing with FLIR lepton
MIT License
209 stars 94 forks source link

[Errno 25] Inappropriate ioctl for device #60

Closed mishave closed 3 years ago

mishave commented 3 years ago

Hey just wondering if anyone has experienced a similar issue with a fix for the following:

running: raspberrypi3 Lepton3.5 breakoutboard2.0

Traceback (most recent call last):
  File "/usr/local/bin/pylepton_capture", line 37, in <module>
    image = capture(flip_v = options.flip_v, device = options.device)
  File "/usr/local/bin/pylepton_capture", line 9, in capture
    with Lepton(device) as l:
  File "/usr/local/lib/python3.7/dist-packages/pylepton/Lepton.py", line 84, in __enter__
    ioctl(self.__handle, SPI_IOC_RD_MODE, struct.pack("=B", Lepton.MODE))
OSError: [Errno 25] Inappropriate ioctl for device

Interestingly, before downloading/installing the pylepton libraries I was able to communicate with the device using a pair back code (https://lepton.flir.com/forums/topic/raw-spi-read-with-lepton-3-5-and-raspberry-pi-3/):

import spidev
import time

spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 2200000
spi.mode = 0b01 # mode = 3

segmentation=[]
discard = 0
valid = 0
counter = 0
counter2 = 0
for i in range(1000):
#while True:
    data = spi.readbytes(164)
    print("Packet:", data)

    header = data[:4]
    h1 = format(header[0], '08b')
    h2 = format(header[1], '08b')
    h3 = format(header[2], '08b')
    h4 = format(header[3], '08b')

    header_bits = h1 + h2 + h3 + h4
    id_field = h1 + h2
    crc = h3 + h4

    first = id_field[0]
    ttt = id_field[1:4]
    packet_number = id_field[4:16]
    print("first: {}, TTT: {}, packet number: {}, CRC: {}". format(first, ttt, packet_number, crc))

    if packet_number == '000000010100':
        segmentation.append(ttt)

    if first != '0':
        print("Priemrio Bit diferente de 0. Repeticoes: {}", counter)
        counter2 = counter2 + 1

    elif packet_number[0:4] == '1111':
        print("------------- Discard Package ------------------\n")
        discard = discard + 1

    else:
        print("------------- Valid Package --------------------\n")
        valid = valid + 1

    counter = counter + 1
    #break        
print("Valid: {}, Errors: {}".format(valid, discard))    
print("Valid percentage: {:.2f}%".format(valid/(discard+valid+counter2) * 100))
print(counter2)
print("Segmentation: ", segmentation)

Since running setup I am now getting the ioctl error on both codes.

mishave commented 3 years ago

Just a quick update, I reinstalled the OS to start from scratch. this time instead of using the master branch I used the lepton-dev3, for those playing at home: git clone -b lepton3-dev https://github.com/groupgets/pylepton.git

to save your self time before running sudo python3 setup.py install you'll most likely need to fix: /pylepton/Lepton3.py

line 8 and 11 to include a '.' from .ioctl_numbers and from .Lepton and where it has print, include the brackets (around the text) - sorry can't find the link to this tip from a privies issue someone had

then you'll need to increase the buffer size as per: https://github.com/groupgets/pylepton/issues/52#issuecomment-770834737

run sudo python3 setup.py install

then sudo pylepton_capture output.png seems to work.... the resulting image is no good/not usable. but the above problem is solved.