Hello,
Ive tried to communicate to an XBee Wi-Fi RF Module that is plugged into the XBee Adapter of my Cerbuino Bee using the SerialPort class.
The DeviceInfo that Ive got from the MFDeploy tool:
DeviceInfo:
HAL build info: 4.2.0.0, Copyright Oberon microsystems, Inc.
OEM Product codes (vendor, model, SKU): 255, 0, 65535
Serial Numbers (module, system):
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
Solution Build Info: 4.2.1.1, Copyright (C) GHI Electronics, LLC
AppDomains:
default, id=1
Assemblies:
mscorlib,4.2.0.0
Microsoft.SPOT.Native,4.2.0.0
Microsoft.SPOT.Hardware,4.2.0.0
Microsoft.SPOT.Graphics,4.2.0.0
Microsoft.SPOT.TinyCore,4.2.0.0
Microsoft.SPOT.Hardware.SerialPort,4.2.0.0
Microsoft.SPOT.Hardware.OneWire,4.2.0.0
Microsoft.SPOT.Hardware.Usb,4.2.0.0
Microsoft.SPOT.Hardware.PWM,4.2.0.1
Microsoft.SPOT.Net,4.2.0.0
System,4.2.0.0
Microsoft.SPOT.IO,4.2.0.0
GHI.OSHW.Hardware,4.2.1.0
Gadgeteer,2.42.0.0
GHIElectronics.Gadgeteer.FEZCerbuinoBee,1.0.2.0
System.Http,4.2.0.0
SimpleXBeeWiFiSample,1.0.0.0
Microsoft.SPOT.Net.Security,4.2.0.0
Microsoft.SPOT.Touch,4.2.0.0
System.Net.Security,4.2.0.0
System.IO,4.2.0.0
What Ive tried to do: Ive tried to query one of the configuration parameter of the XBee module. According to the user manual of the XBee module, you need follow these steps (everything ASCII encoded)
[ul]Wait 1000ms
Write +++ to the XBee module to enter the configuration mode
Wait 1000ms
The XBee module should answer with OK\r
Write ATAP to query the AP parameter
The XBee module should answer with 0\r (at its current configuration)[/ul]
The problem is, that every time I open a SerialPort and try to execute these steps, some of the responses from the XBee module are missing (or not). Ive tried to break down the problem into a simple c# sample:
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.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using System.IO.Ports;
namespace SimpleXBeeWiFiSample
{
public partial class Program
{
AutoResetEvent ar = new AutoResetEvent(false);
Queue q = new Queue();
SerialPort sp;
void ProgramStarted()
{
Debug.Print("Program started (ID: " + Thread.CurrentThread.ManagedThreadId + ")");
sp = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
sp.DataReceived += (sender, e) =>
{
Debug.Print("Data received");
int bToRead = 0;
while (bToRead != sp.BytesToRead)
{
bToRead = sp.BytesToRead;
Thread.Sleep(10);
}
Byte[] b = new byte[sp.BytesToRead];
sp.Read(b, 0, b.Length);
q.Enqueue(b);
ar.Set();
};
sp.ErrorReceived += (sender, e) =>
{
Debug.Print("Error on SerialPort");
};
Thread workerThread = new Thread(WorkerMethod);
workerThread.Start();
}
void WorkerMethod()
{
Debug.Print("Workerthread running (ID: " + Thread.CurrentThread.ManagedThreadId + ")");
ar = new AutoResetEvent(false);
byte[] commandMode = new byte[] { 43, 43, 43 }; // ASCII +++
byte[] atAP = new byte[] { 65, 84, 65, 80, 13 }; //ASCII ATAP\r
byte[] commandModeResponse = new byte[] { 79, 75, 13 }; //ASCII OK\r
byte[] atAPResponse = new byte[] { 48, 13 }; //ASCII 0\r
int counter = 1;
while (counter <= 10)
{
Debug.Print("Try #" + counter);
sp.Open();
Debug.Print("Sending +++");
Thread.Sleep(1000);
sp.Write(commandMode, 0, commandMode.Length);
Thread.Sleep(1000);
if (ar.WaitOne(10000, false))
{
byte[] b = (byte[])q.Dequeue();
if (b.Compare(commandModeResponse))
{
Debug.Print("Response to +++");
Debug.Print("Sending ATAP");
sp.Write(atAP, 0, atAP.Length);
if (ar.WaitOne(10000, false))
{
b = (byte[])q.Dequeue();
if (b.Compare(atAPResponse))
Debug.Print("Response to ATAP received");
else
Debug.Print("Unknown response to ATAP");
}
else
{
Debug.Print("No response to ATAP");
}
}
else
{
Debug.Print("Unknown response to +++");
}
}
else
{
Debug.Print("No response to +++");
}
counter++;
sp.Close();
}
}
}
static class Extensions
{
public static bool Compare(this byte[] b1, byte[] b2)
{
if (b1.Length != b2.Length)
return false;
for (int i = 0; i < b1.Length; i++)
if (b1[i] != b2[i])
return false;
return true;
}
}
}
Ive executed that sample three times and saved the Debug Output:
Program started (ID: 0)
Workerthread running (ID: 4)
Try #1
Sending +++
Data received
Response to +++
Sending ATAP
Data received
Response to ATAP received
Try #2
Sending +++
No response to +++
Try #3
Sending +++
Data received
Response to +++
Sending ATAP
No response to ATAP
Try #4
Sending +++
Data received
Response to +++
Sending ATAP
No response to ATAP
Try #5
Sending +++
Data received
Response to +++
Sending ATAP
No response to ATAP
Try #6
Sending +++
Data received
Response to +++
Sending ATAP
Data received
Response to ATAP received
Try #7
Sending +++
No response to +++
Try #8
Sending +++
Data received
Response to +++
Sending ATAP
Data received
Response to ATAP received
Try #9
Sending +++
No response to +++
Try #10
Sending +++
Data received
Response to +++
Sending ATAP
No response to ATAP
Program started (ID: 0)
Workerthread running (ID: 4)
Try #1
Sending +++
Data received
Response to +++
Sending ATAP
Data received
Response to ATAP received
Try #2
Sending +++
No response to +++
Try #3
Sending +++
Data received
Response to +++
Sending ATAP
Data received
Response to ATAP received
Try #4
Sending +++
No response to +++
Try #5
Sending +++
No response to +++
Try #6
Sending +++
Data received
Response to +++
Sending ATAP
No response to ATAP
Try #7
Sending +++
Data received
Response to +++
Sending ATAP
Data received
Response to ATAP received
Try #8
Sending +++
No response to +++
Try #9
Sending +++
Data received
Response to +++
Sending ATAP
Data received
Response to ATAP received
Try #10
Sending +++
No response to +++
Program started (ID: 0)
Workerthread running (ID: 4)
Try #1
Sending +++
Data received
Response to +++
Sending ATAP
No response to ATAP
Try #2
Sending +++
Data received
Response to +++
Sending ATAP
Data received
Response to ATAP received
Try #3
Sending +++
No response to +++
Try #4
Sending +++
Data received
Response to +++
Sending ATAP
Data received
Response to ATAP received
Try #5
Sending +++
No response to +++
Try #6
Sending +++
Data received
Response to +++
Sending ATAP
No response to ATAP
Try #7
Sending +++
No response to +++
Try #8
Sending +++
Data received
Response to +++
Sending ATAP
Data received
Response to ATAP received
Try #9
Sending +++
No response to +++
Try #10
Sending +++
No response to +++
What am I doing wrong here? Why are these results so different every time I try it?
Thank you for your help,
AlexAUT