コード例 #1
0
ファイル: Program.cs プロジェクト: lulzzz/RFM9XLoRa-TinyCLR
        static void Main()
        {
            Rfm9XDevice rfm9XDevice = new Rfm9XDevice(FEZ.GpioPin.D10, FEZ.GpioPin.D9);
            int         SendCount   = 0;

            // Put device into LoRa + Sleep mode
            rfm9XDevice.RegisterWriteByte(0x01, 0b10000000); // RegOpMode

            // Set the frequency to 915MHz
            byte[] frequencyWriteBytes = { 0xE4, 0xC0, 0x00 }; // RegFrMsb, RegFrMid, RegFrLsb
            rfm9XDevice.RegisterWrite(0x06, frequencyWriteBytes);

            // More power PA Boost
            rfm9XDevice.RegisterWriteByte(0x09, 0b10000000); // RegPaConfig

            rfm9XDevice.RegisterDump();

            while (true)
            {
                rfm9XDevice.RegisterWriteByte(0x0E, 0x0); // RegFifoTxBaseAddress

                // Set the Register Fifo address pointer
                rfm9XDevice.RegisterWriteByte(0x0D, 0x0); // RegFifoAddrPtr

                string messageText = $"Hello LoRa {SendCount += 1}!";

                // load the message into the fifo
                byte[] messageBytes = UTF8Encoding.UTF8.GetBytes(messageText);
                rfm9XDevice.RegisterWrite(0x0, messageBytes); // RegFifo

                // Set the length of the message in the fifo
                rfm9XDevice.RegisterWriteByte(0x22, (byte)messageBytes.Length); // RegPayloadLength

                Debug.WriteLine($"Sending {messageBytes.Length} bytes message {messageText}");
                /// Set the mode to LoRa + Transmit
                rfm9XDevice.RegisterWriteByte(0x01, 0b10000011); // RegOpMode

                // Wait until send done, no timeouts in PoC
                Debug.WriteLine("Send-wait");
                byte IrqFlags = rfm9XDevice.RegisterReadByte(0x12); // RegIrqFlags
                while ((IrqFlags & 0b00001000) == 0)                // wait until TxDone cleared
                {
                    Thread.Sleep(10);
                    IrqFlags = rfm9XDevice.RegisterReadByte(0x12); // RegIrqFlags
                    Debug.WriteLine(".");
                }
                rfm9XDevice.RegisterWriteByte(0x12, 0b00001000); // clear TxDone bit
                Debug.WriteLine("Send-Done");

                Thread.Sleep(30000);
            }
        }
コード例 #2
0
        static void Main()
        {
            int SendCount = 0;

#if ST_STM32F429I_DISCOVERY
            int chipSelectPinNumber = PinNumber('C', 2);
            int resetPinNumber      = PinNumber('C', 3);
#endif
#if ESP32_WROOM_32_LORA_1_CHANNEL
            int chipSelectPinNumber = Gpio.IO16;
#endif
#if NETDUINO3_WIFI
            int chipSelectPinNumber = PinNumber('B', 10);
            int resetPinNumber      = PinNumber('E', 5);
#endif

            try
            {
#if ESP32_WROOM_32_LORA_1_CHANNEL
                Configuration.SetPinFunction(Gpio.IO12, DeviceFunction.SPI1_MISO);
                Configuration.SetPinFunction(Gpio.IO13, DeviceFunction.SPI1_MOSI);
                Configuration.SetPinFunction(Gpio.IO14, DeviceFunction.SPI1_CLOCK);
                Rfm9XDevice rfm9XDevice = new Rfm9XDevice(SpiBusId, chipSelectPinNumber);
#endif
#if ST_STM32F429I_DISCOVERY || NETDUINO3_WIFI
                Rfm9XDevice rfm9XDevice = new Rfm9XDevice(SpiBusId, chipSelectPinNumber, resetPinNumber);
#endif
                Thread.Sleep(500);

                // Put device into LoRa + Standby mode
                rfm9XDevice.RegisterWriteByte(0x01, 0b10000000); // RegOpMode

                // Set the frequency to 915MHz
                byte[] frequencyWriteBytes = { 0xE4, 0xC0, 0x00 }; // RegFrMsb, RegFrMid, RegFrLsb
                rfm9XDevice.RegisterWrite(0x06, frequencyWriteBytes);

                // More power PA Boost
                rfm9XDevice.RegisterWriteByte(0x09, 0b10000000); // RegPaConfig

                rfm9XDevice.RegisterDump();

                while (true)
                {
                    rfm9XDevice.RegisterWriteByte(0x0E, 0x0); // RegFifoTxBaseAddress

                    // Set the Register Fifo address pointer
                    rfm9XDevice.RegisterWriteByte(0x0D, 0x0); // RegFifoAddrPtr

                    string messageText = $"Hello LoRa {SendCount += 1}!";

                    // load the message into the fifo
                    byte[] messageBytes = UTF8Encoding.UTF8.GetBytes(messageText);
                    rfm9XDevice.RegisterWrite(0x0, messageBytes); // RegFifo

                    // Set the length of the message in the fifo
                    rfm9XDevice.RegisterWriteByte(0x22, (byte)messageBytes.Length); // RegPayloadLength

                    Debug.WriteLine($"Sending {messageBytes.Length} bytes message {messageText}");
                    /// Set the mode to LoRa + Transmit
                    rfm9XDevice.RegisterWriteByte(0x01, 0b10000011); // RegOpMode

                    // Wait until send done, no timeouts in PoC
                    Debug.WriteLine("Send-wait");
                    byte IrqFlags = rfm9XDevice.RegisterReadByte(0x12); // RegIrqFlags
                    while ((IrqFlags & 0b00001000) == 0)                // wait until TxDone cleared
                    {
                        Thread.Sleep(10);
                        IrqFlags = rfm9XDevice.RegisterReadByte(0x12); // RegIrqFlags
                        Debug.Write(".");
                    }
                    Debug.WriteLine("");
                    rfm9XDevice.RegisterWriteByte(0x12, 0b00001000); // clear TxDone bit
                    Debug.WriteLine("Send-Done");

                    Thread.Sleep(10000);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }