各种平台GPS编程 总结

GPS是智能手机必备的功能之一,在程序中加入LBS服务器无疑会具有巨大的商业价值。
本文就对个平台下PGS简单操作作个简单的总结
1 /*************************************
2 //转载请保留作者信息
3 // author: sylar xiong jian
4 // cug@live.cn
5 // data: 2010.4.21 PM 22:01:32
6 //1 windows mobile
7 //
8 ***************************************/
9  //很简单的代码,就不用说明什么了。
10  HANDLE m_GpsMudle;
11 DCB m_GpsDCB;
12
13 /***************** 首先是初始化******************/
14 m_GpsMudle = CreateFile("COM1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL ); //打开串口
15 SetCommMask( m_GpsMudle, EV_RXCHAR|EV_BREAK|EV_ERR );
16 m_GpsDCB.DCBlength = sizeof( DCB );
17 GetCommState( m_GpsMudle, &m_GpsDCB ); //获得串口状态
18 m_GpsDCB.BaudRate = 4800; //波特率
19 m_GpsDCB.ByteSize = 8;
20 m_GpsDCB.Parity = NOPARITY;
21 m_GpsDCB.StopBits = ONESTOPBIT;
22
23 //sleep to let dcb be effect
24 Sleep(1000);
25 SetCommState( m_GpsMudle, &m_GpsDCB );//设置串口通讯信息
26 GetCommTimeouts(m_GpsMudle, &timeouts);
27 timeouts.ReadTotalTimeoutConstant = 1000;
28 timeouts.ReadTotalTimeoutMultiplier = 0;
29 timeouts.ReadIntervalTimeout = 1;
30 SetCommTimeouts(m_GpsMudle, &timeouts); //设置读取数据超时时间
31 PurgeComm(m_GpsMudle,PURGE_TXCLEAR|PURGE_RXCLEAR); //清理缓存
32
33 /***************** 设置一个线程开始读取GPS数据 ******************/
34 SetCommMask( m_GpsMudle,EV_RXCHAR|EV_CTS|EV_DSR|EV_RING);
35 while(1)
36 {
37 ReadFile( g_GpsModule.GetSafeHandle(), &bbyte, 1, &dwBytesTransferred, 0);
38 if(bbyte = '$') //表示有效的GPS信息,具体看GPS的格式,这里不说了。
39 {
40 }
41 }
42
43 /*************************************
44 // 转载请保留作者信息
45 // author: sylar xiong jian
46 // QQ:67666938 cug@live.cn
47 // data: 2010.4.21 PM 22:20 after a shower!
48 //2 Symbian OS 5TH S60
49 // refer: http://www.newlc.com/en/playing-n95?page=2
50 // 无错误处理和析构
51 ***************************************/
52 //首先解释下 symbian 的活动对象概念,非常类似于ACE和其他服务器模型,也类似于windows消息传递机制,所以非常简单。
53 //ActiveObjec symbian 里的活动对象类似于windows里的窗口,是接收消息的基本单元。所有的ActiveObject在创建后会通过CActiveScheduler::Add 把自己加入到Scheduler里面。每个ActiveObjec里有一个iStatus标识,类似于windows里面的窗口句柄。
54 //Scheduler 是一个独立线程的调度器,也就是主线程和ActiveObjec在一个线程,而调度器Scheduler单独为一个线程,Scheduler阻塞一直到有事件到来,然后根据iStatus标识调用相对于的活动对象ActiveObjec的RunL函数。
#include <lbsposition.h>
#include <lbssatellite.h>
55 
#include <lbsposition.h>
RPositionServer iPosServer; //表示定位服务器
#include <lbssatellite.h>  
56 RPositioner iPositioner; //会话
57 void CCActivePositioner::ConstructL(CFirstGpsAppView *aView)
58 {
59 iView = aView;
60
61 // Connect to the position server
62 TInt error = iPosServer.Connect( ); //连接到定位服务器
63
64 // Open subsession to the position server
65 error = iPositioner.Open(iPosServer); //获得和定位服务器的会话
66
67 // Set position requestor 告诉LBS谁需要位置服务
68 error = iPositioner.SetRequestor( CRequestor::ERequestorService,CRequestor::EFormatApplication , KRequestor );
69
70 // Set update interval to one second to receive one position data per second
71 TPositionUpdateOptions upOpt;
72 upOpt.SetUpdateInterval(TTimeIntervalMicroSeconds(1000000)); //GPS位置下载时间间隔 1秒
73 upOpt.SetUpdateTimeOut(TTimeIntervalMicroSeconds(1000000 * 15)); //超时时间 15s
74 upOpt.SetMaxUpdateAge(TTimeIntervalMicroSeconds(KMaxAge));
75
76 // Set update options
77 error = iPositioner.SetUpdateOptions( upOpt ); //应用上面的设置,每1秒下载一次GPS位置信息
78
79 // Request first position NotifyPositionUpdate
80 iPositioner.NotifyPositionUpdate(iPositionInfo,iStatus); //开始异步的获取位置信息..
81 // Set this object active
82 SetActive();
83 }
84
85
86 void CCActivePositioner::RunL()
87 {
88 switch (iStatus.Int())
89 {
90 // The fix is valid
91 case KErrNone:
92 case KPositionPartialUpdate: //异步的获得了GPS信息//如果有GPS信号了则进入这里运行,触发条件就是上面的 iPositioner.NotifyPositionUpdate(iPositionInfo,iStatus);
93 {
94 com_pool_data_t *sent = NULL;
95 TUint8 sent_num, i = 0;
96 TBuf8<COM_BUFFER_ITEM_SIZE> sent_data;
97 // get the NMEASentences and add into the list
98 iPositionGeneInfo->GetValue(EPositionFieldNMEASentences, sent_num); //
99 for (i = 0; i < sent_num; i++)
100 {
101 sent = comm_alloc_data(&com_pool_head);
102 if (sent == NULL){
103 comm_free_all_data(&com_pool_head, &g_gps_data_head);
104 sent = comm_alloc_data(&com_pool_head);
105 }
106 iPositionGeneInfo->GetValue(EPositionFieldNMEASentencesStart+i, sent_data);
107 memcpy(sent->data, sent_data.Ptr(), sent_data.Length());
108 list_add_before(&sent->list_node, &g_gps_data_head); //g_gps_data_head是一个linux链表,将sent的内容插入到这个链表中
109 }
110 break;
111 }
112 default:
113 break;
114 }
115 //再次请求GPS信息,这样就形成了一个循环。每1秒请求一次GPS信息
116 iPositioner.NotifyPositionUpdate(*iPositionGeneInfo, iStatus);
117 SetActive();
118 }

 

有点累,而且代码还不能确定,所以android和iphone平台的GPS操作,我会下回补上的。

posted @ 2010-04-22 09:35  熊健  阅读(5722)  评论(0编辑  收藏  举报