Basic connectivity
Over the weekend I have been working on a nanoFramework C# library for my LoRa-E5 Development Kit from Seeedstudio. My initial test rig is based on an STM32F691DISCOVERY board which has an Arduino Uno R3 format socket for a Grove Base Shield V2.0, which I then connected to my LoRa-E5 Development Kit with a Grove – Universal 4 Pin 20cm Unbuckled Cable(TX/RX reversed)
The code has compile time options for synchronous and asynchronous operation.
public class Program
{
private const string SerialPortId = "COM6";
public static void Main()
{
SerialDevice serialDevice;
Debug.WriteLine("devMobile.IoT.SeeedLoRaE5.ShieldSerial starting");
Debug.WriteLine(Windows.Devices.SerialCommunication.SerialDevice.GetDeviceSelector());
try
{
serialDevice = SerialDevice.FromId(SerialPortId);
// set parameters
serialDevice.BaudRate = 9600;
serialDevice.Parity = SerialParity.None;
serialDevice.StopBits = SerialStopBitCount.One;
serialDevice.Handshake = SerialHandshake.None;
serialDevice.DataBits = 8;
serialDevice.ReadTimeout = new TimeSpan(0, 0, 30);
serialDevice.WriteTimeout = new TimeSpan(0, 0, 4);
DataWriter outputDataWriter = new DataWriter(serialDevice.OutputStream);
#if SERIAL_SYNC_READ
DataReader inputDataReader = new DataReader(serialDevice.InputStream);
#else
serialDevice.DataReceived += SerialDevice_DataReceived;
#endif
// set a watch char to be notified when it's available in the input stream
// This doesn't appear to work with synchronous calls
serialDevice.WatchChar = '\n';
while (true)
{
uint bytesWritten = outputDataWriter.WriteString("AT+VER\r\n");
Debug.WriteLine($"TX: {outputDataWriter.UnstoredBufferLength} bytes to output stream.");
// calling the 'Store' method on the data writer actually sends the data
uint txByteCount = outputDataWriter.Store();
Debug.WriteLine($"TX: {txByteCount} bytes via {serialDevice.PortName}");
#if SERIAL_SYNC_READ
uint bytesRead = inputDataReader.Load(50);
Debug.WriteLine($"RXs :{bytesRead} bytes read from {serialDevice.PortName}");
if (bytesRead > 0)
{
String response = inputDataReader.ReadString(bytesRead);
Debug.WriteLine($"RX sync:{response}");
}
#endif
Thread.Sleep(20000);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
private static void SerialDevice_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
switch (e.EventType)
{
case SerialData.Chars:
//Debug.WriteLine("RX SerialData.Chars");
break;
case SerialData.WatchChar:
Debug.WriteLine("RX: SerialData.WatchChar");
SerialDevice serialDevice = (SerialDevice)sender;
using (DataReader inputDataReader = new DataReader(serialDevice.InputStream))
{
inputDataReader.InputStreamOptions = InputStreamOptions.Partial;
// read all available bytes from the Serial Device input stream
uint bytesRead = inputDataReader.Load(serialDevice.BytesToRead);
Debug.WriteLine($"RXa: {bytesRead} bytes read from {serialDevice.PortName}");
if (bytesRead > 0)
{
String response = inputDataReader.ReadString(bytesRead);
Debug.WriteLine($"RX:{response}");
}
}
break;
default:
Debug.Assert(false, $"e.EventType {e.EventType} unknown");
break;
}
}
}
I have reused a significant amount of code built for my nanoFramework RAK811 LoRaWAN library Part1 post.
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
devMobile.IoT.SeeedLoRaE5.ShieldSerial starting
Ports available: COM5,COM6
TX: 8 bytes to output stream.
TX: 8 bytes via COM6
TX: 8 bytes to output stream.
TX: 8 bytes via COM6
RX: SerialData.WatchChar
RXa: 28 bytes read from COM6
RX:+VER: 4.0.11
+VER: 4.0.11
TX: 8 bytes to output stream.
TX: 8 bytes via COM6
RX: SerialData.WatchChar
RXa: 14 bytes read from COM6
RX:+VER: 4.0.11
TX: 8 bytes to output stream.
TX: 8 bytes via COM6
RX: SerialData.WatchChar
RXa: 14 bytes read from COM6
RX:+VER: 4.0.11
TX: 8 bytes to output stream.
TX: 8 bytes via COM6
RX: SerialData.WatchChar
RXa: 14 bytes read from COM6
RX:+VER: 4.0.11
The test rig confirmed that I had the right configuration for the hardware (TX-RX twist) and LoRa-E5 connection (serial port, baud rate, parity etc.)