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