Raspberry Pi上的列表索引超出范围

2024-09-30 10:28:49 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图让NRF24L01模块在Raspberry Pi 3b+上工作时,出现了“列表索引超出范围”错误。代码是用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)输出[]。你知道吗


Tags: importtruereadgpiotime错误radioprint

热门问题