从几天开始,我尝试通过NRF24模块将Arduino与Raspberry连接。 通过示例,我用C语言(@raspberry)得到了一些有效的代码,但是由于Web应用程序,我想使用python。
问题是我无法解释Arduino发送的msq。 Arduino应该在一个有效载荷中发送8次uint32_t
。在Raspberry中接收后,我得到了8次4 uint8_t
。
Arduino味精:
uint32_t SendMsg[8] = {222, micros(), 10, 20, 30, 40, 50, 60};
Raspberry RCV:
Received: [222, 0, 0, 0, 24, 22, 97, 210, 10, 0, 0, 0, 20, 0, 0, 0, 30, 0, 0, 0, 40, 0, 0, 0, 50, 0, 0, 0, 60, 0, 0, 0]
Raspberry将“ int32”破坏到“ int8”的4倍...我又如何将它们组合?
RCV是一个int列表。
这里是完整的代码:
覆盆子:
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
GPIO.setmode(GPIO.BCM)
pipes = [0xAB, 0xCD, 0xAB, 0xCD, 0x71]
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 22)
radio.setPayloadSize(32)
radio.setChannel(77)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MIN)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()
radio.openReadingPipe(1,pipes)
radio.printDetails()
radio.startListening()
while(1):
# ackPL = [1]
while not radio.available(0):
time.sleep(1 / 100)
receivedMessage = []
radio.read(receivedMessage, radio.getDynamicPayloadSize())
print("Received: {}".format(receivedMessage))
Arduino
//Send.ino
#include<SPI.h>
#include<RF24.h>
// ce, csn pins
RF24 radio(6, 10);
uint32_t RcvMsg[8] = {0, 0, 0, 0, 0, 0, 0, 0};
void setup(void){
radio.begin();
radio.setPALevel(RF24_PA_MAX);
radio.setChannel(77);
radio.openWritingPipe(0xABCDABCD71LL);
radio.enableDynamicPayloads();
radio.powerUp();
}
void loop(void){
const char text[] = "Hello World is awe DOAs";
uint32_t SendMessage;
uint32_t SendMsg[8] = {222, micros(), 10, 20, 30, 40, 50, 60};
radio.write(&SendMsg, sizeof(SendMsg));
if ( radio.isAckPayloadAvailable() ) {
radio.read(&RcvMsg, sizeof(RcvMsg));
}
Serial.print(" SendMsg[0]: ");Serial.print(SendMsg[0]); Serial.print(" SendMsg[1]: ");Serial.print(SendMsg[1]); Serial.print(" SendMsg[2]: ");Serial.print(SendMsg[2]); Serial.print(" SendMsg[3]: ");Serial.print(SendMsg[3]); Serial.print(" SendMsg[4]: ");Serial.print(SendMsg[4]);Serial.print(" SendMsg[5]: ");Serial.print(SendMsg[5]);Serial.print(" SendMsg[6]: ");Serial.print(SendMsg[6]); Serial.print(" SendMsg[7]: ");Serial.println(SendMsg[7]);
Serial.print(" RcvMsg[0] : ");Serial.print(RcvMsg[0]);Serial.print(" RcvMsg[1] : ");Serial.print(RcvMsg[1]);Serial.print(" RcvMsg[2] : ");Serial.print(RcvMsg[2]);Serial.print(" RcvMsg[3] : ");Serial.print(RcvMsg[3]); Serial.print(" RcvMsg[4] : ");Serial.print(RcvMsg[4]);Serial.print(" RcvMsg[5] : ");Serial.print(RcvMsg[5]);Serial.print(" RcvMsg[6] : ");Serial.print(RcvMsg[6]);Serial.print(" RcvMsg[7] : ");Serial.println(RcvMsg[7]);
delay(1000);
}
答案 0 :(得分:0)
我找到了解决方法:
#============================================================================#
#============================================================================#
# translate radio Msq from byte to int
#============================================================================#
def translate_from_radio(msg, size):
translated_msg=[]
for i in range (0, size, 4):
translated_msg.append(int.from_bytes([msg[i+3], msg[i+2], msg[i+1], msg[i]], byteorder='big'))
#print("Translate FROM Radio: " + str(msg) + " --> " + str(translated_msg))
return translated_msg
#============================================================================#
#============================================================================#
# Split the msg element in 4 bytes and add it to translated msg
#============================================================================#
def translate_to_radio(msg):
translated_msg=[]
for i in range (0, len(msg)):
x=msg[i].to_bytes(4, byteorder='big')
for g in reversed(x):
translated_msg.append(g)
#print("Translate TO Radio: " + str(msg) + " --> " + str(translated_msg))
return translated_msg