📄 gps.cs
字号:
comport=value;
}
}
public Serial.BaudRates BaudRate
{
set
{
baudrate=value;
}
}
public Serial.BaudRates MaxBaudRate
{
set
{
maxbaudrate=value;
}
get
{
return maxbaudrate;
}
}
public Serial.BaudRates MinBaudRate
{
set
{
minbaudrate=value;
}
get
{
return minbaudrate;
}
}
public string LastError
{
get
{
return lasterror;
}
}
public Decimal HdopMaxError
{
set
{
hdopmaxerror=value;
}
get
{
return hdopmaxerror;
}
}
public Decimal Hdop
{
get
{
return hdop;
}
}
public Decimal Vdop
{
get
{
return vdop;
}
}
public Decimal Pdop
{
get
{
return pdop;
}
}
public int SatInView
{
get
{
return nbsatinview;
}
}
public Fix_Mode FixMode
{
get
{
return fixmode;
}
}
public Fix_Indicator FixIndicator
{
get
{
return fixindicator;
}
}
public Fix_Type FixType
{
get
{
return fixtype;
}
}
public Fix_TypeMode FixTypeMode
{
get
{
return fixtypemode;
}
}
// TODO : waypoint stuff - not yet fully implemented
#region waypoint stuff - not yet fully implemented
public Position WayPoint
{
get
{
return waypoint;
}
set
{
waypoint=value;
}
}
public decimal DistanceToWayPoint
{
get
{
// HELP REQUIRED HERE - to work out distance to waypoint
calculate_waypoint();
return distancetowaypoint;
}
}
public decimal BearingToWayPoint
{
get
{
// HELP REQUIRED HERE - to work out distance to waypoint
calculate_waypoint();
return bearingtowaypoint;
}
}
public decimal CourseCorrection
{
get
{
// HELP REQUIRED HERE - to work out what course correction to apply to get to waypoint
calculate_waypoint();
return coursecorrection;
}
}
#endregion
#endregion
#region private methods
private void reset_gps_vars()
{
strreceived="";
sentencecount=0;
pos = null;
pos = new Position();
mov = null;
mov = new Movement();
satellites = null;
lasterror="";
distancetowaypoint=0;
bearingtowaypoint=0;
coursecorrection=0;
InitDistance = false;
// Position dilution of precision
pdop=50;
// Horizontal dilution of precision
hdop=50;
// Vertical dilution of precision
hdop=50;
}
// loads demo data into our nmea parser
private void load_demo_data()
{
if (!File.Exists(demofile))
{
setstate=States.Stopping;
return;
}
StreamReader demostream = File.OpenText(demofile);
string str="";
while ((str=demostream.ReadLine()) != null && state==States.Running)
{
//nmea(str);
OnDataReceived(str);
}
setstate=States.Stopped;
}
private void calculate_waypoint()
{
if (pos==null || waypoint==null) return;
distancetowaypoint = CalculateDistance(pos,waypoint,Units.Kilometers);
bearingtowaypoint = CalculateBearing(pos,waypoint);
coursecorrection = bearingtowaypoint - mov.Track;
if (coursecorrection < -180m)
coursecorrection += 360m;
if (coursecorrection > 180m)
coursecorrection -= 360m;
}
private void run()
{
setstate=States.Opening;
this.reset_gps_vars();
DataReceived+= new DataReceivedEventHandler(GPS_DataReceived);
if (!demomodeon)
{
if (this.autodiscovery)
{
if (!autodiscover())
{
setstate=States.Stopped;
return;
}
}
}
if (!demomodeon)
{
if (!isconnecteddevice())
{
OnError(null,"Com Port "+comport+" Is Not On Device","");
setstate=States.Stopped;
return;
}
if (state!=States.AutoDiscovery)
{
DetailedPortSettings portSettings = new HandshakeNone();
cp = new Port(comport,portSettings);
cp.Settings.BaudRate=this.baudrate;
cp.RThreshold=64;
cp.InputLen=1;
try
{
cp.Open();
}
catch (Exception e)
{
OnError(e,"Could Not Open Com Port "+comport,"");
cp.Dispose();
setstate=States.Stopped;
return;
}
cp.OnError+=new Port.CommErrorEvent(cp_OnError);
cp.DataReceived+=new Port.CommEvent(cp_DataReceived);
}
}
if (state==States.Opening || state==States.AutoDiscovery)
setstate=States.Running;
if (demomodeon)
{
load_demo_data();
}
else
{
while (state==States.Running)
{
Thread.Sleep(10);
}
if (cp.IsOpen) cp.Close();
cp.Dispose();
cp=null;
}
setstate=States.Stopped;
}
/// <summary>
/// this is fired when the comport receives data
/// </summary>
private void cp_DataReceived()
{
try
{
if (state!=States.Running) return;
string strret="";
string strdata="";
byte[] inputData;
while (cp.InBufferCount > 0)
{
inputData = cp.Input;
strret = Encoding.ASCII.GetString(inputData, 0,1);
if (strret=="\n") // If newline
{
strdata=this.strreceived.Substring(0,strreceived.Length-1);
//nmea(strdata);
OnDataReceived(strdata);
strdata="";
strreceived="";
if (autodiscovery==true)
{
setstate=States.AutoDiscovery;
}
}
else
strreceived+=strret;
}
}
catch(Exception ex)
{
OnError(ex,"Error in cp_DataReceived","");
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="data"></param>
private void GPS_DataReceived(object sender, string data)
{
nmea(data);
}
/// <summary>
/// processes our string of nmea data
/// </summary>
/// <param name="gps_data">string of input data</param>
/// <returns>Boolean to indicate if we were able to process string of data ok<
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -