2024年5月6日发(作者:)
using System;
using c;
using ;
using ;
using ing;
using ;
using ization;
namespace GolfAssistant
{
public class SerialPortGps
{
bool stop = false;
string portName = "COM9";
int baudRate = 111520;
SerialPort sp;
GpsMonitorForm. form;
/*GPS数据*/
public string longitude="0";//经度
public string latitude = "0";//纬度
public string altitude = "0";//海拔
public string altitudeUnit = "";//海拔单位
public string usedSatellite = "0";//应用卫星数
public string locatedSatellite = "0";//定位的卫星总数
public string signalSatellite = "0";//天空中收到讯号的卫星总数
public string totalSatellite = "0";//天空中卫星总数
public bool located = false;
public SerialPortGps(GpsMonitorForm. form)
{
. = form;
}
public void open()
{
if (null != sp)
{
("GPS未停止");
}
sp = new SerialPort();
me = portName;
te = baudRate;
meout = 500;
imeout = 500;
Thread readThread = new Thread(read);
();
stop = false;
();
}
public void close()
{
stop = true;
}
public void read()
{
while (!stop)
{
try
{
string message = ne();
message = ing(0, -
);
if (With("$GPGGA"))
{
//模拟定位
//message =
"$GPGGA,012440.00,2232.458380,N,11356.91024,E,1,05,2.7,40.2,M,0.5,M,,*6F";
//message =
"$GPGGA,012440.00,2232.301654,N,11402.058366,E,1,05,2.7,40.2,M,0.5,M,,*6F";
parseGPGGA(message);
}
else if (With("$GPGSV"))
{
parseGPGSV(message);
}
(new MethodInvoker(a), message);
}
catch (TimeoutException) { }
}
();
sp = null;
("GPS已停止");
}
public static string toDecimalDegree(string s)
{
int idx = f(".");
string d1 = ing(0,idx-2);
string d2 = ing(idx - 2);
double degree = (d1) + (d2) / 60;
return ng("F8", antCulture);
}
delegate void MethodInvoker(string message);
private void parseGPGGA(string message)
{
char[] separator = { ','};
string[] data = (separator);
if (data[6].Equals("1"))
{
latitude=toDecimalDegree(data[2]);//纬度
longitude=toDecimalDegree(data[4]);//经度
altitude=data[9]+data[10];//海拔
altitudeUnit = data[10];//海拔单位
usedSatellite = data[7];//应用卫星数
located = true;
}
else
{
located = false;
}
}
private void parseGPGSV(string message)
{
char[] separator = { ',' };
string[] data = (separator);
locatedSatellite = data[2];//定位的卫星总数
signalSatellite = data[1];//天空中收到讯号的卫星总数
totalSatellite = data[3];//天空中卫星总数
}
private void smoothGPGGA()
{
}
}
}
发布评论