问题描述
本人小白一只,还望各位大神谅解。拟通过笔记本的USB串口连接GPS天线读入数据,并进行解析后将需要数据存入本地数据库或者EXCEL文件,需要的内容是“经度”、“纬度”、“速度”、“时间”。最好是本地数据库。下楼是部分代码,请各位大神帮忙编写程序。谢谢了!!
解决方案
解决方案二:
privatevoidserialPort1_DataReceived(objectsender,SerialDataReceivedEventArgse){stringre;re=serialPort1.ReadExisting();}//GPS数据采集提取publicstringGetGPS(stringstrGPS,stringstrFind){///从GPS中读取的数据中,找出想要的数据///GPSstring原始字符串,///strFind要查找的内容,X:经度,Y:纬度,T:时间,V:速度,是数字从1开始,即以“,”分隔的位置///返回查找到指定位置的字符串stringhanderStr="$GPRMC";//GPS串头intfindHander=strGPS.IndexOf(handerStr);//看是否含有GPS串头if(findHander<0){return"-1";}else{strGPS=strGPS.Substring(findHander,strGPS.Length-findHander);string[]ArryTmp=strGPS.Split(",".ToCharArray());//原始字符串try{if(ArryTmp[2]=="V"){return"V";//没有信号}else{switch(strFind){case"X":returnDM2DD(ArryTmp[5]);case"Y":returnDM2DD(ArryTmp[3]);case"T":returnT2Time(ArryTmp[9],ArryTmp[1]);case"V":returnConvert.ToString(Convert.ToDouble(ArryTmp[7])*1.852);default:return"V";}}}catch{return"V";}}}publicstringT2Time(stringstrDate,stringstrTime){stringdT="20"+strDate.Substring(4,2)+"-"+strDate.Substring(2,2)+"-"+strDate.Substring(0,2);stringTT=Convert.ToString(Convert.ToInt32(strTime.Substring(0,2)))+":"+strTime.Substring(2,2)+":"+strTime.Substring(4,2);DateTimeT=Convert.ToDateTime(dT+""+TT);T=T.AddHours(8);returnT.ToString();}publicstringDM2DD(stringDegreeMinutes){//转换NMEA协议的“度分”格式为十进制“度度”格式stringsDegree;stringsMinute;stringsReturn="";if(DegreeMinutes.IndexOf(".")==4){//DegreeMinutes=Replace(DegreeMinutes,".","")//DM2DD=CDbl(Left(DegreeMinutes,2))+CDbl(Left(CStr(CDbl(Right(DegreeMinutes,Len(DegreeMinutes)-2))/60),8))/10000DegreeMinutes=DegreeMinutes.Replace(".","");doublesDegree1=Convert.ToDouble(DegreeMinutes.Substring(0,2));doublesDegree2=Convert.ToDouble(DegreeMinutes.Substring(2,DegreeMinutes.Length-2));stringsTmp=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;}elseif(DegreeMinutes.IndexOf(".")==5){//DegreeMinutes=Replace(DegreeMinutes,".","")//DM2DD=CDbl(Left(DegreeMinutes,2))+CDbl(Left(CStr(CDbl(Right(DegreeMinutes,Len(DegreeMinutes)-2))/60),8))/10000DegreeMinutes=DegreeMinutes.Replace(".","");doublesMinute1=Convert.ToDouble(DegreeMinutes.Substring(0,3));doublesMinute2=Convert.ToDouble(DegreeMinutes.Substring(3,DegreeMinutes.Length-3));stringsTmp=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;}returnsReturn;}