Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Commit

Permalink
Updated code for new System.IO>prts NuGets. Lots of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
KiwiBryn committed Sep 7, 2021
1 parent 849c9cb commit f6373fd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 176 deletions.
9 changes: 2 additions & 7 deletions NetworkJoinABP/03.NetworkJoinABP.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,8 @@
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="Windows.Devices.SerialCommunication, Version=1.3.4.3, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.Windows.Devices.SerialCommunication.1.3.4\lib\Windows.Devices.SerialCommunication.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="Windows.Storage.Streams, Version=1.12.2.3, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.Windows.Storage.Streams.1.12.2\lib\Windows.Storage.Streams.dll</HintPath>
<Reference Include="System.IO.Ports, Version=1.0.0.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.System.IO.Ports.1.0.0\lib\System.IO.Ports.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
Expand Down
252 changes: 85 additions & 167 deletions NetworkJoinABP/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions NetworkJoinABP/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<packages>
<package id="nanoFramework.CoreLibrary" version="1.10.5" targetFramework="netnanoframework10" />
<package id="nanoFramework.Runtime.Events" version="1.9.1" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.IO.Ports" version="1.0.0" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Text" version="1.1.1" targetFramework="netnanoframework10" />
<package id="nanoFramework.Windows.Devices.SerialCommunication" version="1.3.4" targetFramework="netnanoframework10" />
<package id="nanoFramework.Windows.Storage.Streams" version="1.12.2" targetFramework="netnanoframework10" />
</packages>

0 comments on commit f6373fd

Please sign in to comment.