/*--------------------------------------------------------------------
* Radio Signal Meter: app note for the eMote .NOW
* (c) 2013 The Samraksh Company
*
* Version history
* 1.0: initial release
* 1.1: Corrected an issue with reporting on the length of the message
* 1.2: Added build profiles for on-board and long-range radios
*
* Remarks
* Choose "Long Range" or "On Board" solution configuration depending on which radio you're using.
---------------------------------------------------------------------*/
#define MODE_SEND
using System;
using System.Reflection;
using System.Threading;
using Microsoft.SPOT;
using Samraksh.eMote.DotNow;
using Samraksh.eMote.Net.Radio;
using Samraksh.eMote.Net.Mac;
using Samraksh.AppNote.Utility;
namespace Samraksh.AppNote.RadioSignal {
///
/// This program listens for radio packets and prints information about identity, sig
/// nal strength, etc.
/// It also periodically sends radio packets that another mote can listen to.
/// It can help you debug another program by "sniffing" what's coming over the radio.
///
public class Program {
static SimpleCsmaRadio _csmaRadio;
static readonly EnhancedEmoteLcd Lcd = new EnhancedEmoteLcd();
static int count_rec = 0;
const string Header = "Pkt";
const int SendDelay = 100; // ms
const RadioName TheRadioName = RadioName.RF231RADIO;
///
/// Set up the radio to listen and to send
///
public static void Main()
{
Debug.EnableGCMessages(false); // We don't want to see garbage collector messages in the Output window
// Set up LCD and display a welcome message
Lcd.Display("Strt");
try
{
//Modified by Dhrubo. Preprocessor directives to differently instantiate sender and receiver --Nov 22, 2014
#if MODE_SEND
Debug.Print("\nInitializing sender MAC...");
_csmaRadio = new SimpleCsmaRadio(TheRadioName, 140, TxPowerValue.Power_3dBm, null);
for (int i = 1; i <= 10; i++)
{
Debug.Print("\nSending 1000 packets for round " + i);
SendPackets(1000);
if (i < 10)
Thread.Sleep(10000); //Sleep while the receiver records previous round
}
#elif MODE_RECEIVE
Debug.Print("\nInitializing receiver MAC...");
_csmaRadio = new SimpleCsmaRadio(TheRadioName, 140, TxPowerValue.Power_3dBm, RadioReceive);
// Show that we've initialized and are running
Lcd.Display(count_rec);
while (true) { }
#else
#error Invalid communication mode. Valid options: MODE_SEND, MODE_RECEIVE
#endif
}
catch
{
Lcd.Display("Err");
Thread.Sleep(Timeout.Infinite);
}
}
// MODE_RECEIVE function: Handle a received message
static void RadioReceive(CSMA csma) {
if (csma.GetPendingPacketCount() < 1)
{
return;
}
var rcvMsg = csma.GetNextPacket();
if (rcvMsg == null)
{
return;
}
var rcvPayloadBytes = rcvMsg.GetMessage();
var rcvPayloadChar = System.Text.Encoding.UTF8.GetChars(rcvPayloadBytes);
try {
//Debug.Print("Received " + (rcvMsg.Unicast ? "Unicast" : "Broadcast") + " message from src: " + rcvMsg.Src + ", size: " + rcvMsg.Size + ", rssi: " + rcvMsg.RSSI + ", lqi: " + rcvMsg.LQI);
Debug.Print(" Payload: [" + new string(rcvPayloadChar) + "]");
Lcd.Display(count_rec++);
}
catch (Exception e) {
Debug.Print(e.ToString());
}
}
// MODE_SEND function: Broadcast max_bytes packets
static void SendPackets(int max_bytes)
{
int count_bytes = 0;
while (count_bytes < max_bytes)
{
try
{
// Send a probe
var toSendByte = System.Text.Encoding.UTF8.GetBytes(Header + " " + count_bytes);
_csmaRadio.Send(Addresses.BROADCAST, toSendByte);
//Debug.Print("Sent byte# " + count_bytes);
Lcd.Display(++count_bytes);
Thread.Sleep(SendDelay);
}
catch
{
Lcd.Display("8888");
Thread.Sleep(Timeout.Infinite);
}
}
Debug.Print("Sent full message of size " + max_bytes + " bytes");
}
}
}