TinyCLR OS V2 RC1 RAK811 LoRaWAN library Part1

Basic connectivity

Over the weekend I have been working on a GHI Electronics TinyCLR V2  C# library for my modified RAK811 LPWAN Evaluation Board(EVB) from RAK Wireless. My initial test rig is based on an Fezduino board which has Arduino Uno R3 format socket for the EVB.

Fezduino with RAK Wisnode shield

The code has compile time options for synchronous and asynchronous operation.

   public class Program
   {
      private static UartController serialDevice;
      private const string ATCommand = "at+version\r\n";
#if TINYCLR_V2_FEZDUINO
      private static string SerialPortId = SC20100.UartPort.Uart5;
#endif

      public static void Main()
      {
         Debug.WriteLine("devMobile.IoT.Rak811.ShieldSerial starting");

         try
         {
            serialDevice = UartController.FromName(SerialPortId);

            serialDevice.SetActiveSettings(new UartSetting()
            {
               BaudRate = 9600,
               Parity = UartParity.None,
               StopBits = UartStopBitCount.One,
               Handshaking = UartHandshake.None,
               DataBits = 8
            });

            serialDevice.Enable();

#if SERIAL_ASYNC_READ
            serialDevice.DataReceived += SerialDevice_DataReceived;
#endif

            while (true)
            {
               byte[] txBuffer = UTF8Encoding.UTF8.GetBytes(ATCommand);

               int txByteCount = serialDevice.Write(txBuffer);
               Debug.WriteLine($"TX: {txByteCount} bytes");

#if SERIAL_SYNC_READ
               while( serialDevice.BytesToWrite>0)
               {
                  Debug.WriteLine($" BytesToWrite {serialDevice.BytesToWrite}");
                  Thread.Sleep(100);
               }

               int rxByteCount = serialDevice.BytesToRead;
               if (rxByteCount>0)
               {
                  byte[] rxBuffer = new byte[rxByteCount];

                  serialDevice.Read(rxBuffer);

                  Debug.WriteLine($"RX sync:{rxByteCount} bytes read");
                  String response = UTF8Encoding.UTF8.GetString(rxBuffer);
                  Debug.WriteLine($"RX sync:{response}");
               }
#endif

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


#if SERIAL_ASYNC_READ
      private static void SerialDevice_DataReceived(UartController sender, DataReceivedEventArgs e)
      {
         byte[] rxBuffer = new byte[e.Count];

         serialDevice.Read(rxBuffer, 0, e.Count);

         Debug.WriteLine($"RX Async:{e.Count} bytes read");
         String response = UTF8Encoding.UTF8.GetString(rxBuffer);
         Debug.WriteLine($"RX Async:{response}");
      }
#endif
   }

When I first ran the code I noticed the serialDevice.Read timed out before any characters were received.

The thread '<No Name>' (0x2) has exited with code 0 (0x0).
devMobile.IoT.Rak811.ShieldSerial starting
TX: 12 bytes
TX: 12 bytes
RX sync:19 bytes read
RX sync:OK V3.0.0.13.H.T3

TX: 12 bytes
RX sync:19 bytes read
RX sync:OK V3.0.0.13.H.T3

TX: 12 bytes
RX sync:19 bytes read
RX sync:OK V3.0.0.13.H.T3

I then added code to check the message had been sent and the code worked as expected. I now think, that rather than checking that the characters had been sent the short 100mSec delay was more important.

The thread '<No Name>' (0x2) has exited with code 0 (0x0).
devMobile.IoT.Rak811.ShieldSerial starting
TX: 12 bytes
 BytesToWrite 10
RX sync:19 bytes read
RX sync:OK V3.0.0.13.H.T3

TX: 12 bytes
 BytesToWrite 10
RX sync:19 bytes read
RX sync:OK V3.0.0.13.H.T3

TX: 12 bytes
 BytesToWrite 10
RX sync:19 bytes read
RX sync:OK V3.0.0.13.H.T3

I then added code to receive data asynchronously and the response to the version request was received as expected.

The thread '<No Name>' (0x2) has exited with code 0 (0x0).
devMobile.IoT.Rak811.ShieldSerial starting
TX: 12 bytes
RX Async:1 bytes read
RX Async:O
RX Async:8 bytes read
RX Async:K V3.0.0
RX Async:10 bytes read
RX Async:.13.H.T3

TX: 12 bytes
RX Async:1 bytes read
RX Async:O
RX Async:5 bytes read
RX Async:K V3.
RX Async:9 bytes read
RX Async:0.0.13.H.
RX Async:4 bytes read
RX Async:T3

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.