RF withing range feedback

Hi,

can anyone help me with some RF communication in C#??
i need two devices to know if they are within range of each other, and when they are not.

I’m not sure how to construct code to ping devices, i have some Transmit code that can execute anywhere and a DataRecieved event, the communication works, i just need to know when in range to flip a LED. the code should check every 5 seconds or so.

You could implement a simple ping - pong algorithm in software… send a byte and expect something back…

1 Like

do you have some examples?

@ Darko - Sorry, i don’t have any code… But if you could send a byte and detect it on the receiver as a ping byte… the receiver should reply with a pong byte. The sender should receive this byte within a certain amount of time… if not you know your device is out of range

@ Darko - would it help to tell us what kind of a device you are using for RF communications?

Im using this one

OK, it is a transparent serial device. As RobvanSchelven said, determining if another device is within range is as simple as sending a byte and then receiving a byte. When the other device receives the byte it then sends the byte. If the original device receives the byte it sent, then it knows the other device is within range. You can put a receive timeout on the originating device to handle the situations where the other device is out of range.

Is your question “how to send a byte”. The answer for that is in the documentation for the SerialPort.

Ok, this is a simplified version of what I’ve got so far, it sort of works, but the LED does not stay ON all the time, its flickers seemingly randomly.

So essentially I’m sending a PING every 1 second and resending PONG, when PONG is received I light a LED, in another thread i turn of the LED every 5 seconds. The problem i think is the devices are not turned on at the same time so there’s a thread.sleep difference.

I need a better way of doing this, basically if the PONG is received every 1 second there should be no chance for PingReset to fire and turn of the LED

Anyone have suggestions??

using System;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using GHI.Hardware;
using GHI.Hardware.FEZCerb;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Toolbox.NETMF.Hardware;

    public class Program
    {
      
        static Ic74hc595 Chain = new Ic74hc595(Pin.PA5, Pin.PA7, Pin.PA4, 2);
        static SevenSegment Tens = new SevenSegment(Chain.CreateParallelOut(0), false);
        static SevenSegment Ones = new SevenSegment(Chain.CreateParallelOut(8), false);

        static Thread Wireless = new Thread(new ThreadStart(OnWirelessStart));
        static Thread Ping = new Thread(new ThreadStart(OnPingStart));
        static Thread PingReset = new Thread(new ThreadStart(OnPingResetStart));

        static OutputPort Zulu_Pd = new OutputPort(Pin.PB1, false);
        static OutputPort Zulu_Nrst = new OutputPort(Pin.PB0, false);
        static SerialPort Zulu = new SerialPort("COM2", 4800, Parity.None, 8, StopBits.One);

        public static void Main()
        {
            Initialize();
            Thread.Sleep(Timeout.Infinite);
        }

        static void Initialize()
        {
            MapDisplaySignals();
            Wireless.Start();
            Ping.Start();
            PingReset.Start();
        }

        private static void MapDisplaySignals()
        {
            Tens.ChangeSignal(0, 63);
            Ones.ChangeSignal(0, 63);
            Tens.ChangeSignal(1, 6);
            Ones.ChangeSignal(1, 6);
            Tens.ChangeSignal(2, 91);
            Ones.ChangeSignal(2, 91);
            Tens.ChangeSignal(3, 79);
            Ones.ChangeSignal(3, 79);
            Tens.ChangeSignal(4, 102);
            Ones.ChangeSignal(4, 102);
            Tens.ChangeSignal(5, 109);
            Ones.ChangeSignal(5, 109);
            Tens.ChangeSignal(6, 125);
            Ones.ChangeSignal(6, 125);
            Tens.ChangeSignal(7, 7);
            Ones.ChangeSignal(7, 7);
            Tens.ChangeSignal(8, 127);
            Ones.ChangeSignal(8, 127);
            Tens.ChangeSignal(9, 111);
            Ones.ChangeSignal(9, 111);
        }

        static void OnWirelessStart()
        {
            Zulu_Pd.Write(true);
            Zulu_Nrst.Write(true);
            Zulu.ReadTimeout = 500;
            Zulu.WriteTimeout = 500;
            Zulu.Handshake = Handshake.None;
            Zulu.Open();
            Zulu.DataReceived += new SerialDataReceivedEventHandler(Zulu_DataReceived);
        }

        static void OnPingStart()
        {
            while (true) {
                Transmit("PING");
                Thread.Sleep(1000);
            }
        }

        static void OnPingResetStart()
        {
            while (true)
            {
                // Turn off a DOT on a 7-segment
                Chain.Pins[0].Write(false);
                Thread.Sleep(5000);
            }
        }

        static void Zulu_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(100);
            if (Zulu.BytesToRead > 0)
            {
                Byte[] Bytes = new Byte[Zulu.BytesToRead];
                Zulu.Read(Bytes, 0, Bytes.Length);
                Char[] Chars = Encoding.UTF8.GetChars(Bytes, 0, Bytes.Length);
                String Command = new String(Chars);
                Debug.Print(Command);

                if (Command == "PING")
                {
                    Transmit("PONG");
                }
                if (Command == "PONG")
                {
                    // Light a DOT on a 7-segment
                    Chain.Pins[0].Write(true);
                }
              
            }
        }

        static void Transmit(string Command)
        {
            Byte[] Data = Encoding.UTF8.GetBytes(Command);
            Zulu.Write(Data, 0, Data.Length);

            Thread.Sleep(100);
        }
}
1 Like

each time a pong is received set the current time into a variable and turn led on. In another thread, every 100ms check how long it has been since a pong has been received. if it is more than 5 seconds turn led off.

1 Like

Ok, that worked ok, just need to accelerate things up, it not as performing as id like.

Thanks!