public string DM2DD(string DegreeMinutes)
{
//转换NMEA协议的“度分”格式为十进制“度度”格式
string sDegree;
string sMinute;
string sReturn="";
if(DegreeMinutes.IndexOf(".")==4)
{
//DegreeMinutes = Replace(DegreeMinutes, ".", "")
//DM2DD = CDbl(Left(DegreeMinutes, 2)) + CDbl(Left(CStr(CDbl(Right(DegreeMinutes, Len(DegreeMinutes) - 2)) / 60), 8)) / 10000
DegreeMinutes=DegreeMinutes.Replace(".","");
double sDegree1=Convert.ToDouble(DegreeMinutes.Substring(0,2));
double sDegree2=Convert.ToDouble(DegreeMinutes.Substring(2,DegreeMinutes.Length-2));
string sTmp=Convert.ToString(sDegree2/60);
sDegree2=Convert.ToDouble(sTmp.Substring(0,sTmp.Length));
sDegree2=sDegree2/10000;
sDegree=Convert.ToString(sDegree1+sDegree2);
if(sDegree.Length>11)
sDegree=sDegree.Substring(0,11);
sReturn=sDegree;
}
else if(DegreeMinutes.IndexOf(".")==5)
{
//DegreeMinutes = Replace(DegreeMinutes, ".", "")
//DM2DD = CDbl(Left(DegreeMinutes, 2)) + CDbl(Left(CStr(CDbl(Right(DegreeMinutes, Len(DegreeMinutes) - 2)) / 60), 8)) / 10000
DegreeMinutes=DegreeMinutes.Replace(".","");
double sMinute1=Convert.ToDouble(DegreeMinutes.Substring(0,3));
double sMinute2=Convert.ToDouble(DegreeMinutes.Substring(3,DegreeMinutes.Length-2));
string sTmp=Convert.ToString(sMinute2/60);
sMinute2=Convert.ToDouble(sTmp.Substring(0,sTmp.Length));
sMinute2=sMinute2/10000;
sMinute=Convert.ToString(sMinute1+sMinute2);
if(sMinute.Length>10)
sMinute=sMinute.Substring(0,10);
sReturn=sMinute;
}
return sReturn;
}
public bool ScanPort()
{
try
{
if (Opened)
{
Close();
Open();
}
else
{
Open();//打开串口
}
byte[] bytRead=Read(512);
Close();
if(Encoding.ASCII.GetString(bytRead,0,bytRead.Length).IndexOf("$GP")>=0)
return true;
else
return false;
}
catch
{
return false;
}
}
}
class HexCon
{
// 把十六进制字符串转换成字节型和把字节型转换成十六进制字符串 converter hex string to byte and byte to hex string
public static string ByteToString(byte[] InBytes)
{
string StringOut="";
foreach (byte InByte in InBytes)
{
StringOut=StringOut + String.Format("{0:X2} ",InByte);
}
return StringOut;
}
public static byte[] StringToByte(string InString)
{
string[] ByteStrings;
ByteStrings = InString.Split(" ".ToCharArray());
byte[] ByteOut;
ByteOut = new byte[ByteStrings.Length-1];
for (int i = 0;i==ByteStrings.Length-1;i++)
{
ByteOut[i] = Convert.ToByte(("0x" + ByteStrings[i]));
}
return ByteOut;
}
}
}
在别的class中调用时如Frmlogoin(是通过一个时间控件来循环的)
public class Frmlogin : System.Windows.Forms.Form
{
private GPS ss_port=new GPS();
}