Main Site Documentation

Wireless (RN-XV) without a Wireless Library (Access Mode - point to point)


#1

Modules configured as described here http://www.rescuerobot.org/drupal/content/wireless-without-wireless-library-wifly-part-2
Client code:


// https://www.ghielectronics.com/docs/15/uart 
// Must add reference for Microsoft.SPOT.Hardware.SerialPort

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

using System.IO.Ports;
using System.Text;

namespace SerialClient
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            Debug.Print("Program Started");
            display_N7.SimpleGraphics.DisplayText("Started", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Yellow, 100, 100);

            GT.Timer t = new GT.Timer(1000);
            t.Tick += new GT.Timer.TickEventHandler(t_Tick);
            t.Start();
        }

        void t_Tick(GT.Timer timer)
        {
            timer.Stop();
            try
            {
                string COMPort = GT.Socket.GetSocket(4, true, null, null).SerialPortName;
                System.IO.Ports.SerialPort UART = new SerialPort(COMPort, 115200);
                int read_count = 0;
                byte[] tx_data = new byte[16];
                byte[] rx_data = new byte[16];
                UTF8Encoding encoding = new UTF8Encoding();
                UART.ReadTimeout = 0;
                int counter = 1;
                int len = 0;
                uint linenum = 0;
                bool started = false;
                UART.Open();
                while (true)
                {
                    do
                    {
                        // for every packet received send a real packet or a acknowledgement packet
                        for (int i = 0; i < 16; i++)
                            tx_data[i] = 0x00;
                        // create a string
                        string counter_string = "Client No. " + counter.ToString();
                        len = counter_string.Length;
                        for (int i = len; i < 16; i++)
                            counter_string = counter_string + "#";
                        // convert the string to bytes
                        tx_data = encoding.GetBytes(counter_string);
                        // flush all data
                        //UART.Flush();
                        UART.DiscardOutBuffer();
                        // send some data
                        UART.Write(tx_data, 0, 16);
                        // wait to make sure data is transmitted
                        if (counter == 1)
                            Thread.Sleep(2000);
                        else
                            Thread.Sleep(100);
                    } while (UART.BytesToRead == 0 && started == false);
                    started = true;
                    // increment the counter;
                    counter++;
                    do
                    {
                        read_count = UART.Read(rx_data, 0, rx_data.Length);
                        if (read_count > 0)
                        {
                            string str = new string(Encoding.UTF8.GetChars(rx_data));
                            if (str.CompareTo("*OPEN*") == 0 || str.CompareTo("*HELLO*") == 0)
                                str = "Connection made with server.";
                            if (str.CompareTo("*CLOS*") == 0 || str.CompareTo("*CLOS**CLOS*") == 0)
                                str = "Server closed connection.";
                            if (++linenum > 11)
                            {
                                linenum = 1;
                                display_N7.SimpleGraphics.Clear();
                            }
                            display_N7.SimpleGraphics.DisplayText(str, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Yellow, 100, (linenum * 40));
                            Debug.Print(str + " " + read_count.ToString());
                        }
                        else
                        {
                            Debug.Print("No data received.");
                            Thread.Sleep(2000);
                        }
                        Thread.Sleep(100);
                    } while (read_count <= 0);
                }
            }
            catch (Exception e)
            {
                Debug.Print(e.ToString());
                Debug.Print(e.Message.ToString());
            }

            Debug.Print("\nClosing...");
            Thread.Sleep(200);
        }
    }
}

Server code:


// https://www.ghielectronics.com/docs/15/uart 
// Must add reference for Microsoft.SPOT.Hardware.SerialPort

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

using System.IO.Ports;
using System.Text;

