Main Site Documentation

XBee 802.15.4 + FEZ Spider Gadgeteer


#1

Hi,

I’m developing a wireless sensing application for the Gadgeteer but I’m having trouble in making it wireless. I am using an FEZ Spider Mainboard, an XBee Adapter and XBees 802.15.4 radios. (I’m trying to make it work with an XBee S1 module and a 900MHz module, both 802.15.4).

My mission is to simply send a string to another radio (a base station), but I can’t get it done. I tried the default “xbee” library first to try to send data, but I couldn’t work out how it really worked for sending simple strings. Then I tried XBeeClient, a library that’s supposedly useful for this kind of “easy” applications. However, it didn’t really work. A weird message keeps appearing in the Debug Console: “Waiting for more bytes from serial port, message length indicates more bytes comming” even though I’m not trying to receive anything.

I found out later that this library, XBeeClient, was only supposed to work with XBee modules using “ZigBee”. My modules don’t have the full ZigBee stack, though. (Well, I think so. In XCTU, Digi’s software for setting up Xbees, the modules I use are not marked as “ZigBee”).

Do you know what I can do to make this simple application work, with the hardware I have?

Thank you very much!


#2

@ hodoufo - Welcome!

It’s just simple serial commands, you dont need a library.
I’ll post some code in a moo.

Justin


#3

@ Justin - Hey, thank you! I’d really appreciate that!


#4

Righto - i cheat and use @ Iggmoe’s excellent SimpleSerial library, makes life easier - http://www.tinyclr.com/codeshare/entry/644

And here is some code using it…

using System.IO.Ports;
using Microsoft.SPOT;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.Interfaces;


namespace BalanceBee
{
    public partial class Program
    {
        private SimpleSerial _xbeePort;
        private string[] _xBeeLines;

        void ProgramStarted()
        {
            GT.Socket socket = GT.Socket.GetSocket(8, false, null, "");
            _xbeePort = new SimpleSerial(socket.SerialPortName, 57600, Parity.None, 8, StopBits.One);
            _xbeePort.Open();
            _xbeePort.DataReceived += new SerialDataReceivedEventHandler(XBeePortDataReceived);
        }

        void XBeePortDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            _xBeeLines = _xbeePort.Deserialize();
            foreach (string xBeeLine in _xBeeLines)
            {
                Debug.Print(xBeeLine);
            }
        }
        private void SendXbee(string message)
        {
            _xbeePort.WriteLine(message);
        }
    }
}

#5

I think there are also a couple of community made drivers for the xbee.
here is one of them http://xbee.codeplex.com/


#6

@ Justin - Hey, thank you for the help! Now I can send data to XCTU in another computer! :slight_smile:

However, when I receive data (which is not really required for my application, so I’m not so worried), it keeps catching this exception:

#### Exception System.Exception - CLR_E_WRONG_TYPE (3) ####
#### Message: 
#### System.Text.UTF8Encoding::GetChars [IP: 0000] ####
#### GadgeteerApp1.SimpleSerial::ReadExisting [IP: 000c] ####
#### GadgeteerApp1.SimpleSerial::Deserialize [IP: 0009] ####
#### GadgeteerApp1.Program::XBeePortDataReceived [IP: 000c] ####
#### System.IO.Ports.SerialPort::DataEventHandler [IP: 0012] ####

A first chance exception of type ‘System.Exception’ occurred in mscorlib.dll

Do you know what it means? I’m using XCTU’s terminal test the setup.

Thank you!


#7

Post your full code and we’ll see if it’s obvious…


#8

@ Justin - My full code is exactly what you posted


#9

The error suggests that SimpleSerial is interpreting the incoming data as UTF-8, so if it’s falling over I would imagine the incoming data might not be UTF-8.


#10

You may try my code. Not well writing but maybe help.

namespace DistanceTestGadgeteer_SDK20130214
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.
        //GT.Interfaces.Serial serial;
        double Sendcount = 0;
        double ReceiveCount = 1;
        int timestamp = 0;
        //GT.Timer timer = new GT.Timer(500);
        GT.Timer showTimeTimer = new GT.Timer(1000);

        void ProgramStarted()
        {

            xBee.Configure(115200, GT.Interfaces.Serial.SerialParity.None,
            GT.Interfaces.Serial.SerialStopBits.One, 8);
            //serial = xBee.SerialLine;
            xBee.SerialLine.Open();
            xBee.SerialLine.DataReceived += new GT.Interfaces.Serial.DataReceivedEventHandler(serial_DataReceived);                  
            showTimeTimer.Tick += new GT.Timer.TickEventHandler(showTimeTimer_Tick);
           
            Debug.Print("Program Started");
            showTimeTimer.Start();
        }

        void showTimeTimer_Tick(GT.Timer timer)
        {
            var font = Resources.GetFont(Resources.FontResources.NinaB);
            timestamp = timestamp + 1;
            display_T35.SimpleGraphics.Clear();

            display_T35.SimpleGraphics.DisplayText("Time Stamp:" + timestamp.ToString(), font, GT.Color.White, 50, 50);
            display_T35.SimpleGraphics.DisplayText("Receive Number:" + ReceiveCount.ToString(), font, GT.Color.White, 50, 100);
            Thread.Sleep(500);
        }
 
     
        void serial_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
        {

            ReceiveCount = ReceiveCount + 1;
            var font = Resources.GetFont(Resources.FontResources.NinaB);
            int btr = xBee.SerialLine.BytesToRead;
            if (btr == 0) return;
            byte[] bytes = new byte[btr];
            byte[] idbytes = new byte[2];
            xBee.SerialLine.Read(bytes, 0, btr);
            idbytes[0] = bytes[5];
            idbytes[1] = bytes[4];
            string Text = new string(System.Text.Encoding.UTF8.GetChars(bytes));
            string id = new string(System.Text.Encoding.UTF8.GetChars(idbytes));// 

            UInt16 u = (UInt16)Microsoft.SPOT.Hardware.Utility.ExtractValueFromArray(idbytes, 0, 2);//Xbee ID
                   
            display_T35.SimpleGraphics.Clear();
            Debug.Print(u.ToString());
            //Debug.Print(Text);
            //display_T35.SimpleGraphics.DisplayText(btr.ToString(), font, GT.Color.White, 50, 200);
            // display_T35.SimpleGraphics.DisplayText("Receive Number:"+ReceiveCount.ToString(), font, GT.Color.White, 50, 100);


        }

       
       
    }
}