Last active
December 8, 2015 02:57
-
-
Save eashi/1fdeb7c3e1810054d56d to your computer and use it in GitHub Desktop.
Put XBee Wifi in Command mode and send some AT commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Text; | |
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 NETMF.OpenSource.XBee.Api.Common; | |
using Gadgeteer.Modules.GHIElectronics; | |
using Gadgeteer.Modules.OpenSource; | |
using Gadgeteer.SocketInterfaces; | |
using NETMF.OpenSource.XBee.Util; | |
namespace GadgeteerApp1 | |
{ | |
public partial class Program | |
{ | |
byte[] versionCommand = new byte[] { 0x41, 0x54, 0x56, 0x52, 0x0D }; | |
byte[] ssidSetCommand = new byte[] { 0x41, 0x54, 0x49, 0x44, 0x76, 0x69, 0x72, 0x75, 0x73, 0x0D }; | |
byte[] ssidReadCommand = new byte[] { 0x41, 0x54, 0x49, 0x44, 0x0D }; | |
byte[] ipAddressModeCommand = new byte[] { 0x41, 0x54, 0x4d, 0x41, 0x0D }; | |
byte[] apiEnableStateCommand = new byte[] { 0x41, 0x54, 0x41, 0x50, 0x0D }; | |
byte[] api2EnableCommand = new byte[] { 0x41, 0x54, 0x41, 0x50, 0x32, 0x02, 0x0D }; | |
byte[] acceptChangesCommand = new byte[] { 0x41, 0x54, 0x41, 0x43, 0x0D }; | |
byte[] saveCommand = new byte[] { 0x41, 0x54, 0x57, 0x52, 0x0D }; | |
// This method is run when the mainboard is powered up or reset. | |
void ProgramStarted() | |
{ | |
//Debug.Print("Program Started"); | |
xBeeAdapter.Configure(9600/*115200*/, SerialParity.None, SerialStopBits.One, 8, HardwareFlowControl.NotRequired); | |
xBeeAdapter.Port.Open(); | |
try | |
{ | |
byte[] bytes; | |
using (var stream = new OutputStream()) | |
{ | |
stream.Write("+++"); | |
bytes = stream.ToArray(); | |
} | |
Thread.Sleep(1000); | |
xBeeAdapter.Port.Write(bytes); | |
Thread.Sleep(1200); | |
Debug.Print(Read(xBeeAdapter.Port)); | |
//xBeeAdapter.Port.Write(api2EnableCommand); | |
//Debug.Print(Read(xBeeAdapter.Port)); | |
xBeeAdapter.Port.Write(versionCommand); | |
Debug.Print(Read(xBeeAdapter.Port)); | |
xBeeAdapter.Port.Write(acceptChangesCommand); | |
Debug.Print(Read(xBeeAdapter.Port)); | |
xBeeAdapter.Port.Write(apiEnableStateCommand); | |
Debug.Print(Read(xBeeAdapter.Port)); | |
xBeeAdapter.Port.Write(ssidReadCommand); | |
Debug.Print(Read(xBeeAdapter.Port)); | |
//xBeeAdapter.Port.Write(ssidSetCommand); | |
//Debug.Print(Read(xBeeAdapter.Port)); | |
xBeeAdapter.Port.Write(ssidReadCommand); | |
Debug.Print(Read(xBeeAdapter.Port)); | |
//xBeeAdapter.Port.Write(saveCommand); | |
//Debug.Print(Read(xBeeAdapter.Port)); | |
} | |
catch (Exception ex) | |
{ | |
Debug.Print(ex.Message); | |
} | |
} | |
public string Read(Serial port) | |
{ | |
Thread.Sleep(250); //crucial for the response | |
var response = new byte[xBeeAdapter.Port.BytesToRead]; | |
if (port.BytesToRead > 0) | |
{ | |
while (port.BytesToRead > 0) | |
{ | |
xBeeAdapter.Port.Read(response, 0, xBeeAdapter.Port.BytesToRead); | |
} | |
return new string(Encoding.UTF8.GetChars(response)); | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment