【问题标题】:Communication between Arduino and Raspberry Pi using nRF24L01p使用 nRF24L01p 在 Arduino 和 Raspberry Pi 之间进行通信
【发布时间】:2019-09-01 02:06:15
【问题描述】:

Arduino Connection Pi Connection

我想使用 nRF24L01p 模块设置 Arduino 和 Raspberry Pi(运行 win10 iot)之间的通信。我使用“IS_nRF24L01p”nuget 包作为模块的驱动程序。我的 arduino 正在发送一个“Hello World”字符串。在这种情况下,RPi 是一个接收器,程序只是检查它们的通信是否建立。但我做不到。是不是因为收到数据的事件没有触发?还是配置不正确。提前感谢您的帮助。

发射器 (Arduino) 代码

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8);

const byte rxAddr[6] = "00001";

void setup()
{
  Serial.begin(9600);
  radio.begin();
  radio.setRetries(15, 15);
  radio.setChannel(10);
  radio.openWritingPipe(rxAddr);
  radio.stopListening();
}

void loop()
{
  const char text[] = "Hello World";
  radio.write(&text, sizeof(text));

  delay(1000);
  Serial.println("Sent."); // TO check whether the data is sent.

}

接收器 (Raspberry Pi) 代码

using System;
using Windows.UI.Xaml.Controls;
using Windows.Devices.Spi;
using IS_nRF24L01p;
using System.Threading.Tasks;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace New
{

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public nRF24L01P nRf24 = new nRF24L01P();
        public MainPage()
        {
            this.InitializeComponent();
            setup();

        }



        private void setup()
        {
            System.Diagnostics.Debug.WriteLine("Initialize MTPNordic");
            nRf24.InitNordicSPI(new SpiConnectionSettings(0), 3000000, 12, 13);
            RFConfiguration();
            }

        private void RFConfiguration()
        {
            byte configByte;
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.RF_CH, new byte[] { 10 });

            configByte = 0;
            nRf24.SetRegisterBit(ref configByte, true, 0);
            nRf24.SetRegisterBit(ref configByte, true, 1);
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.SETUP_AW, new byte[] { configByte });

            configByte = 0;
            nRf24.SetRegisterBit(ref configByte, true, 1);
            nRf24.SetRegisterBit(ref configByte, true, 2);
            nRf24.SetRegisterBit(ref configByte, false, 3);
            nRf24.SetRegisterBit(ref configByte, true, 5);
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.RF_SETUP, new byte[configByte]);

            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.SETUP_RETR, new byte[] { 0x23 });
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.FEATURE, new byte[] { System.Convert.ToByte(Math.Pow(2, 2)) });
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.DYNPD, new byte[] { 0x3F });
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.EN_AA, new byte[] { 0x1 });
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.TX_ADDR, new byte[] { 0x30, 0x30, 0x30, 0x30, 0x31 });
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.EN_RXADDR, new byte[] { 0x3 });

            SetAsPRX();
            nRf24.SetChipEnable(true);
            Task.Delay(1);
            System.Diagnostics.Debug.WriteLine("Finish RFConfiguration()");
        }

        private void SetAsPRX()
        {
            byte configByte;
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.RX_ADDR_P0, new byte[] { 0x30, 0x30, 0x30, 0x30, 0x31 });

            configByte = 0;
            nRf24.SetRegisterBit(ref configByte, true, 0);   
            nRf24.SetRegisterBit(ref configByte, true, 1);     
            nRf24.SetRegisterBit(ref configByte, true, 2); 
            nRf24.SetRegisterBit(ref configByte, true, 3);
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.CONFIG, new byte[] { configByte });
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.STATUS, new byte[] { 0xE });

            byte[] StatusBuffer = null;
            nRf24.FlushRXFIFO();
            StatusBuffer = nRf24.ReadRegister(IS_nRF24L01p.Common.Registers.STATUS);
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.STATUS, new byte[] { System.Convert.ToByte(StatusBuffer[0] | 0x40) });
            System.Diagnostics.Debug.WriteLine("Finish SetAsPrx()");
        }

        private void nRf24_OnDataReceived(object sender, nRF24L01P.DataReceivedEventArgs e)
        {
            int PayloadLength = e.Payload.Length;
            string ReceivedString = "Data Received.";
            System.Diagnostics.Debug.WriteLine(ReceivedString); // TO check if the program enter this function == Data Received
        }

    }
}

从调试输出更新

'New.exe' (CoreCLR: DefaultDomain): Loaded 'C:\Data\Programs\WindowsApps\Microsoft.NET.CoreRuntime.2.1_2.1.26124.5_arm__8wekyb3d8bbwe\System.Private.CoreLib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\entrypoint\New.exe'. Symbols loaded.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\System.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\WinMetadata\Windows.winmd'. Module was built without symbols.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\System.Runtime.InteropServices.WindowsRuntime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\System.Runtime.WindowsRuntime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\System.Runtime.WindowsRuntime.UI.Xaml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\System.Collections.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\System.Threading.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\IS_nRF24L01p.dll'. Cannot find or open the PDB file.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\System.Private.Uri.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\System.Diagnostics.Debug.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\System.Threading.Tasks.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Initialize MTPNordic
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\Microsoft.VisualBasic.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\netstandard.dll'. Module was built without symbols.
'New.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\b55d8275-9b3c-41b5-9a76-be21517cd280VS.Debug_ARM.New\System.Runtime.Extensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Finish SetAsPrx()
Finish RFConfiguration()
The thread 0xf30 has exited with code 0 (0x0).

【问题讨论】:

    标签: c# arduino raspberry-pi3 windows-10-iot-core


    【解决方案1】:

    从您提供的代码中,我找不到注册nRf24_OnDataReceived 事件处理程序的行。请尝试像以下代码一样添加处理程序。

        private void setup()
        {
            System.Diagnostics.Debug.WriteLine("Initialize MTPNordic");
            nRf24.InitNordicSPI(new SpiConnectionSettings(0), 3000000, 12, 13);
            nRf24.OnDataReceived += new nRF24L01P.OnDataReceivedEventHandler(nRf24_OnDataReceived);
            RFConfiguration();
        }
    

    抱歉,我没有 nRF24L01p 模块来测试这个问题。如果对您有帮助,请随时告诉我。

    更新:

    发射器 (Arduino) 代码:

    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    
    RF24 radio(7, 8);
    const byte address[6] = "00001";
    
    void setup()
    {
      Serial.begin(9600);
      radio.begin();
      radio.setRetries(15, 15);
      radio.setChannel(10);
      radio.setPALevel(RF24_PA_MIN);
      radio.openWritingPipe(address);
      radio.stopListening();
    }
    
    void loop()
    {
      const char text[] = "Hello World";
      radio.write(&text, sizeof(text));
    
      delay(1000);
      Serial.println("Sent."); // TO check whether the data is sent.
    
    }
    

    接收器 (Raspberry Pi) 代码:

        private void SetAsPRX()
        {
            byte configByte;
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.RX_ADDR_P1, new byte[] { 0xF0, 0xF0, 0xF, 0x0F, 0x0E1 });
    
            configByte = 0;
            nRf24.SetRegisterBit(ref configByte, true, 0);
            nRf24.SetRegisterBit(ref configByte, true, 1);
            nRf24.SetRegisterBit(ref configByte, true, 2);
            nRf24.SetRegisterBit(ref configByte, true, 3);
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.CONFIG, new byte[] { configByte });
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.STATUS, new byte[] { 0xE });
    
            byte[] StatusBuffer = null;
            nRf24.FlushRXFIFO();
            StatusBuffer = nRf24.ReadRegister(IS_nRF24L01p.Common.Registers.STATUS);
            nRf24.WriteRegister(IS_nRF24L01p.Common.Registers.STATUS, new byte[] { System.Convert.ToByte(StatusBuffer[0] | 0x40) });
            System.Diagnostics.Debug.WriteLine("Finish SetAsPrx()");
        }
    

    发送方发送数据时,数据包中的地址采用“TX_ADD”的地址。该地址是接收端的几个接收通道(6 个通道)之一。接收到数据后,接收端对应的通道进行响应,采用响应包中的地址。接收通道对应的地址为发送端的发送通道地址,发送端默认使用通道0应答最多,所以发送端的通道0地址应设置为发送通道地址。

    接收最好使用1-5个通道,因为发送时会使用0个通道来接收响应,并且在发送数据之前将对方的一个接收通道的地址写入发送通道。

    【讨论】:

    • 感谢您的帮助。我添加了代码,但仍然没有任何从 Arduino 接收数据的迹象。
    • @ChenL,我已经更新了代码,请尝试使用这个进行测试。
    • 再次感谢。但它不起作用。可能的错误是什么? SPI是用IS_nrf24l01p nuget包在内部初始化的吗?
    • 我又试了一次,检查了所有的电线连接。没有问题。所以,我也上传了问题的调试输出。
    • @ChenL,SPI在InitNordicSPI方法中初始化。我认为问题可能是地址或渠道造成的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    相关资源
    最近更新 更多