using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace VirtualGPS
{
public class GPS
{
private System.Windows.Forms.Timer timer; //定时器, 模拟GPS时发送,周期间隔1S
private System.IO.Ports.SerialPort serialPort; //串口
private ArrayList buf = new ArrayList();
public GPS(string portName, int baudRate)
{
timer = new System.Windows.Forms.Timer();
this.timer.Tick += new System.EventHandler(this.timer_Tick);
serialPort=new System.IO.Ports.SerialPort(portName,baudRate,System.IO.Ports.Parity.None);
timer.Interval = 1000; //GPS 发送周期
DefData = new gpsEN("0", "0");
Sdata = DefData;//待发送数据指向 缺省的数据
}
public bool StartGPS()
{
//启用VirtualGPS
try
{
serialPort.Open();
timer.Enabled = true; //启用定时器
return true;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("连接状态 :Error" + ex.Message);
return false;
}
}
public bool StopGPS()
{
//关闭VirtualGPS
try
{
serialPort.Close();
timer.Enabled = false; //关闭定时器
return true;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("连接状态 :Error" + ex.Message);
return false;
}
}
private gpsEN DefData; //缺省的GPS数据
private gpsEN Sdata; //发送的GPS数据
//GPS结构体
private struct gpsEN
{
public gpsEN(string E, string N)
{
this.E = E;
this.N = N;
this.GpsData = GetGpsData(E, N);
}
public void copy(gpsEN g)
{
this.E = g.E;
this.N = g.N;
this.GpsData = g.GpsData;
}
public string E;
public string N;
public string GpsData;
};
private void timer_Tick(object sender, EventArgs e)
{
//GPS数据定时发送
//serialPort.Write(Encoding.ASCII.GetBytes(string.Format("AT+CMGR={0}/r", n)), 0, Encoding.ASCII.GetBytes(string.Format("AT+CMGR={0}/r", n)).Length);
if (buf.Count > 0)
{
// gpsEN g = (gpsEN)buf[0];
// Sdata.E = g.E;
// Sdata.N = g.N;
// Sdata.GpsData = g.GpsData;
Sdata.copy((gpsEN)buf[0]);
buf.RemoveAt(0);
}
//else
//{
serialPort.WriteLine(Sdata.GpsData); //发送上一次的数据,GPS首次启动就发送默认的数据
//}
Console.WriteLine("E="+Sdata.E + " N=" + Sdata.N + " GPS=" + Sdata.GpsData);
}
/// <summary>
/// 向VirtualGPS 写入数据(E经度 N纬度)
/// </summary>
/// <param name="E">经度</param>
/// <param name="N">纬度</param>
public void WriteGpsEN(string E, string N)
{
gpsEN g = new gpsEN(E, N);
buf.Add(g); //写入GPS数据进入缓冲区
}
/// <summary>
/// 根据经纬度信息,转换经纬度信息为地图所需经纬度信息
/// </summary>
/// <param name="placeInfo">palaceInfo可为经度,或者是纬度值</param>
/// <returns>返回一个地图gps数据</returns>
private static decimal convertJWD(decimal placeInfo)
{
decimal tt;
int bb = (int)placeInfo;
decimal m = placeInfo - bb;//--取出整数
tt = bb * 100 + m * 60;
return tt;
}
/// <summary>
/// 获取gps数据
/// </summary>
/// <param name="E">经度</param>
/// <param name="N">纬度</param>
/// <returns>返回一个gps数据包</returns>
public static string GetGpsData(string E, string N)
{
string strGpsInfo = null;
decimal e;
decimal n;
e = convertJWD(decimal.Parse(E));
n = convertJWD(decimal.Parse(N));
strGpsInfo = "$GPGGA,095013.000," + e + ",N," + n + ",E,1,07,04.7,00064.4,M,07.2,M,,*6E/n";
strGpsInfo += "$GPGSA,A,2,14,30,22,,05,06,18,16,,,,,04.8,04.7,*19/n";
strGpsInfo += "$GPGSV,2,1,08,14,67,025,75,30,53,051,75,22,43,182,79,25,45,295,*7A/n";
strGpsInfo += "$GPGSV,2,2,08,05,17,042,75,06,22,118,65,18,12,162,64,16,10,210,75*71/n";
strGpsInfo += "$GPRMC,095013.000,A," + e + ",N," + n + ",E,0.00,0.00,150807,004.3,W*73";
return strGpsInfo;
}
}
}