diff --git a/NetworkJoinABP/03.NetworkJoinABP.nfproj b/NetworkJoinABP/03.NetworkJoinABP.nfproj
index 78daa3b..16bc3af 100644
--- a/NetworkJoinABP/03.NetworkJoinABP.nfproj
+++ b/NetworkJoinABP/03.NetworkJoinABP.nfproj
@@ -37,13 +37,8 @@
True
True
-
- ..\packages\nanoFramework.Windows.Devices.SerialCommunication.1.3.4\lib\Windows.Devices.SerialCommunication.dll
- True
- True
-
-
- ..\packages\nanoFramework.Windows.Storage.Streams.1.12.2\lib\Windows.Storage.Streams.dll
+
+ ..\packages\nanoFramework.System.IO.Ports.1.0.0\lib\System.IO.Ports.dll
True
True
diff --git a/NetworkJoinABP/Program.cs b/NetworkJoinABP/Program.cs
index 1793b93..3e24b9d 100644
--- a/NetworkJoinABP/Program.cs
+++ b/NetworkJoinABP/Program.cs
@@ -19,193 +19,111 @@ namespace devMobile.IoT.Rak811.NetworkJoinABP
{
using System;
using System.Diagnostics;
+ using System.IO.Ports;
using System.Threading;
- using Windows.Devices.SerialCommunication;
- using Windows.Storage.Streams;
-
public class Program
{
private const string SerialPortId = "COM6";
- private const string DevAddress = "";
- private const string NwksKey = "";
- private const string AppsKey = "";
+ private const string DevAddress = "...";
+ private const string NwksKey = "...";
+ private const string AppsKey = "...";
private const byte MessagePort = 1;
private const string Payload = "48656c6c6f204c6f526157414e"; // Hello LoRaWAN
public static void Main()
{
- SerialDevice serialDevice;
- uint bytesWritten;
- uint txByteCount;
- uint bytesRead;
+ string response;
Debug.WriteLine("devMobile.IoT.Rak811.NetworkJoinABP starting");
- Debug.WriteLine(Windows.Devices.SerialCommunication.SerialDevice.GetDeviceSelector());
+ Debug.Write("Ports:");
+ foreach (string port in SerialPort.GetPortNames())
+ {
+ Debug.Write($" {port}");
+ }
+ Debug.WriteLine("");
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, 3);
- serialDevice.WriteTimeout = new TimeSpan(0, 0, 4);
-
- DataWriter outputDataWriter = new DataWriter(serialDevice.OutputStream);
- DataReader inputDataReader = new DataReader(serialDevice.InputStream);
-
- // set a watch char to be notified when it's available in the input stream
- serialDevice.WatchChar = '\n';
-
- // clear out the RX buffer
- bytesRead = inputDataReader.Load(128);
- while (bytesRead > 0)
- {
- string response = inputDataReader.ReadString(bytesRead);
- Debug.WriteLine($"RX :{response}");
-
- bytesRead = inputDataReader.Load(128);
- }
-
- // Set the Working mode to LoRaWAN
- bytesWritten = outputDataWriter.WriteString("at+set_config=lora:work_mode:0\r\n");
- Debug.WriteLine($"TX: work_mode {outputDataWriter.UnstoredBufferLength} bytes to output stream.");
- txByteCount = outputDataWriter.Store();
- Debug.WriteLine($"TX: {txByteCount} bytes via {serialDevice.PortName}");
-
- // Read the response
- bytesRead = inputDataReader.Load(128);
- if (bytesRead > 0)
- {
- string response = inputDataReader.ReadString(bytesRead);
- Debug.WriteLine($"RX :{response}");
- }
-
- // Set the Region to AS923
- bytesWritten = outputDataWriter.WriteString("at+set_config=lora:region:AS923\r\n");
- Debug.WriteLine($"TX: region {outputDataWriter.UnstoredBufferLength} bytes to output stream.");
- txByteCount = outputDataWriter.Store();
- Debug.WriteLine($"TX: {txByteCount} bytes via {serialDevice.PortName}");
-
- // Read the response
- bytesRead = inputDataReader.Load(128);
- if (bytesRead > 0)
- {
- String response = inputDataReader.ReadString(bytesRead);
- Debug.WriteLine($"RX :{response}");
- }
-
- // Set the JoinMode to ABP
- bytesWritten = outputDataWriter.WriteString("at+set_config=lora:join_mode:1\r\n");
- Debug.WriteLine($"TX: join_mode {outputDataWriter.UnstoredBufferLength} bytes to output stream.");
- txByteCount = outputDataWriter.Store();
- Debug.WriteLine($"TX: {txByteCount} bytes via {serialDevice.PortName}");
-
- // Read the response
- bytesRead = inputDataReader.Load(128);
- if (bytesRead > 0)
- {
- String response = inputDataReader.ReadString(bytesRead);
- Debug.WriteLine($"RX :{response}");
- }
-
- // Set the device address
- bytesWritten = outputDataWriter.WriteString($"at+set_config=lora:dev_addr:{DevAddress}\r\n");
- Debug.WriteLine($"TX: dev_addr {outputDataWriter.UnstoredBufferLength} bytes to output stream.");
- txByteCount = outputDataWriter.Store();
- Debug.WriteLine($"TX: {txByteCount} bytes via {serialDevice.PortName}");
-
- // Read the response
- bytesRead = inputDataReader.Load(128);
- if (bytesRead > 0)
+ using (SerialPort serialDevice = new SerialPort(SerialPortId))
{
- String response = inputDataReader.ReadString(bytesRead);
- Debug.WriteLine($"RX :{response}");
- }
-
- // Set the network session key
- bytesWritten = outputDataWriter.WriteString($"at+set_config=lora:nwks_key:{NwksKey}\r\n");
- Debug.WriteLine($"TX: nwks_key {outputDataWriter.UnstoredBufferLength} bytes to output stream.");
- txByteCount = outputDataWriter.Store();
- Debug.WriteLine($"TX: {txByteCount} bytes via {serialDevice.PortName}");
-
- // Read the response
- bytesRead = inputDataReader.Load(128);
- if (bytesRead > 0)
- {
- String response = inputDataReader.ReadString(bytesRead);
- Debug.WriteLine($"RX :{response}");
- }
-
- // Set the application session key
- bytesWritten = outputDataWriter.WriteString($"at+set_config=lora:apps_key:{AppsKey}\r\n");
- Debug.WriteLine($"TX: apps_key {outputDataWriter.UnstoredBufferLength} bytes to output stream.");
- txByteCount = outputDataWriter.Store();
- Debug.WriteLine($"TX: {txByteCount} bytes via {serialDevice.PortName}");
-
- // Read the response
- bytesRead = inputDataReader.Load(128);
- if (bytesRead > 0)
- {
- String response = inputDataReader.ReadString(bytesRead);
- Debug.WriteLine($"RX :{response}");
- }
-
- // Set the Confirm flag
- bytesWritten = outputDataWriter.WriteString("at+set_config=lora:confirm:0\r\n");
- Debug.WriteLine($"TX: confirm {outputDataWriter.UnstoredBufferLength} bytes to output stream.");
- txByteCount = outputDataWriter.Store();
- Debug.WriteLine($"TX: {txByteCount} bytes via {serialDevice.PortName}");
-
- // Read the response
- bytesRead = inputDataReader.Load(128);
- if (bytesRead > 0)
- {
- String response = inputDataReader.ReadString(bytesRead);
- Debug.WriteLine($"RX :{response}");
- }
-
-
- // Join the network
- bytesWritten = outputDataWriter.WriteString("at+join\r\n");
- Debug.WriteLine($"TX: join {outputDataWriter.UnstoredBufferLength} bytes to output stream.");
- txByteCount = outputDataWriter.Store();
- Debug.WriteLine($"TX: {txByteCount} bytes via {serialDevice.PortName}");
-
- // Read the response
- bytesRead = inputDataReader.Load(128);
- while (bytesRead >= 0)
- {
- String response = inputDataReader.ReadString(bytesRead);
- Debug.WriteLine($"RX :{response}");
-
- bytesRead = inputDataReader.Load(128);
- }
-
- while (true)
- {
- bytesWritten = outputDataWriter.WriteString($"at+send=lora:{MessagePort}:{Payload}\r\n");
- Debug.WriteLine($"TX: send {outputDataWriter.UnstoredBufferLength} bytes to output stream.");
+ // set parameters
+ serialDevice.BaudRate = 9600;
+ serialDevice.Parity = Parity.None;
+ serialDevice.StopBits = StopBits.One;
+ serialDevice.Handshake = Handshake.None;
+ serialDevice.DataBits = 8;
+
+ //serialDevice.ReadTimeout = 5000;
+ serialDevice.ReadTimeout = 10000;
+
+ serialDevice.NewLine = "\r\n";
+
+ serialDevice.Open();
+
+ // clear out the RX buffer
+ serialDevice.ReadExisting();
+ response = serialDevice.ReadExisting();
+ Debug.WriteLine($"RX :{response.Trim()} bytes:{response.Length}");
+ Thread.Sleep(500);
+
+ // Set the Working mode to LoRaWAN
+ serialDevice.WriteLine("at+set_config=lora:work_mode:0");
+ Thread.Sleep(5000);
+ response = serialDevice.ReadExisting();
+ response = response.Trim('\0');
+ Debug.WriteLine($"RX :{response.Trim()} bytes:{response.Length}");
+
+ // Set the Region to AS923
+ serialDevice.WriteLine("at+set_config=lora:region:AS923");
+ response = serialDevice.ReadLine();
+ Debug.WriteLine($"RX :{response.Trim()} bytes:{response.Length}");
+
+ // Set the JoinMode to ABP
+ serialDevice.WriteLine("at+set_config=lora:join_mode:1");
+ response = serialDevice.ReadLine();
+ Debug.WriteLine($"RX :{response.Trim()} bytes:{response.Length}");
+
+ // Set the device address
+ serialDevice.WriteLine($"at+set_config=lora:dev_addr:{DevAddress}");
+ response = serialDevice.ReadLine();
+ Debug.WriteLine($"RX :{response.Trim()} bytes:{response.Length}");
+
+ // Set the network session key
+ serialDevice.WriteLine($"at+set_config=lora:nwks_key:{NwksKey}");
+ response = serialDevice.ReadLine();
+ Debug.WriteLine($"RX :{response.Trim()} bytes:{response.Length}");
+
+ // Set the application session key
+ serialDevice.WriteLine($"at+set_config=lora:apps_key:{AppsKey}");
+ response = serialDevice.ReadLine();
+ Debug.WriteLine($"RX :{response.Trim()} bytes:{response.Length}");
+
+ // Set the Confirm flag
+ serialDevice.WriteLine("at+set_config=lora:confirm:0");
+ response = serialDevice.ReadLine();
+ Debug.WriteLine($"RX :{response.Trim()} bytes:{response.Length}");
+
+ // Join the network
+ serialDevice.WriteLine("at+join");
+ Thread.Sleep(10000);
+ response = serialDevice.ReadLine();
+ Debug.WriteLine($"Response :{response.Trim()} bytes:{response.Length}");
+
+ while (true)
+ {
+ // Send the BCD messages
+ serialDevice.WriteLine($"at+send=lora:{MessagePort}:{Payload}");
+ Thread.Sleep(1000);
- // calling the 'Store' method on the data writer actually sends the data
- txByteCount = outputDataWriter.Store();
- Debug.WriteLine($"TX: {txByteCount} bytes via {serialDevice.PortName}");
+ // The OK
+ //response = serialDevice.ReadLine();
+ response = serialDevice.ReadExisting();
+ Debug.WriteLine($"Response :{response.Trim()} bytes:{response.Length}");
- bytesRead = inputDataReader.Load(128);
- if (bytesRead > 0)
- {
- String response = inputDataReader.ReadString(bytesRead);
- Debug.WriteLine($"RX :{response}");
+ Thread.Sleep(20000);
}
-
- Thread.Sleep(20000);
}
}
catch (Exception ex)
diff --git a/NetworkJoinABP/packages.config b/NetworkJoinABP/packages.config
index 319620d..6ea4d13 100644
--- a/NetworkJoinABP/packages.config
+++ b/NetworkJoinABP/packages.config
@@ -2,7 +2,6 @@
+
-
-
\ No newline at end of file