Chinaunix首页 | 论坛 | 博客
  • 博客访问: 416810
  • 博文数量: 380
  • 博客积分: 75
  • 博客等级: 民兵
  • 技术积分: 1925
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-05 15:35
文章分类

全部博文(380)

文章存档

2014年(1)

2013年(2)

2012年(19)

2011年(358)

我的朋友

分类: LINUX

2011-12-07 12:40:18

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.     #include <fcntl.h> //文件控制定义
  5.     #include <unistd.h> //Unix 标准函数定义
  6.     #include <termios.h> //PPSIX 终端控制定义 //结构体termios,
  7.     #include <errno.h> //错误号定义
  8.     /* struct termios{
  9.         unsigned short c_iflag; //输入模式标志
  10.         unsigned short c_cflag; //控制模式标志
  11.         unsigned short c_lflag; //本地模式标志
  12.         unsigned char c_line; //控制协议
  13.         unsigned char c_cc[NCCS] //控制字符
  14.     } */

  15.     static int g_nErrorCode = 0;

  16.         char TestMode4[15] = //切换sirf至mode 4
  17.            {0xA0, 0xA2,
  18.             0x00, 0x07, // payload length
  19.             0x96, // 150,switch operation mode
  20.             0x1E ,0x54, // test mode 4
  21.             0x00, 0x14, // SvID on simulator (21..) MUST match simulator
  22.             0x00, 0x0A, // Period, tracking period 10 sec
  23.             0x01, 0x15, // checksum --> (add all payload) % 0x07ff
  24.             0xb0, 0xb3};
  25.     unsigned char SwitchToSirf[] = //切回nmea协议命令
  26.           { 0xa0, // KILL THIS BYTE
  27.            0xa2,0x00,0x18, // start sequence and payload length
  28.            0x81, // message ID dec 129, switch to NMEA
  29.            0x02, // Mode
  30.            0x01,0x01, // GGA
  31.            0x00,0x01, // GLL
  32.            0x01,0x01, // GSA
  33.            0x05,0x01, // GSV
  34.            0x01,0x01, // RMC
  35.            0x00,0x01, // VTG
  36.            0x00,0x01, // MSS
  37.            0x00,0x01, // unused
  38.            0x00,0x01, // ZDA
  39.            0x00,0x01, // unused
  40.            0x12,0xc0, // baudrate 4800
  41.            0x01,0x67, // msg checksum
  42.            0xb0,0xb3 // end sequence
  43.           };

  44.     //切换至sirf协议的nmea报文(两种报文对应波特率不同)
  45.         char *SwitchToNMEA = "$PSRF100,0,57600,8,1,0*XX\r\n";
  46.         char *SwitchToNMEA4800 = "$PSRF100,0,4800,8,1,0*XX\r\n";
  47.         //char *pMsg;


  48.     int GPS_Info_Process(char* info)
  49.     {
  50.         int dwCNOMean = 0;
  51.         dwCNOMean=*((unsigned short*)&info[23]);
  52.         printf("CNOMean = %d\n", dwCNOMean);
  53.         return dwCNOMean;
  54.     }


  55.     static int GPS_DataReceive(int nProtocol)
  56.     {


  57.             struct termios options; //终端特性变量定义及初始化
  58.             int fd, Rt, recv_len, fd_sel;
  59.             //fd_set fd_gps, fd_sel;
  60.             struct timeval tv; //定义超时控制结构
  61.             fd_set fds; //文件描述符集合变量
  62.             char buf[1024];
  63.             int i, nReadIndex = 0, bHeaderOK = 0, nWriteIndex = 0;

  64.             static char m_sMessage_ID46Buffer[60];

  65.             const char acMessge2E[5] = { 0xa0, 0xa2, 0x00, 0x33, 0x2e }; // 2E Test Mode 3/4 - Message ID 46
  66.             long nSeconds = time(NULL);
  67.             printf("Start Time: %d\n", nSeconds);
  68.             long nBegin;

  69.             fd = open("/dev/tq2440_serial2", O_RDWR); //打开串口
  70.             printf("Get fd: %d\n", fd);
  71.             if (-1 == fd) // 不能打开串口一
  72.             {
  73.                    perror("Can't Open Port!\n");
  74.                    g_nErrorCode = 1;
  75.             }


  76.             if (tcgetattr ( fd, &options) == -1) //获取当前设备方式
  77.             {
  78.                 printf("Cannot get GPS configuration!\n");
  79.                 g_nErrorCode = 2;
  80.                 return 0;
  81.             }
  82.             cfsetispeed(&options,B4800); //设置输入为4800Bps
  83.             cfsetospeed(&options,B4800); //设置输出为4800Bps
  84.             options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  85.             options.c_oflag &= ~OPOST;
  86.             if (tcsetattr ( fd, TCSANOW, &options) == -1) //设置波特率
  87.             {
  88.                 printf("Cannot set GPS configuration!\n");
  89.                 g_nErrorCode = 3;
  90.                 return 0;
  91.             }



  92.             tv.tv_sec = 5;
  93.             tv.tv_usec = 0;

  94.             FD_ZERO(&fds);
  95.             FD_SET(fd, &fds);

  96.         /*fd_sel = select(fd+1, &fds, NULL, NULL, &tv);
  97.         if (fd_sel == 0)
  98.         {
  99.             printf("Select Failed!\n");
  100.             g_nErrorCode = 4;
  101.             return 0;
  102.         }*/



  103.         Rt = write(fd, SwitchToSirf, strlen(SwitchToSirf));

  104.         if(Rt == -1)
  105.         {
  106.               printf("Switch to Sirf failed!\n");
  107.               g_nErrorCode= 5 ;
  108.               return 0;
  109.         }
  110.         printf("Switch OK\n");
  111.         nBegin = time(NULL);
  112.         while (time(NULL) - nBegin>1);


  113.         Rt = write(fd, TestMode4, strlen(TestMode4));

  114.         if(Rt == -1)
  115.         {
  116.               printf("Switch test mode 4 failed!\n");
  117.               g_nErrorCode = 6;
  118.               return 0;
  119.         }
  120.         printf("Mode 4 OK\n");
  121.         nBegin = time(NULL);
  122.         while (time(NULL) - nBegin>1);

  123.         while(time(NULL) - nSeconds<35)
  124.         {
  125.             printf("Time: %d\n", time(NULL));
  126.             if (FD_ISSET(fd, &fds))
  127.             {
  128.                 printf("Data in\n");
  129.                 recv_len = read(fd, buf, 200);
  130.                 printf("%s\n", buf);
  131.                 printf("length: %d", recv_len);
  132.                 if (recv_len > 0)
  133.                 {
  134.                     for (i = 0;i<recv_len;i++)
  135.                     {
  136.                         m_sMessage_ID46Buffer[i + nWriteIndex] = buf[i];
  137.                     }
  138.                     nWriteIndex += recv_len;

  139.                     while (!bHeaderOK && (nReadIndex != nWriteIndex))
  140.                     {
  141.                         if (m_sMessage_ID46Buffer[nReadIndex] == acMessge2E[nReadIndex])
  142.                         {
  143.                             nReadIndex++;
  144.                             if (nReadIndex == 5)
  145.                                 bHeaderOK = 1;
  146.                         }
  147.                         else {
  148.                             for (i = 0;i<(nWriteIndex - nReadIndex + 1);i++)
  149.                             {
  150.                                 m_sMessage_ID46Buffer[i] = m_sMessage_ID46Buffer[nReadIndex + i];
  151.                             }
  152.                             nReadIndex = 0;
  153.                             nWriteIndex = i;
  154.                             break;
  155.                         }
  156.                     }
  157.                     if (nWriteIndex - 8 >= 0x33)
  158.                     {
  159.                         if (m_sMessage_ID46Buffer[58] != 0xB3 || m_sMessage_ID46Buffer[57] != 0xB0)
  160.                         {
  161.                             printf("Receiving Error gps data.\n");
  162.                             g_nErrorCode = 7;
  163.                             break;
  164.                         }
  165.                         else{
  166.                             return GPS_Info_Process(m_sMessage_ID46Buffer) >= 43.5;
  167.                         }
  168.                     }
  169.                 }
  170.             }
  171.             printf("No data\n");
  172.         }

  173.         if(time(NULL) - nSeconds > 35) g_nErrorCode = 8; // timeout
  174.         sleep(1);
  175.         close(fd);
  176.         return 0;
  177.     }

  178.     int main(int argc,char *argv[])
  179.     {
  180.         int nProtocol = 0;
  181.         if (argc >= 1)
  182.         {
  183.             if (argv[1] == "nmea") {
  184.                 printf("Nmea Protocol\n");
  185.                 nProtocol = 1;
  186.             }else if (argv[1] == "sirf"){
  187.                 printf("Sirf Protocol\n");
  188.                 nProtocol = 0;
  189.             }
  190.         }
  191.         //nProtocol = 1;
  192.         printf("---GPS Dump Test---\n");
  193.         //printf("---GPS Dump Test: %s ---\n", GPS_DataReceive(nProtocol) ? "SUCCEEDED" : "FAILED");
  194.         int GetGpsInfoSuccess=0;
  195.         while(!GetGpsInfoSuccess){
  196.             if(GPS_DataReceive(nProtocol)){
  197.                 GetGpsInfoSuccess=1;
  198.                 printf("Get Gps Info Success! \n\n");
  199.             }
  200.             sleep(5);
  201.             printf("Sorry, Get Gps Info Failed! \n\n\n\n\n");
  202.         }
  203.         return g_nErrorCode;
  204.     }

阅读(1015) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~