using System;
using System.Collections;
using System.IO.Ports;
using System.IO;
using System.Threading;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
namespace NetduinoPlusGPS
{
public class Program
{
public static void Main()
{
//create a connection to the led to show we're running
OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
//instantiate the serial port connection
SerialPort serialPort1 = new SerialPort("COM1", 57600, Parity.None, 8, StopBits.One);
serialPort1.Open();
SerialPort serialPort2 = new SerialPort("COM2", 115200, Parity.None, 8, StopBits.One);
serialPort2.Open();
while (true)
{
int GPStoRead = serialPort1.BytesToRead;
int CHRtoRead = serialPort2.BytesToRead;
//*start reading the stream
if (GPStoRead > 0)
{
//blink the LED to show we got data
led.Write(true);
//Thread.Sleep(100);
led.Write(false);
// get the waiting ++data
byte[] GPSbuffer = new byte[GPStoRead];
//serialPort1.Flush();
serialPort1.Read(GPSbuffer, 0, GPSbuffer.Length);
serialPort1.Write(GPSbuffer, 0, GPSbuffer.Length);
String GPSMessage = new String(System.Text.Encoding.UTF8.GetChars(GPSbuffer));
Debug.Print(GPSMessage);
}
if (CHRtoRead > 12)
{
int i = 0;
//blink the LED to show we got data
led.Write(true);
//Thread.Sleep(100);
led.Write(false);
// get the waiting data
byte[] CHRbuffer = new byte[CHRtoRead];
//serialPort2.Flush();
serialPort2.Read(CHRbuffer, 0, CHRbuffer.Length);
if (CHRbuffer[i] == 's')
{
//Print final result
i = i + 7;
int YawVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16);
String ShowYaw = YawVal.ToString();
double YawCalc = YawVal * 0.0109863;
String ShowYCalc = YawCalc.ToString("F2");
int PitchVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16);
String ShowPitch = PitchVal.ToString();
double PitchCalc = PitchVal * 0.0109863;
String ShowPCalc = PitchCalc.ToString("F2");
int RollVal = (int)((((CHRbuffer[i++] << 8) | CHRbuffer[i++]) << 16) >> 16);
String ShowRoll = RollVal.ToString();
double RollCalc = RollVal * 0.0109863;
String ShowRCalc = RollCalc.ToString("F2");
String CTAP = "$CTAP,R," + ShowRCalc + ",P," + ShowPCalc + ",Y," + ShowYCalc;
Debug.Print(CTAP);
byte[] CTAPArray = Encoding.UTF8.GetBytes(CTAP);
serialPort1.Write(CTAPArray, 0, CTAPArray.Length);
}
else
{
i++;
}
}
Thread.Sleep(500);
//serialPort1.Flush();
}
}
}
}