Simple Serial Xbee class for .NET

using System;
using Microsoft.SPOT;
using System.IO.Ports;
using System.Text;

class Xbee
{
public static int write_count = 0;

public static byte[] tx_data;//array for sending data
//public static byte[] rx_data = new byte[10];//array for receiving data, not used here

public static SerialPort UART = null;//defines serial port

//constructor
public Xbee()
{
UART = new SerialPort("COM2", 57600);//initialize serial port
UART.Open();//open serial port
}

//public methods

public bool Send(string message)//sends string values
{
//sent successful flag
bool success = false;

try
{
//clear the serial data
UART.Flush();

//get the bytes of the message to be sent
tx_data = Encoding.UTF8.GetBytes(message);

//send the bytes
UART.Write(tx_data, 0, tx_data.Length);

//set the output flag as successful
success = true;
}
catch (Exception ex)
{
//if there is an error, sent it to the debug window (if you are connecting from visual studio)
Debug.Print("ERROR: " + message + "/r" + ex.Message);
}

//record how many strings you have sent
write_count++;

//return flag
return success;
}

public bool SendByteValues(int x, int y)//sends a byte pair (needed for headtracking)
{
bool success = false;

try
{
//clear data
UART.Flush();

//gets a byte array to send
tx_data = new byte[] { (byte)x, (byte)y };

//sends byte array
UART.Write(tx_data, 0, tx_data.Length);

//flag as successful
success = true;
}
catch (Exception ex)
{
//output errors to visual studio debug window
Debug.Print("ERROR: " + ex.Message);

}

//record how many byte pairs you have sent
write_count++;

//return success flag
return success;
}

}

9 Responses to Simple Serial Xbee class for .NET

  1. Swarrio says:

    I get an error on Microsoft.SPOT. I read on google i need hardware.dll but can’t find it anywhere on the internet 😦

    Can anyone help me with this please? Do I need to install something?

  2. Swarrio, references must be added for any unusual “using” statements at the top of your project and can be added by right clicking on the “references” node in the project you are in, and go to “add reference…”. Microsoft.SPOT is in the .NET tab, but I am not really sure if it is needed (depending on what else your project is using) so you may get away with just commenting out that “using Microsoft.SPOT;” line. I think I had that left in this class file for some other stuff I was doing. I would test, but am installing VS 2013 right now and wont be able to open it up for at least an hour or two. Let me know if that works. Connect with me on g+ at:
    http://google.com/+jameswolf
    if you are still having issues.

  3. you should have Microsoft.SPOT installed if you installed the .NET microframework to use with the netduinos

  4. Swarrio says:

    Hey cwolf,

    Thanks for your quick reply.
    I removed the Microsoft.SPOT line and I think everything Will Work but receiving z’n error on UART.Flush in code (blue underlined). Do I need to clear the serial data before refilling/resending it?

    For you second reply I have to figure it out. I’ll look on the internet for the .Net microframework 🙂

    Thanks alot already for this info

    Kind rgds,
    Swarrio

  5. Swarrio says:

    Sorry I speak dutch and my Phone replaced ‘an error’ to ‘z’n error’. The error is that he cant find the reference 😉 so I think the class Microsoft.SPOT is taking care of this?

  6. no thats a method on the SerialPort class, what are you actually trying to do? what the code running on?

  7. make sure you have the SerialPort reference added

  8. Swarrio says:

    I just want to test to send data over a serial port so I can send data using my Xbee but I got z’n error on the Microsoft.SPOT and the UART.Flush.

    I’m using visual studio 2012 and made a c Sharp project so I could copy/paste the code in a class(.cs) but then when i started debugging the program stops before I see the form because i get errors that he cant find references for the .flush method

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s