代码之家  ›  专栏  ›  技术社区  ›  user7135972

无法打开设备:打开的文件太多错误

  •  1
  • user7135972  · 技术社区  · 7 年前

    嗨,我有可怕的代码在树莓皮3。我正在尝试阅读4 RC522模块,我能做到。但几分钟后,我得到了“无法打开设备:打开的文件太多”的错误,我的while循环终止。

    注意:我有4个SPI地址,我用它们创建实例。

    #!/usr/bin/env python
    # -*- coding: utf8 -*-
    import time
    import sys
    import os
    import RPi.GPIO as GPIO
    import MFRC522
    import signal
    from time import gmtime, strftime
    from time import sleep
    import requests
    #import xml.etree.ElementTree as ET
    #import xmltodict, json
    from lxml import objectify
    from bs4 import BeautifulSoup
    continue_reading = True
    
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(7, GPIO.OUT)
    GPIO.setup(37, GPIO.OUT)
    GPIO.setup(13, GPIO.OUT)
    GPIO.setup(15, GPIO.OUT)
    
    # Capture SIGINT for cleanup when the script is aborted
    def end_read(signal,frame):
        global continue_reading
        print "Ctrl+C captured, ending read."
        continue_reading = False
        GPIO.cleanup()
    
    kullanici = "xxxx"
    sifre = "xxxx"
    birim = "xxxx"
    ogrKEY = " "
    
    
    def end_read(signal,frame):
        global continue_reading
        print "Ctrl+C captured, ending read."
        continue_reading = False
        GPIO.cleanup()
    
    
    # Hook the SIGINT
    signal.signal(signal.SIGINT, end_read)
    
    # Create an object of the class MFRC522
    #
    adresler = ["/dev/spidev0.0", "/dev/spidev0.1", "/dev/spidev1.0","/dev/spidev1.1"]
    
    # Welcome message
    print "Welcome to the MFRC522 data read example"
    print "Press Ctrl-C to stop."
    j = 0
    # This loop keeps checking for chips. If one is near it will get the UID and authenticate
    while continue_reading:
        MIFAREReader = None
        pin = 0
        if j == 100:
           j = 0
        i = j % 4
    
        if i == 0:
        MIFAREReader =  MFRC522.MFRC522(adresler[0], 16)
            pin = 15
        elif i == 1:
        MIFAREReader =  MFRC522.MFRC522(adresler[1], 18)
            pin = 13
        elif i == 2:
            MIFAREReader = MFRC522.MFRC522(adresler[2], 33)
            pin = 7
        else:
            MIFAREReader = MFRC522.MFRC522(adresler[3], 31)
            pin = 37
    
    
        # Scan for cards 
        (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
    
        # If a card is found
        if status == MIFAREReader.MI_OK:
           print "Card detected"
    
           # Get the UID of the card
           (status,uid) = MIFAREReader.MFRC522_Anticoll()
    
           # If we have the UID, continue
           if status == MIFAREReader.MI_OK:
                  # print "su okuyucudan okundu" % i
               # Print UID
               print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
    
               # This is the default key for authentication
               key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
               # Select the scanned tag
               MIFAREReader.MFRC522_SelectTag(uid)
    
               # Authenticate
               status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 10, key, uid)
    
               # Check if authenticated
               if status == MIFAREReader.MI_OK:
                   MIFAREReader.MFRC522_Read(10)
               ogrKEY = MIFAREReader.returnOGR()
                       sonuc =  parser(ogrKEY)
                       if sonuc == True:
                openTurnsTile(pin)
                       else:
                sonuc = personelKontrol(ogrKey)
                            if sonuc == True:
                    openTurnsTile(pin)
                   MIFAREReader.MFRC522_StopCrypto1()
               else:
                   print "Authentication error"
        j = j + 1
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Milind Deore    7 年前

    创建MFRC522对象时,它实际上调用 spi.openSPI() 但从不关闭它。这个python库适用于单个实例,但现在您正在处理打开FD的多个实例,还需要使用 spi.closeSPI() ,否则会出现错误“打开的FD太多”。

    检查有多少 open FDs 这是你的PID。