Raspberry Pi 上的列表索引超出范围
List index out of range on Raspberry Pi
我在尝试让我的 NRF24L01 模块在 Raspberry Pi 3b+ 上工作时遇到 'List index out of range' 错误。代码是用 python 编写的,我似乎无法注意到错误。
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
GPIO.setmode(GPIO.BCM)
pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]
radio = NRF24 (GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setPayloadSize (32)
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MIN)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()
radio.openReadingPipe(1, pipes[1])
radio.printDetails()
radio.startListening()
while True :
while not radio.available(0):
time.sleep(1/100)
a = []
b = []
receivedMessage = []
radio.read(receivedMessage, radio.getDynamicPayloadSize())
a = receivedMessage[0]
b = receivedMessage[1]
print( "Temperature:" , a, "Humidity:" , b )
错误出现在行 a = receivedMessage[0]
和 b = receivedMessage[1]
上。
会喜欢一些新的外观和有关如何解决此问题的建议。
回溯:
Traceback (most recent call last):
File "/home/pi/Desktop/NRF24L01/VEIIIK.py", line 34, in <module>
a = receivedMessage[0]
IndexError: list index out of range
更新
使用行 print(radio.read(receivedMessage, radio.getDynamicPayloadSize()))
给出输出:
0
1
1
1
1
1
1
1
1
1
1
而行 print(receivedMessage)
输出 []
.
你为什么不这样做:
if(receivedMessage):
a = receivedMessage[0]
b = receivedMessage[1]
print( "Temperature:" , a, "Humidity:" , b )
这可能会解决问题。
我在尝试让我的 NRF24L01 模块在 Raspberry Pi 3b+ 上工作时遇到 'List index out of range' 错误。代码是用 python 编写的,我似乎无法注意到错误。
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
GPIO.setmode(GPIO.BCM)
pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]
radio = NRF24 (GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setPayloadSize (32)
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MIN)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()
radio.openReadingPipe(1, pipes[1])
radio.printDetails()
radio.startListening()
while True :
while not radio.available(0):
time.sleep(1/100)
a = []
b = []
receivedMessage = []
radio.read(receivedMessage, radio.getDynamicPayloadSize())
a = receivedMessage[0]
b = receivedMessage[1]
print( "Temperature:" , a, "Humidity:" , b )
错误出现在行 a = receivedMessage[0]
和 b = receivedMessage[1]
上。
会喜欢一些新的外观和有关如何解决此问题的建议。
回溯:
Traceback (most recent call last):
File "/home/pi/Desktop/NRF24L01/VEIIIK.py", line 34, in <module>
a = receivedMessage[0]
IndexError: list index out of range
更新
使用行 print(radio.read(receivedMessage, radio.getDynamicPayloadSize()))
给出输出:
0
1
1
1
1
1
1
1
1
1
1
而行 print(receivedMessage)
输出 []
.
你为什么不这样做:
if(receivedMessage):
a = receivedMessage[0]
b = receivedMessage[1]
print( "Temperature:" , a, "Humidity:" , b )
这可能会解决问题。