Chinaunix首页 | 论坛 | 博客
  • 博客访问: 42644
  • 博文数量: 20
  • 博客积分: 475
  • 博客等级: 下士
  • 技术积分: 215
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-04 21:13
文章分类

全部博文(20)

文章存档

2011年(20)

我的朋友
最近访客

分类: 嵌入式

2011-08-23 20:05:14

GPS模块
  1. Gps模块引出四个管脚(1,2,3,5)分别为(Vcc,Tx,Rx,GND)只需要这四个管脚就可以了
  2. 四个管脚接至Uart的serial port 2(挨着温度模块)
  3. 模块接上天线(天线最好原装,使用桥梁Gps的天线发现不行)

备注:有可能出现无法读取到Gps数据,原因可能为管脚之间已经短路 

  1. #include <stdio.h>
  2.     #include <string.h>
  3.     #include <stdlib.h>
  4.  
  5.     #include <fcntl.h> //文件控制定义
  6.     #include <unistd.h> //Unix 标准函数定义
  7.     #include <termios.h> //PPSIX 终端控制定义 //结构体termios,
  8.     #include <errno.h> //错误号定义
  9.     /* struct termios{
  10.         unsigned short c_iflag; //输入模式标志
  11.         unsigned short c_cflag; //控制模式标志
  12.         unsigned short c_lflag; //本地模式标志
  13.         unsigned char c_line; //控制协议
  14.         unsigned char c_cc[NCCS] //控制字符
  15.     } */
  16.  
  17.     static int g_nErrorCode = 0;
  18.  
  19.         char TestMode4[15] = //切换sirf至mode 4
  20.            {0xA0, 0xA2,
  21.             0x00, 0x07, // payload length
  22.             0x96, // 150,switch operation mode
  23.             0x1E ,0x54, // test mode 4
  24.             0x00, 0x14, // SvID on simulator (21..) MUST match simulator
  25.             0x00, 0x0A, // Period, tracking period 10 sec
  26.             0x01, 0x15, // checksum --> (add all payload) % 0x07ff
  27.             0xb0, 0xb3};
  28.     unsigned char SwitchToSirf[] = //切回nmea协议命令
  29.           { 0xa0, // KILL THIS BYTE
  30.            0xa2,0x00,0x18, // start sequence and payload length
  31.            0x81, // message ID dec 129, switch to NMEA
  32.            0x02, // Mode
  33.            0x01,0x01, // GGA
  34.            0x00,0x01, // GLL
  35.            0x01,0x01, // GSA
  36.            0x05,0x01, // GSV
  37.            0x01,0x01, // RMC
  38.            0x00,0x01, // VTG
  39.            0x00,0x01, // MSS
  40.            0x00,0x01, // unused
  41.            0x00,0x01, // ZDA
  42.            0x00,0x01, // unused
  43.            0x12,0xc0, // baudrate 4800
  44.            0x01,0x67, // msg checksum
  45.            0xb0,0xb3 // end sequence
  46.           };
  47.  
  48.     //切换至sirf协议的nmea报文(两种报文对应波特率不同)
  49.         char *SwitchToNMEA = "$PSRF100,0,57600,8,1,0*XX\r\n";
  50.         char *SwitchToNMEA4800 = "$PSRF100,0,4800,8,1,0*XX\r\n";
  51.         //char *pMsg;
  52.  
  53.  
  54.     int GPS_Info_Process(char* info)
  55.     {
  56.         int dwCNOMean = 0;
  57.         dwCNOMean=*((unsigned short*)&info[23]);
  58.         printf("CNOMean = %d\n", dwCNOMean);
  59.         return dwCNOMean;
  60.     }
  61.  
  62.  
  63.     static int GPS_DataReceive(int nProtocol)
  64.     {
  65.  
  66.  
  67.             struct termios options; //终端特性变量定义及初始化
  68.             int fd, Rt, recv_len, fd_sel;
  69.             //fd_set fd_gps, fd_sel;
  70.             struct timeval tv; //定义超时控制结构
  71.             fd_set fds; //文件描述符集合变量
  72.             char buf[1024];
  73.             int i, nReadIndex = 0, bHeaderOK = 0, nWriteIndex = 0;
  74.  
  75.             static char m_sMessage_ID46Buffer[60];
  76.  
  77.             const char acMessge2E[5] = { 0xa0, 0xa2, 0x00, 0x33, 0x2e }; // 2E Test Mode 3/4 - Message ID 46
  78.             long nSeconds = time(NULL);
  79.             printf("Start Time: %d\n", nSeconds);
  80.             long nBegin;
  81.  
  82.             fd = open("/dev/tq2440_serial2", O_RDWR); //打开串口
  83.             printf("Get fd: %d\n", fd);
  84.             if (-1 == fd) // 不能打开串口一
  85.             {
  86.                    perror("Can't Open Port!\n");
  87.                    g_nErrorCode = 1;
  88.             }
  89.  
  90.  
  91.             if (tcgetattr ( fd, &options) == -1) //获取当前设备方式
  92.             {
  93.                 printf("Cannot get GPS configuration!\n");
  94.                 g_nErrorCode = 2;
  95.                 return 0;
  96.             }
  97.             cfsetispeed(&options,B4800); //设置输入为4800Bps
  98.             cfsetospeed(&options,B4800); //设置输出为4800Bps
  99.             options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  100.             options.c_oflag &= ~OPOST;
  101.             if (tcsetattr ( fd, TCSANOW, &options) == -1) //设置波特率
  102.             {
  103.                 printf("Cannot set GPS configuration!\n");
  104.                 g_nErrorCode = 3;
  105.                 return 0;
  106.             }
  107.  
  108.  
  109.  
  110.             tv.tv_sec = 5;
  111.             tv.tv_usec = 0;
  112.  
  113.             FD_ZERO(&fds);
  114.             FD_SET(fd, &fds);
  115.  
  116.         /*fd_sel = select(fd+1, &fds, NULL, NULL, &tv);
  117.         if (fd_sel == 0)
  118.         {
  119.             printf("Select Failed!\n");
  120.             g_nErrorCode = 4;
  121.             return 0;
  122.         }*/
  123.  
  124.  
  125.  
  126.         Rt = write(fd, SwitchToSirf, strlen(SwitchToSirf));
  127.  
  128.         if(Rt == -1)
  129.         {
  130.               printf("Switch to Sirf failed!\n");
  131.               g_nErrorCode= 5 ;
  132.               return 0;
  133.         }
  134.         printf("Switch OK\n");
  135.         nBegin = time(NULL);
  136.         while (time(NULL) - nBegin>1);
  137.  
  138.  
  139.         Rt = write(fd, TestMode4, strlen(TestMode4));
  140.  
  141.         if(Rt == -1)
  142.         {
  143.               printf("Switch test mode 4 failed!\n");
  144.               g_nErrorCode = 6;
  145.               return 0;
  146.         }
  147.         printf("Mode 4 OK\n");
  148.         nBegin = time(NULL);
  149.         while (time(NULL) - nBegin>1);
  150.  
  151.         while(time(NULL) - nSeconds<35)
  152.         {
  153.             printf("Time: %d\n", time(NULL));
  154.             if (FD_ISSET(fd, &fds))
  155.             {
  156.                 printf("Data in\n");
  157.                 recv_len = read(fd, buf, 200);
  158.                 printf("%s\n", buf);
  159.                 printf("length: %d", recv_len);
  160.                 if (recv_len > 0)
  161.                 {
  162.                     for (i = 0;i<recv_len;i++)
  163.                     {
  164.                         m_sMessage_ID46Buffer[i + nWriteIndex] = buf[i];
  165.                     }
  166.                     nWriteIndex += recv_len;
  167.  
  168.                     while (!bHeaderOK && (nReadIndex != nWriteIndex))
  169.                     {
  170.                         if (m_sMessage_ID46Buffer[nReadIndex] == acMessge2E[nReadIndex])
  171.                         {
  172.                             nReadIndex++;
  173.                             if (nReadIndex == 5)
  174.                                 bHeaderOK = 1;
  175.                         }
  176.                         else {
  177.                             for (i = 0;i<(nWriteIndex - nReadIndex + 1);i++)
  178.                             {
  179.                                 m_sMessage_ID46Buffer[i] = m_sMessage_ID46Buffer[nReadIndex + i];
  180.                             }
  181.                             nReadIndex = 0;
  182.                             nWriteIndex = i;
  183.                             break;
  184.                         }
  185.                     }
  186.                     if (nWriteIndex - 8 >= 0x33)
  187.                     {
  188.                         if (m_sMessage_ID46Buffer[58] != 0xB3 || m_sMessage_ID46Buffer[57] != 0xB0)
  189.                         {
  190.                             printf("Receiving Error gps data.\n");
  191.                             g_nErrorCode = 7;
  192.                             break;
  193.                         }
  194.                         else{
  195.                             return GPS_Info_Process(m_sMessage_ID46Buffer) >= 43.5;
  196.                         }
  197.                     }
  198.                 }
  199.             }
  200.             printf("No data\n");
  201.         }
  202.  
  203.         if(time(NULL) - nSeconds > 35) g_nErrorCode = 8; // timeout
  204.         sleep(1);
  205.         close(fd);
  206.         return 0;
  207.     }
  208.  
  209.     int main(int argc,char *argv[])
  210.     {
  211.         int nProtocol = 0;
  212.         if (argc >= 1)
  213.         {
  214.             if (argv[1] == "nmea") {
  215.                 printf("Nmea Protocol\n");
  216.                 nProtocol = 1;
  217.             }else if (argv[1] == "sirf"){
  218.                 printf("Sirf Protocol\n");
  219.                 nProtocol = 0;
  220.             }
  221.         }
  222.         //nProtocol = 1;
  223.         printf("---GPS Dump Test---\n");
  224.         //printf("---GPS Dump Test: %s ---\n", GPS_DataReceive(nProtocol) ? "SUCCEEDED" : "FAILED");
  225.         int GetGpsInfoSuccess=0;
  226.         while(!GetGpsInfoSuccess){
  227.             if(GPS_DataReceive(nProtocol)){
  228.                 GetGpsInfoSuccess=1;
  229.                 printf("Get Gps Info Success! \n\n");
  230.             }
  231.             sleep(5);
  232.             printf("Sorry, Get Gps Info Failed! \n\n\n\n\n");
  233.         }
  234.         return g_nErrorCode;
  235.     }

阅读(767) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Socket远程执行命令并结果回传(服务端)

给主人留下些什么吧!~~