c#怎么c 获取图片gps 平板电脑GPS数据

>> 在 C# 中读取 GPS 装置
在 C# 中读取 GPS 装置
所属分类:
下载地址:
~_Project_READING GPS DEVICE I文件大小:724.08 kB
分享有礼! 》
请点击右侧的分享按钮,把本代码分享到各社交媒体。
通过您的分享链接访问Codeforge,每来2个新的IP,您将获得0.1 积分的奖励。
通过您的分享链接,每成功注册一个用户,该用户在Codeforge上所获得的每1个积分,您都将获得0.2 积分的分成奖励。
这篇文章将描述一个非常简单的方法,致力于与在 C# 应用程序的上下文内的 GPS 设备。这篇文章不能解决 GPS 设备的工作原理,或可以从大多数 GPS 设备 ; 输出的 NEMA 0183 字符串从搜集到的一切相反,这篇文章是为那些从 GPS 获得现在的位置和使用那点做某事有趣像秀你你在哪里,在地图上只是兴趣。任何外来的或昂贵用于这一项目 ;GPS 源是提供由我 Garmin 过程传说手持 GPS 为 100.00 元 (一个漂亮的小 GPS 但不是高的结束,以确保) 购买。因为我的笔记本电脑提供没有男性的串行端口,以便将该设备连接我需要适配器 ;为此,我选择购买贝尔金串行端口到 USB 适配器 (F5U109) 效果很好 ;用来将设备连接到计算机的电缆是随设备提供的。图 1: 从 GPS 获得目前的工作岗位。让事情比只输出现在的位置更有趣的是,我提供了手段来映射点直接到谷歌地图使用接受在填充当前的经度和纬度的设备该网站上的查询字符串。我已经发布了些类似 C# 角上的一段时间了,但没有 GPS 界面提供。够有趣的是 (但并不惊讶),如果你比较目前的设备状况,与物理地址时在谷歌地图上绘制在地图上所示,您将可能注意到,全球定位系统定位也更加准确的地理编码的物理地址。注意: 要从 GPS 设备检索目前的工作岗位,就必须要配置的设备输出的 NEMA 0183 投诉字符串。请参阅您的所有者手册,以确定如何设置与您可能使用的任何设备。图 2: 显示绘制的谷歌地图提出的立场。获取使用解决方案只包含一个 Windows 窗体项目叫做 ReadGPS,用 C# 编写 ;应用程序包含两种形式 (frmPP.cs,frmMap.cs),并且除了 Program.cs 文件中,所有需驱动应用程序的代码都包含在这两个窗体的类中。
Sponsored links
源码文件列表
温馨提示: 点击源码文件名可预览文件内容哦 ^_^
GpsMapping.doc529.00 kB 22:45
screenShot.jpg179.00 kB 02:28
ReadGPS.zip80.90 kB 22:49
(提交有效评论获得积分)
评论内容不能少于15个字,不要超出160个字。
评价成功,多谢!
下载~_Project_READING GPS DEVICE I
CodeForge积分(原CF币)全新升级,功能更强大,使用更便捷,不仅可以用来下载海量源代码马上还可兑换精美小礼品了
您的积分不足,优惠套餐快速获取 30 积分
10积分 / ¥100
30积分 / ¥200原价 ¥300 元
100积分 / ¥500原价 ¥1000 元
订单支付完成后,积分将自动加入到您的账号。以下是优惠期的人民币价格,优惠期过后将恢复美元价格。
支付宝支付宝付款
微信钱包微信付款
更多付款方式:、
您本次下载所消耗的积分将转交上传作者。
同一源码,30天内重复下载,只扣除一次积分。
鲁ICP备号-3 runtime:Elapsed:694.538ms - init:0.2;find:3.0;t:2.1;tags:0.3;related:646.6;comment:0.2; 27.69
登录 CodeForge
还没有CodeForge账号?
Switch to the English version?
^_^"呃 ...
Sorry!这位大神很神秘,未开通博客呢,请浏览一下其他的吧C__NET环境下GPSOEM板接收机数据的提取_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
C__NET环境下GPSOEM板接收机数据的提取
上传于||文档简介
&&0183数据提取
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩1页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢> 用C#读取GPS数据的基类,适用于wince操作系统。 适合自带GPS模块的PDA或智能手机的设备
用C#读取GPS数据的基类,适用于wince操作系统。 适合自带GPS模块的PDA或智能手机的设备
在多普达p800 , windows Mobile 5.0 操作系统下测试通过.using Susing System.Runtime.InteropSusing System.Tnamespace BaseStationPDA { class GPS{public string PortNpublic int BaudRpublic byte ByteSpublic byte P // 0-4=no,odd,even,mark,spacepublic byte StopB // 0,1,2 = 1, 1.5, 2public int ReadT//comm port win32 file handleprivate int hComm = -1;public bool Opened =//win32 api constantsprivate const uint GENERIC_READ = 0x;private const uint GENERIC_WRITE = 0x;private const int OPEN_EXISTING = 3;private const int INVALID_HANDLE_VALUE = -1;[StructLayout(LayoutKind.Sequential)]public struct DCB{//taken from c struct in platform sdkpublic int DCB// sizeof(DCB)public int BaudR// 指定当前波特率 current baud rate// these are the c struct bit fields, bit twiddle flag to setpublic int fB// 指定是否允许二进制模式,在windows95中必须主TRUE binary mode, no EOF checkpublic int fP// 指定是否允许奇偶校验 enable parity checkingpublic int fOutxCtsF// 指定CTS是否用于检测发送控制,当为TRUE是CTS为OFF,发送将被挂起。 CTS output flow controlpublic int fOutxDsrF// 指定CTS是否用于检测发送控制 DSR output flow controlpublic int fDtrC// DTR_CONTROL_DISABLE值将DTR置为OFF, DTR_CONTROL_ENABLE值将DTR置为ON, DTR_CONTROL_HANDSHAKE允许DTR&握手& DTR flow control typepublic int fDsrS// 当该值为TRUE时DSR为OFF时接收的字节被忽略 DSR sensitivitypublic int fTXContinueOnX // 指定当接收缓冲区已满,并且驱动程序已经发送出XoffChar字符时发送是否停止。TRUE时,在接收缓冲区接收到缓冲区已满的字节XoffLim且驱动程序已经发送出XoffChar字符中止接收字节之后,发送继续进行。 FALSE时,在接收缓冲区接收到代表缓冲区已空的字节XonChar且驱动程序已经发送出恢复发送的XonChar之后,发送继续进行。XOFF continues Txpublic int fOutX;// TRUE时,接收到XoffChar之后便停止发送接收到XonChar之后将重新开始 XON/XOFF out flow controlpublic int fInX;// TRUE时,接收缓冲区接收到代表缓冲区满的XoffLim之后,XoffChar发送出去接收缓冲区接收到代表缓冲区空的XonLim之后,XonChar发送出去 XON/XOFF in flow controlpublic int fErrorC// 该值为TRUE且fParity为TRUE时,用ErrorChar 成员指定的字符代替奇偶校验错误的接收字符 enable error replacementpublic int fN// eTRUE时,接收时去掉空(0值)字节 enable null strippingpublic int fRtsC// RTS flow control/*RTS_CONTROL_DISABLE时,RTS置为OFFRTS_CONTROL_ENABLE时, RTS置为ONRTS_CONTROL_HANDSHAKE时,当接收缓冲区小于半满时RTS为ON当接收缓冲区超过四分之三满时RTS为OFFRTS_CONTROL_TOGGLE时,当接收缓冲区仍有剩余字节时RTS为ON ,否则缺省为OFF*/public int fAbortOnE// TRUE时,有错误发生时中止读和写操作 abort on errorpublic int fDummy2;// 未使用 reservedpublic ushort wR// 未使用,必须为0 not currently usedpublic ushort XonL// 指定在XON字符发送这前接收缓冲区中可允许的最小字节数 transmit XON thresholdpublic ushort XoffL// 指定在XOFF字符发送这前接收缓冲区中可允许的最小字节数 transmit XOFF thresholdpublic byte ByteS// 指定端口当前使用的数据位 number of bits/byte, 4-8public byte P// 指定端口当前使用的奇偶校验方法,可能为:EVENPARITY,MARKPARITY,NOPARITY,ODDPARITY0-4=no,odd,even,mark,spacepublic byte StopB// 指定端口当前使用的停止位数,可能为:ONESTOPBIT,ONE5STOPBITS,TWOSTOPBITS0,1,2 = 1, 1.5, 2public char XonC// 指定用于发送和接收字符XON的值 Tx and Rx XON characterpublic char XoffC// 指定用于发送和接收字符XOFF值 Tx and Rx XOFF characterpublic char ErrorC// 本字符用来代替接收到的奇偶校验发生错误时的值 error replacement characterpublic char EofC// 当没有使用二进制模式时,本字符可用来指示数据的结束 end of input characterpublic char EvtC// 当接收到此字符时,会产生一个事件 received event characterpublic ushort wReserved1;// 未使用 do not use}[StructLayout(LayoutKind.Sequential)]private struct COMMTIMEOUTS{public int ReadIntervalTpublic int ReadTotalTimeoutMpublic int ReadTotalTimeoutCpublic int WriteTotalTimeoutMpublic int WriteTotalTimeoutC}[StructLayout(LayoutKind.Sequential)]private struct OVERLAPPED{public intIpublic intInternalHpublic intOpublic intOffsetHpublic int hE}[DllImport(&coredll.dll&)]private static extern int CreateFile(string lpFileName,// 要打开的串口名称uint dwDesiredAccess,// 指定串口的访问方式,一般设置为可读可写方式int dwShareMode,// 指定串口的共享模式,串口不能共享,所以设置为0int lpSecurityAttributes, // 设置串口的安全属性,WIN9X下不支持,应设为NULLint dwCreationDisposition,// 对于串口通信,创建方式只能为OPEN_EXISTINGint dwFlagsAndAttributes,// 指定串口属性与标志,设置为FILE_FLAG_OVERLAPPED(重叠I/O操作),指定串口以异步方式通信int hTemplateFile// 对于串口通信必须设置为NULL);[DllImport(&coredll.dll&)]private static extern bool GetCommState(int hFile,//通信设备句柄ref DCB lpDCB// 设备控制块DCB);[DllImport(&coredll.dll&)]private static extern bool BuildCommDCB(string lpDef,// 设备控制字符串ref DCB lpDCB// 设备控制块);[DllImport(&coredll.dll&)]private static extern bool SetCommState(int hFile,// 通信设备句柄ref DCB lpDCB// 设备控制块);[DllImport(&coredll.dll&)]private static extern bool GetCommTimeouts(int hFile,// 通信设备句柄 handle to comm deviceref COMMTIMEOUTS lpCommTimeouts// 超时时间 time-out values);[DllImport(&coredll.dll&)]private static extern bool SetCommTimeouts(int hFile,// 通信设备句柄 handle to comm deviceref COMMTIMEOUTS lpCommTimeouts// 超时时间 time-out values);[DllImport(&coredll.dll&)]private static extern bool ReadFile(int hFile,// 通信设备句柄 handle to filebyte[] lpBuffer,// 数据缓冲区 data bufferint nNumberOfBytesToRead,// 多少字节等待读取 number of bytes to readref int lpNumberOfBytesRead, // 读取多少字节 number of bytes readref OVERLAPPED lpOverlapped// 溢出缓冲区 overlapped buffer);[DllImport(&coredll.dll&)]private static extern bool WriteFile(int hFile,// 通信设备句柄 handle to filebyte[] lpBuffer,// 数据缓冲区 data bufferint nNumberOfBytesToWrite,// 多少字节等待写入 number of bytes to writeref int lpNumberOfBytesWritten,// 已经写入多少字节 number of bytes writtenref OVERLAPPED lpOverlapped// 溢出缓冲区 overlapped buffer);[DllImport(&coredll.dll&)]private static extern bool CloseHandle(int hObject// handle to object);[DllImport(&coredll.dll&)]private static extern uint GetLastError();public void Open(){DCB dcbCommPort = new DCB();COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();// 打开串口 OPEN THE COMM PORT.hComm = CreateFile(PortNum ,GENERIC_READ | GENERIC_WRITE,0, 0,OPEN_EXISTING,0,0);// 如果串口没有打开,就打开 IF THE PORT CANNOT BE OPENED, BAIL OUT.if(hComm == INVALID_HANDLE_VALUE){throw(new ApplicationException(&非法操作,不能打开串口!&));}// 设置通信超时时间 SET THE COMM TIMEOUTS.GetCommTimeouts(hComm,ref ctoCommPort);ctoCommPort.ReadTotalTimeoutConstant = ReadTctoCommPort.ReadTotalTimeoutMultiplier = 0;ctoCommPort.WriteTotalTimeoutMultiplier = 0;ctoCommPort.WriteTotalTimeoutConstant = 0;SetCommTimeouts(hComm,ref ctoCommPort);// 设置串口 SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.GetCommState(hComm, ref dcbCommPort);dcbCommPort.BaudRate=BaudRdcbCommPort.flags=0;//dcb.fBinary=1;dcbCommPort.flags|=1;if (Parity&0){//dcb.fParity=1dcbCommPort.flags|=2;}dcbCommPort.Parity=PdcbCommPort.ByteSize=ByteSdcbCommPort.StopBits=StopBif (!SetCommState(hComm, ref dcbCommPort)){//uint ErrorNum=GetLastError();throw(new ApplicationException(&非法操作,不能打开串口!&));}//unremark to see if setting took correctly//DCB dcbCommPort2 = new DCB();//GetCommState(hComm, ref dcbCommPort2);Opened =}public void Close(){if (hComm!=INVALID_HANDLE_VALUE){CloseHandle(hComm);}}public byte[] Read(int NumBytes){byte[] BufBbyte[] OutBBufBytes = new byte[NumBytes];if (hComm!=INVALID_HANDLE_VALUE){OVERLAPPED ovlCommPort = new OVERLAPPED();int BytesRead=0;ReadFile(hComm,BufBytes,NumBytes,ref BytesRead,ref ovlCommPort);try{OutBytes = new byte[BytesRead];Array.Copy(BufBytes,0,OutBytes,0,BytesRead);}catch{return BufB}}else{throw(new ApplicationException(&串口未打开!&));}return OutB//return BufB}public void Write(byte[] WriteBytes){if (hComm!=INVALID_HANDLE_VALUE){OVERLAPPED ovlCommPort = new OVERLAPPED();int BytesWritten = 0;WriteFile(hComm,WriteBytes,WriteBytes.Length,ref BytesWritten,ref ovlCommPort);}else{throw(new ApplicationException(&串口未打开!&));}}public string GetGPS(string strGPS,string strFind){///从GPS中读取的数据中,找出想要的数据///GPSstring原始字符串,///strFind要查找的内容,X:经度,Y:纬度,T:时间,V:速度,是数字从1开始,即以“,”分隔的位置///返回查找到指定位置的字符串string handerStr=&$GPRMC&;//GPS串头int findHander=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&:return DM2DD(ArryTmp[5]);case &Y&:return DM2DD(ArryTmp[3]);case &T&:return T2Time(ArryTmp[9],ArryTmp[1]);case &V&:return Convert.ToString(Convert.ToDouble(ArryTmp[7])* 1.852);default:return &V&;}}}catch{return &V&;}}}public string T2Time(string strDate,string strTime){string dT=&20&+strDate.Substring(4,2)+&-&+strDate.Substring(2,2)+&-&+strDate.Substring(0,2);string TT=Convert.ToString(Convert.ToInt32(strTime.Substring(0,2)))+&:&+strTime.Substring(2,2)+&:&+strTime.Substring(4,2);DateTime T=Convert.ToDateTime(dT+& &+TT);T=T.AddHours(8);return T.ToString();}public string DM2DD(string DegreeMinutes){//转换NMEA协议的“度分”格式为十进制“度度”格式string sDstring sMstring sReturn=&&;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(&.&,&&);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=sD}else if(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(&.&,&&);double sMinute1=Convert.ToDouble(DegreeMinutes.Substring(0,3));double sMinute2=Convert.ToDouble(DegreeMinutes.Substring(3,DegreeMinutes.Length-3));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=sM}return sR}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)else}catch{}} } class HexCon{// 把十六进制字符串转换成字节型和把字节型转换成十六进制字符串 converter hex string to byte and byte to hex stringpublic static string ByteToString(byte[] InBytes){string StringOut=&&;foreach (byte InByte in InBytes){StringOut=StringOut + String.Format(&{0:X2} &,InByte);}return StringO}public static byte[] StringToByte(string InString){string[] ByteSByteStrings = InString.Split(& &.ToCharArray());byte[] ByteOByteOut = new byte[ByteStrings.Length-1];for (int i = 0;i==ByteStrings.Length-1;i++){ByteOut[i] = Convert.ToByte((&0x& + ByteStrings[i]));}return ByteO} }}在别的class中调用时如Frmlogoin(是通过一个时间控件来循环的)public class Frmlogin : System.Windows.Forms.Form {private GPS ss_port=new GPS();} #region 读取GPSprivate void opengps(string ComPoint){ss_port.PortNum = ComPss_port.BaudRate = 4800;ss_port.ByteSize = 8;ss_port.Parity = 0;ss_port.StopBits = 1;ss_port.ReadTimeout = 1000;try{if (ss_port.Opened){ss_port.Close();ss_port.Open();timer1.Enabled=}else{ss_port.Open();//打开串口timer1.Enabled=}}catch{//MessageBox.Show(&读取GPS错误!& ,&系统提示&);}}private void timer1_Tick(object sender, System.EventArgs e){if (ss_port.Opened)gpsread();elsess_port.Open();//打开串口}private void gpsread(){byte[] aa=ss_port.Read(512);string gpsinfo =System.Text.Encoding.ASCII.GetString(aa,0,aa.Length);GetParam.GpsLongitude=ss_port.GetGPS(gpsinfo,&X&);GetParam.GpsLatitude=ss_port.GetGPS(gpsinfo,&Y&);GetParam.GpsSpeed=ss_port.GetGPS(gpsinfo,&V&);GetParam.GpsTime=ss_port.GetGPS(gpsinfo,&T&);if(GetParam.GpsLongitude==&-1&)GetParam.GpsState=&0&;if(GetParam.GpsLongitude==&V& && GetParam.GpsLatitude==&V&)GetParam.GpsState=&0&;if(GetParam.GpsLongitude!=&-1& && GetParam.GpsLongitude!=&V&)GetParam.GpsState=&1&;GetParam.GpsLongitude=(GetParam.GpsLongitude==&V&) ? &0& : GetParam.GpsLGetParam.GpsLatitude=(GetParam.GpsLatitude==&V&) ? &0& : GetParam.GpsLGetParam.GpsSpeed=(GetParam.GpsSpeed==&V&) ? &0& : GetParam.GpsSGetParam.GpsTime=(GetParam.GpsTime==&V&) ? &0& :GetParam.GpsT}private void GpsClose(){timer1.Enabled=if (ss_port.Opened)ss_port.Close();}
相关推荐:在多普达p800 , windows Mobile 5.0 操作系统下测试通过.using Susing System.Runtime.InteropSusing System.Tnamespace BaseStationPDA {class GPS {public string PortN public int BaudRpublic b
在多普达p800 , windows Mobile 5.0 操作系统下测试通过.? ?using?Susing?System.Runtime.InteropSusing?System.Tnamespace?BaseStationPDA?{?class?GPS...
------分隔线----------------------------
相关阅读排行2016年10月 .NET技术大版内专家分月排行榜第一2016年8月 .NET技术大版内专家分月排行榜第一2016年7月 .NET技术大版内专家分月排行榜第一
2016年12月 .NET技术大版内专家分月排行榜第二2016年9月 .NET技术大版内专家分月排行榜第二2016年6月 .NET技术大版内专家分月排行榜第二2016年3月 .NET技术大版内专家分月排行榜第二2016年1月 .NET技术大版内专家分月排行榜第二2015年12月 .NET技术大版内专家分月排行榜第二2015年2月 .NET技术大版内专家分月排行榜第二2015年1月 .NET技术大版内专家分月排行榜第二2014年11月 .NET技术大版内专家分月排行榜第二2014年5月 .NET技术大版内专家分月排行榜第二2014年4月 .NET技术大版内专家分月排行榜第二2012年2月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第二
2016年10月 .NET技术大版内专家分月排行榜第一2016年8月 .NET技术大版内专家分月排行榜第一2016年7月 .NET技术大版内专家分月排行榜第一
2016年12月 .NET技术大版内专家分月排行榜第二2016年9月 .NET技术大版内专家分月排行榜第二2016年6月 .NET技术大版内专家分月排行榜第二2016年3月 .NET技术大版内专家分月排行榜第二2016年1月 .NET技术大版内专家分月排行榜第二2015年12月 .NET技术大版内专家分月排行榜第二2015年2月 .NET技术大版内专家分月排行榜第二2015年1月 .NET技术大版内专家分月排行榜第二2014年11月 .NET技术大版内专家分月排行榜第二2014年5月 .NET技术大版内专家分月排行榜第二2014年4月 .NET技术大版内专家分月排行榜第二2012年2月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第二
2016年10月 .NET技术大版内专家分月排行榜第一2016年8月 .NET技术大版内专家分月排行榜第一2016年7月 .NET技术大版内专家分月排行榜第一
2016年12月 .NET技术大版内专家分月排行榜第二2016年9月 .NET技术大版内专家分月排行榜第二2016年6月 .NET技术大版内专家分月排行榜第二2016年3月 .NET技术大版内专家分月排行榜第二2016年1月 .NET技术大版内专家分月排行榜第二2015年12月 .NET技术大版内专家分月排行榜第二2015年2月 .NET技术大版内专家分月排行榜第二2015年1月 .NET技术大版内专家分月排行榜第二2014年11月 .NET技术大版内专家分月排行榜第二2014年5月 .NET技术大版内专家分月排行榜第二2014年4月 .NET技术大版内专家分月排行榜第二2012年2月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第二
2015年3月 总版技术专家分月排行榜第二2014年12月 总版技术专家分月排行榜第二2014年9月 总版技术专家分月排行榜第二
2015年3月 .NET技术大版内专家分月排行榜第一2015年2月 .NET技术大版内专家分月排行榜第一2015年1月 .NET技术大版内专家分月排行榜第一2014年12月 .NET技术大版内专家分月排行榜第一2014年11月 .NET技术大版内专家分月排行榜第一
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。&|&&|&&|&&&&|&&&&|&
GPS技术网络文摘精华
经飞扬科技编辑整理的GPS技术原理专辑
DGPS系统原理
GSP系统设计规则
用C#读取GPS数据的基类
这里将详细介绍如何利用用C#读取GPS数据的基类进行编程,以下为具体实例
以下文字及图片属于网络摘录,飞扬科技编辑及理理,版权所有仍由原创作人持有
-------------------------------------------------------------------------------------------------------
using System.Runtime.InteropS
namespace BaseStationPDA
public string PortN
public int BaudR
byte ByteS
public byte P // 0-4=no,odd,even,mark,space
public byte StopB // 0,1,2 = 1, 1.5, 2
public int
//comm port win32 file handle
private int hComm =
public bool Opened =
//win32 api
private const uint GENERIC_READ = 0x;
private const
uint GENERIC_WRITE = 0x;
private const int OPEN_EXISTING =
private const int INVALID_HANDLE_VALUE =
[StructLayout(LayoutKind.Sequential)]
public struct DCB
//taken from c struct in platform sdk
public int
// sizeof(DCB)
public int BaudR
指定当前波特率 current baud rate
// these are the c struct bit fields, bit
twiddle flag to set
public int fB
指定是否允许二进制模式,在windows95中必须主TRUE binary mode, no EOF check
public int
// 指定是否允许奇偶校验 enable parity checking
public int
// 指定CTS是否用于检测发送控制,当为TRUE是CTS为OFF,发送将被挂起。 CTS output flow
public int fOutxDsrF
// 指定CTS是否用于检测发送控制 DSR output flow
public int fDtrC
// DTR_CONTROL_DISABLE值将DTR置为OFF,
DTR_CONTROL_ENABLE值将DTR置为ON, DTR_CONTROL_HANDSHAKE允许DTR&握手& DTR flow control
public int fDsrS
// 当该值为TRUE时DSR为OFF时接收的字节被忽略 DSR
sensitivity
public int fTXContinueOnX //
指定当接收缓冲区已满,并且驱动程序已经发送出XoffChar字符时发送是否停止。TRUE时,在接收缓冲区接收到缓冲区已满的字节XoffLim且驱动程序已经发送出XoffChar字符中止接收字节之后,发送继续进行。 FALSE时,在接收缓冲区接收到代表缓冲区已空的字节XonChar且驱动程序已经发送出恢复发送的XonChar之后,发送继续进行。XOFF
continues Tx
public int fOutX;
TRUE时,接收到XoffChar之后便停止发送接收到XonChar之后将重新开始 XON/XOFF out flow control
public int fInX;
TRUE时,接收缓冲区接收到代表缓冲区满的XoffLim之后,XoffChar发送出去接收缓冲区接收到代表缓冲区空的XonLim之后,XonChar发送出去
XON/XOFF in flow control
public int fErrorC
该值为TRUE且fParity为TRUE时,用ErrorChar 成员指定的字符代替奇偶校验错误的接收字符 enable error replacement
public int fN
// eTRUE时,接收时去掉空(0值)字节 enable null stripping
public int fRtsC
// RTS flow control
/*RTS_CONTROL_DISABLE时,RTS置为OFF
RTS_CONTROL_ENABLE时,
RTS_CONTROL_HANDSHAKE时,
当接收缓冲区小于半满时RTS为ON
当接收缓冲区超过四分之三满时RTS为OFF
RTS_CONTROL_TOGGLE时,
当接收缓冲区仍有剩余字节时RTS为ON
,否则缺省为OFF*/
public int fAbortOnE
// TRUE时,有错误发生时中止读和写操作 abort on
public int fDummy2;
// 未使用 reserved
public ushort wR
// 未使用,必须为0 not currently
public ushort XonL
// 指定在XON字符发送这前接收缓冲区中可允许的最小字节数
transmit XON threshold
public ushort XoffL
指定在XOFF字符发送这前接收缓冲区中可允许的最小字节数 transmit XOFF threshold
public byte
// 指定端口当前使用的数据位 number of bits/byte, 4-8
public byte
指定端口当前使用的奇偶校验方法,可能为:EVENPARITY,MARKPARITY,NOPARITY,ODDPARITY
0-4=no,odd,even,mark,space
public byte StopB
指定端口当前使用的停止位数,可能为:ONESTOPBIT,ONE5STOPBITS,TWOSTOPBITS
0,1,2 = 1, 1.5, 2
public char XonC
// 指定用于发送和接收字符XON的值 Tx and Rx XON
public char XoffC
// 指定用于发送和接收字符XOFF值 Tx and Rx
XOFF character
public char ErrorC
// 本字符用来代替接收到的奇偶校验发生错误时的值
error replacement character
public char EofC
当没有使用二进制模式时,本字符可用来指示数据的结束 end of input character
public char
// 当接收到此字符时,会产生一个事件 received event character
ushort wReserved1;
// 未使用 do not use
[StructLayout(LayoutKind.Sequential)]
private struct
COMMTIMEOUTS
public int ReadIntervalT
public int
ReadTotalTimeoutM
public int ReadTotalTimeoutC
public int WriteTotalTimeoutM
public int
WriteTotalTimeoutC
[StructLayout(LayoutKind.Sequential)]
private struct
OVERLAPPED
public int
public int
public int
public int
public int
[DllImport(&coredll.dll&)]
private static
extern int CreateFile(
string lpFileName,
要打开的串口名称
uint dwDesiredAccess,
指定串口的访问方式,一般设置为可读可写方式
int dwShareMode,
指定串口的共享模式,串口不能共享,所以设置为0
int lpSecurityAttributes, //
设置串口的安全属性,WIN9X下不支持,应设为NULL
int dwCreationDisposition,
对于串口通信,创建方式只能为OPEN_EXISTING
int dwFlagsAndAttributes,
指定串口属性与标志,设置为FILE_FLAG_OVERLAPPED(重叠I/O操作),指定串口以异步方式通信
hTemplateFile
对于串口通信必须设置为NULL
[DllImport(&coredll.dll&)]
private static
extern bool GetCommState(
int hFile,
//通信设备句柄
ref DCB lpDCB
设备控制块DCB
[DllImport(&coredll.dll&)]
private static extern
bool BuildCommDCB(
string lpDef,
// 设备控制字符串
ref DCB lpDCB
设备控制块
[DllImport(&coredll.dll&)]
private static extern bool
SetCommState(
int hFile,
// 通信设备句柄
ref DCB lpDCB
设备控制块
[DllImport(&coredll.dll&)]
private static extern bool
GetCommTimeouts(
int hFile,
// 通信设备句柄 handle to comm
ref COMMTIMEOUTS lpCommTimeouts
// 超时时间 time-out
[DllImport(&coredll.dll&)]
private static extern
bool SetCommTimeouts(
int hFile,
// 通信设备句柄 handle to comm
ref COMMTIMEOUTS lpCommTimeouts
// 超时时间 time-out
[DllImport(&coredll.dll&)]
private static extern bool
int hFile,
// 通信设备句柄 handle to file
// 数据缓冲区 data buffer
int nNumberOfBytesToRead,
多少字节等待读取 number of bytes to read
ref int lpNumberOfBytesRead, // 读取多少字节
number of bytes read
ref OVERLAPPED lpOverlapped
// 溢出缓冲区 overlapped
[DllImport(&coredll.dll&)]
private static extern bool
WriteFile(
int hFile,
// 通信设备句柄 handle to
byte[] lpBuffer,
// 数据缓冲区 data buffer
nNumberOfBytesToWrite,
// 多少字节等待写入 number of bytes to write
lpNumberOfBytesWritten,
// 已经写入多少字节 number of bytes written
OVERLAPPED lpOverlapped
// 溢出缓冲区 overlapped
[DllImport(&coredll.dll&)]
private static extern bool
CloseHandle(
int hObject
// handle to
[DllImport(&coredll.dll&)]
private static extern uint
GetLastError();
public void Open()
DCB dcbCommPort
= new DCB();
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
// 打开串口 OPEN THE COMM PORT.
hComm = CreateFile(PortNum
,GENERIC_READ | GENERIC_WRITE,0, 0,OPEN_EXISTING,0,0);
// 如果串口没有打开,就打开 IF
THE PORT CANNOT BE OPENED, BAIL OUT.
if(hComm == INVALID_HANDLE_VALUE)
ApplicationException(&非法操作,不能打开串口!&));
// 设置通信超时时间 SET THE
COMM TIMEOUTS.
GetCommTimeouts(hComm,ref
ctoCommPort);
ctoCommPort.ReadTotalTimeoutConstant =
ctoCommPort.ReadTotalTimeoutMultiplier =
ctoCommPort.WriteTotalTimeoutMultiplier =
ctoCommPort.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm,ref ctoCommPort);
// 设置串口 SET BAUD
RATE, PARITY, WORD SIZE, AND STOP BITS.
GetCommState(hComm, ref
dcbCommPort);
dcbCommPort.BaudRate=BaudR
dcbCommPort.flags=0;
//dcb.fBinary=1;
dcbCommPort.flags|=1;
(Parity&0)
//dcb.fParity=1
dcbCommPort.flags|=2;
dcbCommPort.Parity=P
dcbCommPort.ByteSize=ByteS
dcbCommPort.StopBits=StopB
(!SetCommState(hComm, ref dcbCommPort))
ErrorNum=GetLastError();
ApplicationException(&非法操作,不能打开串口!&));
//unremark to see if
setting took correctly
//DCB dcbCommPort2 = new
//GetCommState(hComm, ref dcbCommPort2);
public void Close()
(hComm!=INVALID_HANDLE_VALUE)
CloseHandle(hComm);
public byte[]
Read(int NumBytes)
byte[] BufB
BufBytes = new byte[NumBytes];
(hComm!=INVALID_HANDLE_VALUE)
OVERLAPPED ovlCommPort = new
OVERLAPPED();
BytesRead=0;
ReadFile(hComm,BufBytes,NumBytes,ref BytesRead,ref
ovlCommPort);
OutBytes = new
byte[BytesRead];
Array.Copy(BufBytes,0,OutBytes,0,BytesRead);
ApplicationException(&串口未打开!&));
return BufB
public void Write(byte[]
WriteBytes)
if (hComm!=INVALID_HANDLE_VALUE)
OVERLAPPED ovlCommPort = new OVERLAPPED();
BytesWritten = 0;
WriteFile(hComm,WriteBytes,WriteBytes.Length,ref
BytesWritten,ref ovlCommPort);
ApplicationException(&串口未打开!&));
public string GetGPS(string strGPS,string
///从GPS中读取的数据中,找出想要的数据
///GPSstring原始字符串,
///strFind要查找的内容,X:经度,Y:纬度,T:时间,V:速度,是数字从1开始,即以“,”分隔的位置
///返回查找到指定位置的字符串
handerStr=&$GPRMC&;//GPS串头
findHander=strGPS.IndexOf(handerStr);//看是否含有GPS串头
(findHander&0)
strGPS=strGPS.Substring(findHander,strGPS.Length-findHander);
ArryTmp=strGPS.Split(&,&.ToCharArray());
if(ArryTmp[2]==&V&)
&V&;//没有信号
switch(strFind)
return DM2DD(ArryTmp[5]);
return DM2DD(ArryTmp[3]);
return T2Time(ArryTmp[9],ArryTmp[1]);
return Convert.ToString(Convert.ToDouble(ArryTmp[7])*
public string T2Time(string strDate,string
dT=&20&+strDate.Substring(4,2)+&-&+strDate.Substring(2,2)+&-&+strDate.Substring(0,2);
TT=Convert.ToString(Convert.ToInt32(strTime.Substring(0,2)))+&:&+strTime.Substring(2,2)+&:&+strTime.Substring(4,2);
T=Convert.ToDateTime(dT+& &+TT);
T=T.AddHours(8);
T.ToString();
public string DM2DD(string
DegreeMinutes)
//转换NMEA协议的“度分”格式为十进制“度度”格式
sReturn=&&;
if(DegreeMinutes.IndexOf(&.&)==4)
//DegreeMinutes
= Replace(DegreeMinutes, &.&, &&)
//DM2DD = CDbl(Left(DegreeMinutes, 2))
+ CDbl(Left(CStr(CDbl(Right(DegreeMinutes, Len(DegreeMinutes) - 2)) / 60), 8)) /
DegreeMinutes=DegreeMinutes.Replace(&.&,&&);
sDegree1=Convert.ToDouble(DegreeMinutes.Substring(0,2));
sDegree2=Convert.ToDouble(DegreeMinutes.Substring(2,DegreeMinutes.Length-2));
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=sD
if(DegreeMinutes.IndexOf(&.&)==5)
//DegreeMinutes =
Replace(DegreeMinutes, &.&, &&)
//DM2DD = CDbl(Left(DegreeMinutes, 2)) +
CDbl(Left(CStr(CDbl(Right(DegreeMinutes, Len(DegreeMinutes) - 2)) / 60), 8)) /
DegreeMinutes=DegreeMinutes.Replace(&.&,&&);
sMinute1=Convert.ToDouble(DegreeMinutes.Substring(0,3));
sMinute2=Convert.ToDouble(DegreeMinutes.Substring(3,DegreeMinutes.Length-2));
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=sM
public bool
ScanPort()
Open();//打开串口
bytRead=Read(512);
if(Encoding.ASCII.GetString(bytRead,0,bytRead.Length).IndexOf(&$GP&)&=0)
class HexCon
// 把十六进制字符串转换成字节型和把字节型转换成十六进制字符串
converter hex string to byte and byte to hex string
public static string
ByteToString(byte[] InBytes)
string StringOut=&&;
(byte InByte in InBytes)
StringOut=StringOut +
String.Format(&{0:X2} &,InByte);
return StringO
public static byte[] StringToByte(string InString)
string[] ByteS
ByteStrings = InString.Split(&
&.ToCharArray());
byte[] ByteO
ByteOut = new
byte[ByteStrings.Length-1];
for (int i = 0;i==ByteStrings.Length-1;i++)
ByteOut[i] = Convert.ToByte((&0x& + ByteStrings[i]));
return ByteO
在别的class中调用时如Frmlogoin(是通过一个时间控件来循环的)
public class Frmlogin :
System.Windows.Forms.Form
private GPS ss_port=new GPS();
#region 读取GPS
private void opengps(string
ss_port.PortNum = ComP
ss_port.BaudRate =
ss_port.ByteSize = 8;
ss_port.Parity =
ss_port.StopBits = 1;
ss_port.ReadTimeout = 1000;
(ss_port.Opened)
ss_port.Close();
ss_port.Open();
timer1.Enabled=
ss_port.Open();//打开串口
timer1.Enabled=
MessageBox.Show(&读取GPS错误!&
,&系统提示&);
private void timer1_Tick(object sender,
System.EventArgs e)
(ss_port.Opened)
gpsread();
ss_port.Open();//打开串口
void gpsread()
byte[] aa=ss_port.Read(512);
string gpsinfo
=System.Text.Encoding.ASCII.GetString(aa,0,aa.Length);
GetParam.GpsLongitude=ss_port.GetGPS(gpsinfo,&X&);
GetParam.GpsLatitude=ss_port.GetGPS(gpsinfo,&Y&);
GetParam.GpsSpeed=ss_port.GetGPS(gpsinfo,&V&);
GetParam.GpsTime=ss_port.GetGPS(gpsinfo,&T&);
if(GetParam.GpsLongitude==&-1&)
GetParam.GpsState=&0&;
if(GetParam.GpsLongitude==&V& &&
GetParam.GpsLatitude==&V&)
GetParam.GpsState=&0&;
if(GetParam.GpsLongitude!=&-1&
&& GetParam.GpsLongitude!=&V&)
GetParam.GpsState=&1&;
GetParam.GpsLongitude=(GetParam.GpsLongitude==&V&) ? &0& :
GetParam.GpsL
GetParam.GpsLatitude=(GetParam.GpsLatitude==&V&) ?
&0& : GetParam.GpsL
GetParam.GpsSpeed=(GetParam.GpsSpeed==&V&) ?
&0& : GetParam.GpsS
GetParam.GpsTime=(GetParam.GpsTime==&V&) ? &0&
:GetParam.GpsT
private void
GpsClose()
timer1.Enabled=
(ss_port.Opened)
ss_port.Close();
#endregion

我要回帖

更多关于 gps数据解析c程序 的文章

 

随机推荐