Hi Guys,
I have a strange problem. While I’m waiting for some parts to arrive to start building a remote controlled / autonomous feeder boat I thought I’d start writing up some code for the GPS. After sorting out a couple of problems with the buffer and serialport I have the gps spitting out the data and an xbee sending it over remotely to my computer (xbee hooked to an ardiuno). Everything works great when I’m debugging it in visual studio. However, when I unplug the board and hook it up to a battery it doesn’t do anything.
Anyone have an idea why?
Runinng on a FEZ Domino:
Program:
using System;
using System.IO.Ports;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Text;
using System.Threading;
namespace GPS_Data
{
public class Program
{
public static SerialPort xbee = new SerialPort("COM1", 115200);
public static void Main()
{
Debug.EnableGCMessages(false);
Logger.OnLog += new Logger.LogCallback(Logger_OnLog);
GPS gps = new GPS("COM2", 4800);
gps.Open();
Thread.Sleep(1000);
xbee.Open();
Thread.Sleep(1000);
while (true)
{
Thread.Sleep(1000);
}
//gps.Close();
}
static void Logger_OnLog(string Data)
{
if (xbee.IsOpen)
{
//Debug.Print(Data);
byte[] helloBytes = Encoding.UTF8.GetBytes(Data);
xbee.Write(helloBytes, 0, helloBytes.Length);
}
}
}
}
GPS Class:
using System;
using System.IO.Ports;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Text;
namespace GPS_Data
{
public class GPS
{
SerialPort serialPort = null;
SerialBuffer serialBuffer = new SerialBuffer(70);
public GPS(string Port, int BaudRate)
{
serialPort = new SerialPort(Port, BaudRate);
serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(serialPort_ErrorReceived);
}
public void Open()
{
if (serialPort != null)
serialPort.Open();
}
public void Close()
{
if (serialPort != null)
if(serialPort.IsOpen)
serialPort.Close() ;
}
byte[] buffer = new byte[10];
void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
serialBuffer.LoadSerial(serialPort);
string dataLine;
for (int i = 0; i < 100; i++)
{
while ((dataLine = serialBuffer.ReadLine()) != null)
{
Logger.Log(dataLine);
}
}
}
void serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
Logger.Log(e.EventType.ToString());
}
}
}
Logger Class:
using System;
using System.Text;
namespace GPS_Data
{
/// <summary>
/// This is a event based logger
/// and is available through out the whole project
/// </summary>
public static class Logger
{
public delegate void LogCallback(string Data);
public static event LogCallback OnLog;
public static void Log(string data)
{
if (OnLog != null)
{
OnLog(data);
}
}
}
}
SerialBuffer:
using System;
using System.IO.Ports;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Text;
namespace GPS_Data
{
public class SerialBuffer
{
private System.Text.Decoder decoder = System.Text.UTF8Encoding.UTF8.GetDecoder();
private byte[] buffer;
private int startIndex = 0;
private int endIndex = 0;
private char[] charBuffer;
public SerialBuffer(int initialSize)
{
buffer = new byte[initialSize];
charBuffer = new char[256];
}
public void LoadSerial(SerialPort port)
{
int bytesToRead = port.BytesToRead;
if (buffer.Length < endIndex + bytesToRead) // do we have enough buffer to hold this read?
{
// if not, look and see if we have enough free space at the front
if (buffer.Length - DataSize >= bytesToRead)
{
ShiftBuffer();
}
else
{
// not enough room, we'll have to make a bigger buffer
ExpandBuffer(DataSize + bytesToRead);
}
}
//Debug.Print("serial buffer load " + bytesToRead + " bytes read, " + DataSize + " buffer bytes before read");
port.Read(buffer, endIndex, bytesToRead);
endIndex += bytesToRead;
}
private void ShiftBuffer()
{
// move the data to the left, reclaiming space from the data already read out
Array.Copy(buffer, startIndex, buffer, 0, DataSize);
endIndex = DataSize;
startIndex = 0;
}
private void ExpandBuffer(int newSize)
{
byte[] newBuffer = new byte[newSize];
Array.Copy(buffer, startIndex, newBuffer, 0, DataSize);
buffer = newBuffer;
endIndex = DataSize;
startIndex = 0;
}
public byte[] Buffer
{
get
{
return buffer;
}
}
public int DataSize
{
get
{
return endIndex - startIndex;
}
}
public string ReadLine()
{
lock (buffer)
{
int lineEndPos = Array.IndexOf(buffer, '\n', startIndex, DataSize); // HACK: not looking for \r, just assuming that they'll come together
if (lineEndPos > 0)
{
int lineLength = lineEndPos - startIndex;
if (charBuffer.Length < lineLength) // do we have enough space in our char buffer?
{
charBuffer = new char[lineLength];
}
int bytesUsed, charsUsed;
bool completed;
decoder.Convert(buffer, startIndex, lineLength, charBuffer, 0, lineLength, true, out bytesUsed, out charsUsed, out completed);
string line = new string(charBuffer, 0, lineLength);
startIndex = lineEndPos + 1;
//Debug.Print("found string length " + lineLength + "; new buffer = " + startIndex + " to " + endIndex);
return line;
}
else
{
return null;
}
}
}
}
}