namespace SerialServer
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            Debug.Print("Program Started");
            display_TE35.SimpleGraphics.DisplayText("Started", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Yellow, 100, 100);

            GT.Timer t = new GT.Timer(1000);
            t.Tick += new GT.Timer.TickEventHandler(t_Tick);
            t.Start();
        }
        void t_Tick(GT.Timer timer)
        {
            timer.Stop();
            try
            {
                string COMPort = GT.Socket.GetSocket(8, true, null, null).SerialPortName;
                System.IO.Ports.SerialPort UART = new SerialPort(COMPort, 115200);
                int read_count = 0;
                byte[] tx_data = new byte[16];
                byte[] rx_data = new byte[16];
                UTF8Encoding encoding = new UTF8Encoding();
                UART.ReadTimeout = 0;
                int counter = 1;
                int len = 0;
                uint linenum = 0;
                UART.Open();
                while (true)
                {
                    do
                    {
                        read_count = UART.Read(rx_data, 0, rx_data.Length);
                        if (read_count > 0)
                        {
                            string str = new string(Encoding.UTF8.GetChars(rx_data));
                            if (str.CompareTo("*CLOS*") == 0 || str.CompareTo("*CLOS**CLOS*") == 0)
                                str = "Client closing.";
                            if (str.CompareTo("*HELLO*") == 0 || str.CompareTo("*OPEN*") == 0)
                                str = "Client has made connection.";
                            if (++linenum > 10)
                            {
                                linenum = 1;
                                display_TE35.SimpleGraphics.Clear();
                            }
                            display_TE35.SimpleGraphics.DisplayText(str, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Yellow, 100, (linenum * 20));
                            Debug.Print(str + " " + read_count.ToString());
                        }
                        else
                        {
                            Debug.Print("No data received.");
                            Thread.Sleep(2000);
                        }
                        Thread.Sleep(100);
                    } while (read_count <= 0);
                    // for every packet received send a real packet or a acknowledgement packet
                    for (int i = 0; i < 16; i++)
                        tx_data[i] = 0x00;
                    // create a string
                    string counter_string = "Server No. " + counter.ToString();
                    len = counter_string.Length;
                    for (int i = len; i < 16; i++)
                        counter_string = counter_string + "#";
                    // convert the string to bytes
                    tx_data = encoding.GetBytes(counter_string);
                    // increment the counter;
                    counter++;
                    // flush all data
                    //UART.Flush();
                    UART.DiscardOutBuffer();
                    // send some data
                    UART.Write(tx_data, 0, 16);
                    // wait to make sure data is transmitted
                    Thread.Sleep(100);
                }
            }
            catch (Exception e)
            {
                Debug.Print(e.ToString());
                Debug.Print(e.Message.ToString());
            }

            Debug.Print("\nClosing...");
            Thread.Sleep(200);
        }
    }
}

In both cases of client and server (FEZ Raptor and Corba2) when I reset the board the wifi module is not reset at the same time. Can I link up the reset on the wifi module with the reset on the processor board?
Many thanks,
Kevin


#2

You could connect reset of wifi to any gpio and reset it just after startup from code.


#3

i agree, using a gpio os the best idea, control its state yourself.


#4

Some more detail:
The FEZ Raptor has a RN-XV plus Sparkfun base connected to Socket 4 type K reset pin number 3 and Raptor pin PA27. When external power is applied the RN-XV is instantly available and waiting to connect. The main board and screen initialise and the program runs.
The Cobra 2 has a “Wifi RN171 Module” connected to Socket 8 (on the optional extension board) type K reset pin number 3 and Cobra pin “P0.10 / COM3 TXD”. When the USB debugging plug is connected wifi starts instantly and the main board starts and the screen initialises. If the external power is applied (no USB plug connected) the wifi starts instantly and the screen remains white and does not initialise. If I then press the reset button the main board boots and the screen initialises normally.
The wifi modules function independently of the main boards and will associate and connect to one another instantly. Resetting the main board has no effect on the wifi modules.
If either main board fails or wifi modules lose their connection then the wifi modules need to be reset as well as the main boards.
Is their any known problem with the power unit in Cobra 2?
Because of the rapid automatic connection made by the wifi modules start up is not as easy as I had hoped. In the Cobra 2 using a software reset at start up is not good for the wifi modules. At present I tend to start up the client (Raptor) first and then the Server (Cobra). I apply the external power to the client (Raptor) then count to ten then plug in the USB debugging plug for the server (Cobra) and I have the perfect start with data flowing both ways. However out in the field I cannot plug in a USB plug!
Any thoughts?
Kevin.