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

时间:2017-10-24 07:54:21

标签: linux python-3.x raspberry-pi spi

嗨,我在Raspberry Pi 3中有可怕的代码。我正在尝试阅读4 RC522模块,我可以做到。但几分钟后,我得到了#34;无法打开设备:太多打开的文件"错误,我的while循环终止。

我没有分享所有功能。我想重要的部分是while循环。我在每个循环中创建实例。我想这是个大错误。我不能加延误。我尝试为我的对象分配null但我仍然有同样的错误。我的代码在下面

注意:我有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 个答案:

答案 0 :(得分:0)

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

检查PID的open FDs数量。