TinyCLR OS LoRa library Part5

Receive Basic

For testing this code I used the same version of the LoRaSetSyncWord example as Transmit Basic

15:01:06.073 -> LoRa init succeeded.
15:01:06.584 -> Sending HeLoRa World! 0
15:01:08.489 -> Message: ⸮LoRaIoT1Maduino2at 74.3,ah 56,wsa 2,wsg 6,wd 15.75,r 0.00,
15:01:08.593 -> RSSI: -76
15:01:08.593 -> Snr: 9.25
15:01:08.593 -> 
15:01:17.575 -> Sending HeLoRa World! 2
15:01:28.352 -> Sending HeLoRa World! 4
15:01:38.675 -> Sending HeLoRa World! 6
15:01:49.239 -> Sending HeLoRa World! 8
15:02:00.006 -> Sending HeLoRa World! 10

The TinyCLR-OS code builds on my TinyCLR-OS Transmit Basic samples.

   class Program
   {
      static void Main()
      {
         Rfm9XDevice rfm9XDevice = new Rfm9XDevice(FEZ.GpioPin.D10, FEZ.GpioPin.D9);

         // 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);

         rfm9XDevice.RegisterWriteByte(0x0F, 0x0); // RegFifoRxBaseAddress 

         rfm9XDevice.RegisterWriteByte(0x01, 0b10000101); // RegOpMode set LoRa & RxContinuous

         while (true)
         {
            // Wait until a packet is received, no timeouts in PoC
            Debug.WriteLine("Receive-Wait");
            byte irqFlags = rfm9XDevice.RegisterReadByte(0x12); // RegIrqFlags
            while ((irqFlags & 0b01000000) == 0)  // wait until RxDone cleared
            {
               Thread.Sleep(100);
               irqFlags = rfm9XDevice.RegisterReadByte(0x12); // RegIrqFlags
               //Debug.Write(".");
            }
            Debug.WriteLine("");
            Debug.WriteLine($"RegIrqFlags 0X{irqFlags:X2}");
            Debug.WriteLine("Receive-Message");
            byte currentFifoAddress = rfm9XDevice.RegisterReadByte(0x10); // RegFifiRxCurrent
            rfm9XDevice.RegisterWriteByte(0x0d, currentFifoAddress); // RegFifoAddrPtr

            byte numberOfBytes = rfm9XDevice.RegisterReadByte(0x13); // RegRxNbBytes

            byte[] messageBytes = rfm9XDevice.RegisterRead(0x00, numberOfBytes); // RegFifo

            rfm9XDevice.RegisterWriteByte(0x0d, 0);
            rfm9XDevice.RegisterWriteByte(0x12, 0b11111111); // RegIrqFlags clear all the bits

            string messageText = UTF8Encoding.UTF8.GetString(messageBytes);
            Debug.WriteLine($"Received {messageBytes.Length} byte message {messageText}");

            Debug.WriteLine("Receive-Done");
         }
      }
   }

The receive code works reliably but has no error detection or correction capability.

RegIrqFlags 0X50
Receive-Message
Received 15 byte message HeLoRa World! 0
Receive-Done
Receive-Wait

RegIrqFlags 0X50
Receive-Message
Received 59 byte message  �LoRaIoT1Maduino2at 74.3,ah 56,wsa 2,wsg 6,wd 15.75,r 0.00,
Receive-Done
Receive-Wait

RegIrqFlags 0X50
Receive-Message
Received 15 byte message HeLoRa World! 2
Receive-Done
Receive-Wait

RegIrqFlags 0X50
Receive-Message
Received 15 byte message HeLoRa World! 4
Receive-Done
Receive-Wait

RegIrqFlags 0X50
Receive-Message
Received 15 byte message HeLoRa World! 6
Receive-Done
Receive-Wait

RegIrqFlags 0X50
Receive-Message
Received 15 byte message HeLoRa World! 8
Receive-Done
Receive-Wait

RegIrqFlags 0X50
Receive-Message
Received 16 byte message HeLoRa World! 10
Receive-Done
Receive-Wait

RegIrqFlags 0X50
Receive-Message
Received 59 byte message  �LoRaIoT1Maduino2at 74.1,ah 56,wsa 3,wsg 8,wd 46.50,r 0.00,
Receive-Done
Receive-Wait

I will look at implementing some sort of carrier-sense multiple access with collision avoidance solution to reduce the number of corrupted messages when two (or possibly more devices) transmit at the same time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.