.NET Core SX127X library Part4

Receive Basic

Next step was proving I could receive a message sent by an Arduino device running the LoRaSimpleNode example from the SandeepMistry Arduino LoRa library. For some variety I’m using a Dragino Raspberry Pi HAT featuring GPS and LoRa® technology on my Raspberry PI.

Dragino pHat on my Raspberry 3 device
Arduino Monitor output running LoRaSimpleNode example code

The Dragino pHat has the same pin configuration as the M2M 1 Channel LoRaWAN Gateway Shield for Raspberry Pi so there weren’t any code changes.

class Program
{
	static void Main(string[] args)
	{
		// M2M device has reset pin uses non standard chip select 
		SX127XDevice sX127XDevice = new SX127XDevice(chipSelectLine: 1, chipSelectLogicalPinNumber: 25, resetPin: 17);

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

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

		sX127XDevice.WriteByte(0x0F, 0x0); // RegFifoRxBaseAddress 

		sX127XDevice.WriteByte(0x01, 0b10000101); // RegOpMode set LoRa & RxContinuous

		while (true)
		{
			// Wait until a packet is received, no timeouts in PoC
			Console.WriteLine("Receive-Wait");
			byte IrqFlags = sX127XDevice.ReadByte(0x12); // RegIrqFlags
			while ((IrqFlags & 0b01000000) == 0)  // wait until RxDone cleared
			{
				Thread.Sleep(100);
				IrqFlags = sX127XDevice.ReadByte(0x12); // RegIrqFlags
				Debug.Write(".");
			}
			Console.WriteLine("");
			Console.WriteLine($"RegIrqFlags {Convert.ToString((byte)IrqFlags, 2).PadLeft(8, '0')}");
			Console.WriteLine("Receive-Message");
			byte currentFifoAddress = sX127XDevice.ReadByte(0x10); // RegFifiRxCurrent
			sX127XDevice.WriteByte(0x0d, currentFifoAddress); // RegFifoAddrPtr

			byte numberOfBytes = sX127XDevice.ReadByte(0x13); // RegRxNbBytes

			// Read the message from the FIFO
			byte[] messageBytes = sX127XDevice.ReadBytes(0x00, numberOfBytes);

			sX127XDevice.WriteByte(0x0d, 0);
			sX127XDevice.WriteByte(0x12, 0b11111111); // RegIrqFlags clear all the bits

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

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

There wasn’t much code to configure the SX127X to receive messages. The device has to be put into sleep mode (RegOpMode), the frequency set to 915MHz(RegFrMsb, RegFrMid, RegFrLsb) and receiver enabled(RxContinuous).

While running the applications polls (RegIrqFlags) until a message has arrived (RxDone). It then gets a pointer to the start of the message buffer (RegFifiRxCurrent, RegFifoAddrPtr), gets the message length, and then reads the message (RegPayloadLength, RegFifo) from the buffer. Finally the flags are reset ready for the next message(RegIrqFlags)

Loaded '/usr/lib/dotnet/shared/Microsoft.NETCore.App/5.0.4/System.Memory.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Sending 28 bytes message Hello LoRa from .NET Core! 1
RegIrqFlags 00001000
Transmit-Done
RegIrqFlags 01010000
Receive-Message
Received 31 byte message HeLoRa World! I'm a Node! 20000
Sending 28 bytes message Hello LoRa from .NET Core! 2
RegIrqFlags 00001000
Transmit-Done
RegIrqFlags 01010000
Receive-Message
Received 31 byte message HeLoRa World! I'm a Node! 30000
Sending 28 bytes message Hello LoRa from .NET Core! 3
RegIrqFlags 00001000
Transmit-Done
RegIrqFlags 01010000
Receive-Message
Received 31 byte message HeLoRa World! I'm a Node! 40000
Sending 28 bytes message Hello LoRa from .NET Core! 4
RegIrqFlags 00001000
Transmit-Done
RegIrqFlags 01010000
Receive-Message
Received 31 byte message HeLoRa World! I'm a Node! 50000
Sending 28 bytes message Hello LoRa from .NET Core! 5
RegIrqFlags 00001000
Transmit-Done
RegIrqFlags 01010000
Receive-Message
Received 31 byte message HeLoRa World! I'm a Node! 60000
Sending 28 bytes message Hello LoRa from .NET Core! 6
RegIrqFlags 00001000
Transmit-Done
RegIrqFlags 01010000
Receive-Message
Received 31 byte message HeLoRa World! I'm a Node! 70000
Sending 28 bytes message Hello LoRa from .NET Core! 7
RegIrqFlags 00001000
Transmit-Done
RegIrqFlags 01010000
Receive-Message
Received 31 byte message HeLoRa World! I'm a Node! 80000
Sending 28 bytes message Hello LoRa from .NET Core! 8
RegIrqFlags 00001000
Transmit-Done

Summary

In this iteration I sent a from my a Dragino LoRa Shield 915MHz on a Seeeduino V4.2 device to my .Net Core 5 dotnet/iot powered Raspberry PI.

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.