树莓派4B中调用spidev.SpiDev()初始化NRF24模块报错的解决咨询
Let's break down what's going wrong and how to fix it:
The Root Cause
The error happens because the new nrf24 library has a completely different constructor signature than the old lib_nrf24 you replaced. In your code, you're passing GPIO as the first argument and an uninitialized SpiDev() instance as the second—but the new library expects the first argument to be an initialized SPI device, followed by the CE and CSN pin numbers (or keyword arguments for clarity).
The line radio = NRF24(GPIO, spidev.SpiDev()) is causing the library to treat GPIO as the CE pin (hence the assertion checking if it's between 0-31) and the SpiDev() instance as something else entirely, leading to the type mismatch error.
Step-by-Step Fix
Here's how to adjust your code to work with the modern nrf24 library:
1. Initialize the SPI Device Properly
First, you need to open the SPI bus/device you're using (you confirmed /dev/spidev0.0 and /dev/spidev0.1 exist—pick the one connected to your NRF24 module):
spi = spidev.SpiDev() spi.open(0, 0) # Uses /dev/spidev0.0; change to (0,1) if you're using that device
2. Create the NRF24 Object Correctly
Replace your existing radio initialization lines with this:
# Remove these two lines: # radio = NRF24(GPIO, spidev.SpiDev()) # radio.begin(0, 25) # Replace with this: radio = NRF24(spi, ce_pin=8, csn_pin=25) # CE=GPIO8, CSN=GPIO25 (BCM numbering)
The new library handles pin setup directly in the constructor, so you don't need the begin() method anymore.
3. Verify Method Compatibility
Most of your remaining code should work, but double-check a few things:
- Ensure constants like
NRF24.PA_MIN,NRF24.BR_1MBPSexist in the new library (they usually do, but if not, check the library's docs for equivalent values). - The
printDetails()method might have a different name (likeprint_info()) in some versions—if it throws an error, replace it with the correct method from the library.
Full Modified Code
Here's your code with all the fixes applied:
import RPi.GPIO as GPIO import time import spidev from nrf24 import NRF24 GPIO.setmode(GPIO.BCM) pipes = [[0xE0, 0xE0, 0xF1, 0xF1, 0xE0], [0xF1, 0xF1, 0xF0, 0xF0, 0xE0]] # Initialize SPI device spi = spidev.SpiDev() spi.open(0, 0) # Adjust to (0,1) if using /dev/spidev0.1 # Create radio instance with correct parameters radio = NRF24(spi, ce_pin=8, csn_pin=25) # Configure radio settings radio.setPALevel(NRF24.PA_MIN) radio.setChannel(0x76) radio.setDataRate(NRF24.BR_1MBPS) radio.setPayloadSize(32) radio.setAutoAck(True) radio.enableDynamicPayloads() radio.enableAckPayload() radio.openWritingPipe(pipes[0]) try: radio.printDetails() # Fallback to radio.print_info() if this fails except AttributeError: radio.print_info() # Prepare message sendMessage = list("Hi, there!") while len(sendMessage) < 32: sendMessage.append(0) try: while True: start = time.time() radio.write(sendMessage) print("Sent the message: {}".format(sendMessage)) radio.startListening() while not radio.available(0): time.sleep(1/100) if time.time() - start > 2: print("Timed out.") break radio.stopListening() time.sleep(3) finally: GPIO.cleanup() spi.close()
Additional Notes
- Always clean up GPIO and SPI resources when your script exits (added a
finallyblock for this). - If you still have issues, double-check your wiring: CE and CSN pins must match the numbers you pass to the constructor (GPIO8 and GPIO25 in this example).
- Make sure you installed the correct
nrf24library—runpip show nrf24to confirm it's the modern one (not the deprecatedlib_nrf24).
内容的提问来源于stack exchange,提问作者Elmar

