I am using BLavery’s lib_nrf24 library with an nRF24L01 module on my pi running Octopi (it’s pretty much rasbpian for the purposes here, AFAIK – here is the result of the os-release command).
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)" NAME="Raspbian GNU/Linux" VERSION_ID="10" VERSION="10 (buster)" VERSION_CODENAME=buster ID=raspbian ID_LIKE=debian HOME_URL="http://www.raspbian.org/" SUPPORT_URL="http://www.raspbian.org/RaspbianForums" BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
I am able to setup the radio according to this tutorial, but the code that waits for the radio to receive data;
while not radio.available(0): time.sleep(1/100)
throws an OSError (errno 9) Bad file descriptor. Here is the full error traceback:
Traceback (most recent call last): File "main.py", line 72, in <module> main() File "main.py", line 18, in main while not radio.available(0): File "/home/pi/smarthome/lib_nrf24.py", line 506, in available status = self.get_status() File "/home/pi/smarthome/lib_nrf24.py", line 293, in get_status return self.spidev.xfer2([NRF24.NOP]) OSError: [Errno 9] Bad file descriptor
I found this stackoverflow post about what OSError is and means, but I’m not sure how this would help me figure out what is going on in the lib_nrf24 library, and how to make it work so the radio behaves normally.
The post says that this error can be thrown if a file is opened, and closed elsewhere so the current environment thinks it is still open, and throws this error when it tries to close the file. The implicating code in the library is just a return statement;
def get_status(self): return self.spidev.xfer2([NRF24.NOP])
And I’m not learning anything more from looking at the available method that calls the get_status
def available(self, pipe_num=None): if not pipe_num: pipe_num =  status = self.get_status() result = False ...
I’ve searched for any posts that mention both the lib_nrf24 library and any of ‘OSError’ ‘errno 9’ or ‘Bad file descriptor’ and come up with zero hits for the intersection of these two issues.
I realize on the lib_nrf24 github page it says the library is out of support as of May 2018. Am I out of luck? I couldn’t find a more recent python library for these rf modules. The only other approach I have seen is to figure out how to use the c++ boost libraries to use the tmrh20 library… but I’ve looked at that and couldn’t figure it out.
1Please don’t present only links to understand your problem. Most of us will not follow it because to much effort.. Explain what’s the problem by yourself so we can understand it only from the question. Please take the short Tour and visit the Help Center to get an idea how things work here. – Ingo 23 hours ago
The following similar question seems already has an answer: raspberrypi.stackexchange.com/questions/107608/…. – tlfong01 18 hours ago
Your error message seems origins from this statement “return self.spidev.xfer2([NRF24.NOP])”. The “spidev.xfer2” is a standard SPI transfer operation. It appears that the parameter “([NRF24.NOP])” is causing the trouble.The traced back statement is a statement in the “lib_nrf24” which is just python. The 2018 May version is the author’s last beta. He says he verified the basic operation, and so far nobody complains. Perhaps you might like to insert debug print statements around the “get_status()” method, to check out what causes the trouble. In other words, bug is at python level, not C. – tlfong01 17 hours ago
@Ingo I’ve really tried to do that, I explicitly stated the errors I am getting, and I summarized the post I linked to, saying what is relevant from it. I put the code from the library I linked in this post as well. I really think everything is in this post – can you be more specific about what I could’ve done differently? – sideways8 8 hours ago
@tlfong01 Yes, I know the error is from the python code in the library. I am only using python in this project. I tried printing the information that the get_status method needs to return, but then the print statement throws the same error; Bad file descriptor. Where can I find more information about spidev.xfer2 ? The spidev library I have is github.com/Gadgetoid/py-spidev, and I don’t know what I should look for in this library to solve this issue. – sideways8 6 hours ago
Hi @sideways8, Yes, you need to know more about spidev.xfer2. I did have trouble using spidev in another project. Perhaps you may like to skim my problem on the part about spi sorrows: (3) SPI MFRC522 NFC reader notes raspberrypi.stackexchange.com/questions/109773/… (3a) Part 3 – SPI Loopback test, (3b) Part 4 – Where is spidev, (3c) Ref 28, 32, 40 – SPI gitHub, spec, python loopback test program. – tlfong01 4 hours ago
Let me summarize the causes of trouble. (i) There are a couple of SPI variants associated with different libraries. If you pip install or git clone, the spidev for the particular library might be placed together with the library and example programs. So you need to make sure you are using the correct spidev. Part 4 of the above references uses pip install to locate the directories where they library and spidev are hiding. You might like to check if you are using different versions, eg. PyPi spidev v4.3 or others. – tlfong01 4 hours ago
You know it is the following function causing trouble: “def get_status(self): return self.spidev.xfer2([NRF24.NOP])”. So let us look at the function/method “spidev.xfer2″and see why the argument “([NRF24.NOP]” makes trouble. The time has finally come to read the friendly user manual:”spidev 3.4 pip install spidev pypi.org/project/spidev” explaining the following methods: (a) xfer(list of values[, speed_hz, delay_usec, bits_per_word]), (b) xfer2(list of values[, speed_hz, delay_usec, bits_per_word]), (c) xfer3(list of values[, speed_hz, delay_usec, bits_per_word]). – tlfong01 3 hours ago
Now let us zoom in to the trace back record: (1) File “/home/pi/smarthome/lib_nrf24.py”, line 506, in available, status = self.get_status(), (2) File “/home/pi/smarthome/lib_nrf24.py”, line 293, in get_status, return self.***spidev.xfer2([NRF24.NOP])***, OSError: [Errno 9] Bad file descriptor. It appears that the 0th element of the output list causes trouble. Comments welcome. – tlfong01 20 mins ago