I am using Xbee’s to communicate between my pc and FEZ Mini. I can effectively pass data back and forth, however in most cases I lose one piece of data. If I send “Hello” I will usually receive “ello,” or some combination of a single lost character.
I am using 9600 baudrate and I’ll post some code later after I can simplify it to necessary parts. But, it is nothing more complex than the example that is shown for the Xbee Expansion board.
Thanks for the response. Here is my code. Currently my output on the Fez Mini end is:
First pass: Hello!
Second Pass:
Hello!
eello!
llllo!
o!llo!
//Fez Mini code
using System;
using System.Threading;
using System.Text;
using System.IO.Ports;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.Net;
namespace FezMini
{
public class Program
{
public static int read_count = 0;
public static byte[] rx_data = new byte[10];
public static byte[] tx_data;
public static SerialPort xBee = null;
public static void Main()
{
xBee = new SerialPort("COM3", 9600);
xBee.DataReceived += new SerialDataReceivedEventHandler(UART_DataReceived);
xBee.Open();
Thread.Sleep(Timeout.Infinite);
}
private static void UART_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
xBee.Read(rx_data, 0, xBee.BytesToRead);
string receivedDataAsString = new string(Encoding.UTF8.GetChars(rx_data));
Debug.Print(receivedDataAsString);
}
}
}
//PC code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace CommWithXbee
{
public partial class Form1 : Form
{
public static int read_count = 0;
public static byte[] rx_data = new byte[10];
public static byte[] tx_data;
public Form1()
{
InitializeComponent();
serialPort1 = new System.IO.Ports.SerialPort("COM7", 9600);
serialPort1.Open();
}
private void button1_Click(object sender, EventArgs e)
{
byte[] helloBytes = Encoding.UTF8.GetBytes("Hello!");
serialPort1.Write(helloBytes, 0, helloBytes.Length);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
serialPort1.Close();
}
}
}
or try the code, if it produce something different.
using System;
using System.Threading;
using System.Text;
using System.IO.Ports;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
namespace MFConsoleApplication1
{
public class Program
{
public static int read_count = 0;
public static byte[] rx_data = new byte[1];
public static byte[] tx_data;
public static SerialPort UART = null;
public static void Main()
{
UART = new SerialPort("COM3", 9600);
UART.Open();
string buffString = "";
int i = 0;
//Wait for three sec. make sure that everthing is ready.
Thread.Sleep(3000);
while (true)
{
// read
read_count = UART.Read(rx_data, 0, rx_data.Length);
if (read_count != 0)
{
buffString = new String(Encoding.UTF8.GetChars(rx_data));
Debug.Print(buffString);
}
Thread.Sleep(100);
}
}
}
}
I’ve discovered that with both your sample code and my existing code that the message was being split, with the exception of the first message instance that always works.
I discovered that ‘abcde’ was being split into two messages, first ‘ab’ and then ‘cde.’ Of course in debug mode the message is never split, so I can’t determine where the problem lies. Now I’m using a byte array of 2 and everything is working perfectly. I’m not sure why or what is splinting up the message with arrays over 3 bytes.
I’ve got to do a little reading up on this, I’m likely over looking something simple.
???With the code, I was able to use XBee to talk between FEZ Domino and Arduino.
I use the Graphics LCD to display the message,
and the message show up exactly the same.
picture below.
You can not assume that you will get an entire message with one event. It is normal to have a message split into multiple events. Uurther, the number of events is unpredictable.
I dont know if this helps you, but this is the code I use on my project.
The eventhandler waits for the carriage return ‘\n signifying that there is a new command from the XBee COM port.
It has two methods Command that returns a bool if a complete command is available CommandText returns a string with the XBee text
(and the constructor COM_Xbee(string comPort, int baudrate))
using System;
using Microsoft.SPOT;
using System.Text;
using System.IO.Ports;
namespace Whirligig
{
public class COM_Xbee
{
private SerialPort XBeeHandle;
string XBee_line = "";
private bool m_Command = false;
private string m_line = "";
public bool Command
{
get
{
return m_Command;
}
set
{
m_Command = value;
}
}
public string CommandText
{
get
{
return m_line;
}
}
public void Send(string Tekst)
{
byte[] buffer = Encoding.UTF8.GetBytes(Tekst);
XBeeHandle.Write(buffer, 0, buffer.Length);
}
public COM_Xbee(string comPort, int baudrate)
{
XBeeHandle = new SerialPort(comPort, baudrate);
XBeeHandle.Open();
XBeeHandle.DataReceived += new SerialDataReceivedEventHandler(XBee_DataReceived);
}
void XBee_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (e.EventType == SerialData.Chars)
{
byte[] ReadBuffer = new byte[XBeeHandle.BytesToRead];
XBeeHandle.Read(ReadBuffer, 0, ReadBuffer.Length);
XBee_line = XBee_line + new String(Encoding.UTF8.GetChars(ReadBuffer));
if (XBee_line.IndexOf('\n') > 0)
{
m_Command = true;
m_line = XBee_line.Substring(0, XBee_line.Length - 1);
XBee_line = "";
}
}
}
}
}
Sorry to bring up this topic again. I’ve put this project away temporarily and in the mean time only updated the firmware to 4.1 and I can’t get any data through to the Fez. Code hasn’t changed and wiring hasn’t changed. The XBee expansion board lights up when I send data from another Xbee. So I believe the problem is between the expansion board and the Fez Mini. I’ve also used PWM with an led to test that the pin is working correctly.
I’ve looked over this a hundred times and can’t figure out what’s going on. Anything with 4.1 that may of changed? Any ideas?