s3c2440开发板使用gps模块

 
  1. <p>/*GPS模块</p><pre name="code" class="cpp">Gps模块引出四个管脚(1,2,3,5)分别为(Vcc,Tx,Rx,GND)只需要这四个管脚就可以了 
  2. 四个管脚接至Uart的serial port 2(挨着温度模块) 
  3. 模块接上天线(天线最好原装,使用桥梁Gps的天线发现不行) 
  4. 备注:有可能出现无法读取到Gps数据,原因可能为管脚之间已经短路*/  

#include <stdio.h>

 
 
 
  1. <p><span style="font-family: 'Courier New'; ">#include <string.h> </span></p>  
  1. <pre>  
  1. <span style="font-family:'Courier New';">    #include <stdlib.h>     
  2.     #include <fcntl.h>   //文件控制定义     
  3.     #include <unistd.h>    //Unix 标准函数定义     
  4.     #include <termios.h>  //PPSIX 终端控制定义 //结构体termios,       
  5.     #include <errno.h>      //错误号定义   
  6.     /* struct  termios{  
  7.         unsigned short c_iflag;  //输入模式标志       
  8.         unsigned short c_cflag;  //控制模式标志     
  9.         unsigned short c_lflag;  //本地模式标志     
  10.         unsigned char c_line;   //控制协议     
  11.         unsigned char c_cc[NCCS]  //控制字符 
  12.     } */  
  13.    
  14.     static int g_nErrorCode = 0;     
  15.    
  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.    
  45.     //切换至sirf协议的nmea报文(两种报文对应波特率不同)     
  46.         char *SwitchToNMEA   =  "$PSRF100,0,57600,8,1,0*XX\r\n";     
  47.         char *SwitchToNMEA4800   =  "$PSRF100,0,4800,8,1,0*XX\r\n";     
  48.         //char *pMsg;     
  49.    
  50.    
  51.     int  GPS_Info_Process(char* info)     
  52.     {     
  53.         int dwCNOMean = 0;     
  54.         dwCNOMean=*((unsigned short*)&info[23]);         
  55.         printf("CNOMean = %d\n", dwCNOMean);     
  56.         return dwCNOMean;     
  57.     }  
  58.    
  59.    
  60.     static int GPS_DataReceive(int nProtocol)     
  61.     {          
  62.    
  63.    
  64.             struct termios options;  //终端特性变量定义及初始化     
  65.             int fd, Rt, recv_len, fd_sel;     
  66.             //fd_set fd_gps, fd_sel;     
  67.             struct timeval tv;  //定义超时控制结构      
  68.             fd_set fds;   //文件描述符集合变量     
  69.             char buf[1024];     
  70.             int i, nReadIndex = 0, bHeaderOK = 0, nWriteIndex = 0;     
  71.    
  72.             static char  m_sMessage_ID46Buffer[60];     
  73.    
  74.             const char acMessge2E[5] = { 0xa0, 0xa2, 0x00, 0x33, 0x2e };  // 2E Test Mode 3/4 - Message ID 46     
  75.             long nSeconds = time(NULL);     
  76.             printf("Start Time: %d\n", nSeconds);     
  77.             long nBegin;     
  78.    
  79.             fd = open("/dev/tq2440_serial2", O_RDWR); //打开串口     
  80.             printf("Get fd: %d\n", fd);     
  81.             if (-1 == fd)  // 不能打开串口一     
  82.             {      
  83.                    perror("Can't Open Port!\n");     
  84.                    g_nErrorCode = 1;     
  85.             }     
  86.    
  87.    
  88.             if (tcgetattr ( fd, &options) == -1)   //获取当前设备方式     
  89.             {     
  90.                 printf("Cannot get GPS configuration!\n");     
  91.                 g_nErrorCode = 2;     
  92.                 return 0;     
  93.             }     
  94.             cfsetispeed(&options,B4800); //设置输入为4800Bps     
  95.             cfsetospeed(&options,B4800); //设置输出为4800Bps     
  96.             options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);      
  97.             options.c_oflag &= ~OPOST;      
  98.             if (tcsetattr ( fd, TCSANOW, &options) == -1)    //设置波特率     
  99.             {     
  100.                 printf("Cannot set GPS configuration!\n");     
  101.                 g_nErrorCode = 3;     
  102.                 return 0;     
  103.             }     
  104.    
  105.    
  106.    
  107.             tv.tv_sec = 5;       
  108.             tv.tv_usec = 0;     
  109.    
  110.             FD_ZERO(&fds);        
  111.             FD_SET(fd, &fds);       
  112.    
  113.         /*fd_sel = select(fd+1, &fds, NULL, NULL, &tv);   
  114.         if (fd_sel == 0)   
  115.         {   
  116.             printf("Select Failed!\n");   
  117.             g_nErrorCode = 4;         
  118.             return 0;               
  119.         }*/     
  120.    
  121.    
  122.    
  123.         Rt = write(fd, SwitchToSirf, strlen(SwitchToSirf));     
  124.    
  125.         if(Rt == -1)     
  126.         {        
  127.               printf("Switch to Sirf failed!\n");     
  128.               g_nErrorCode= 5 ;     
  129.               return 0;     
  130.         }     
  131.         printf("Switch OK\n");     
  132.         nBegin = time(NULL);     
  133.         while (time(NULL) - nBegin>1);     
  134.    
  135.    
  136.         Rt = write(fd, TestMode4, strlen(TestMode4));     
  137.    
  138.         if(Rt == -1)     
  139.         {        
  140.               printf("Switch test mode 4 failed!\n");     
  141.               g_nErrorCode = 6;     
  142.               return 0;     
  143.         }     
  144.         printf("Mode 4 OK\n");     
  145.         nBegin = time(NULL);     
  146.         while (time(NULL) - nBegin>1);     
  147.    
  148.         while(time(NULL) - nSeconds<35)     
  149.         {     
  150.             printf("Time: %d\n", time(NULL));     
  151.             if (FD_ISSET(fd, &fds))     
  152.             {     
  153.                 printf("Data in\n");     
  154.                 recv_len = read(fd, buf, 200);     
  155.                 printf("%s\n", buf);     
  156.                 printf("length: %d", recv_len);     
  157.                 if (recv_len > 0)     
  158.                 {    
  159.                     for (i = 0;i<recv_len;i++)     
  160.                     {     
  161.                         m_sMessage_ID46Buffer[i + nWriteIndex] = buf[i];       
  162.                     }     
  163.                     nWriteIndex += recv_len;     
  164.    
  165.                     while (!bHeaderOK && (nReadIndex != nWriteIndex))     
  166.                     {  
  167.                         if (m_sMessage_ID46Buffer[nReadIndex] == acMessge2E[nReadIndex])     
  168.                         {     
  169.                             nReadIndex++;     
  170.                             if (nReadIndex == 5)     
  171.                                 bHeaderOK = 1;     
  172.                         }     
  173.                         else {     
  174.                             for (i = 0;i<(nWriteIndex - nReadIndex + 1);i++)     
  175.                             {     
  176.                                 m_sMessage_ID46Buffer[i] = m_sMessage_ID46Buffer[nReadIndex + i];         
  177.                             }     
  178.                             nReadIndex = 0;     
  179.                             nWriteIndex = i;     
  180.                             break;     
  181.                         }  
  182.                     }  
  183.                     if (nWriteIndex - 8 >= 0x33)     
  184.                     {     
  185.                         if (m_sMessage_ID46Buffer[58] != 0xB3 || m_sMessage_ID46Buffer[57] != 0xB0)     
  186.                         {                                 
  187.                             printf("Receiving Error gps data.\n");     
  188.                             g_nErrorCode = 7;     
  189.                             break;     
  190.                         }     
  191.                         else{     
  192.                             return  GPS_Info_Process(m_sMessage_ID46Buffer) >= 43.5;     
  193.                         }     
  194.                     }     
  195.                 }     
  196.             }     
  197.             printf("No data\n");     
  198.         }     
  199.    
  200.         if(time(NULL) - nSeconds > 35) g_nErrorCode = 8; // timeout     
  201.         sleep(1);     
  202.         close(fd);     
  203.         return 0;     
  204.     }     
  205.    
  206.     int main(int argc,char *argv[])     
  207.     {     
  208.         int nProtocol = 0;     
  209.         if (argc >= 1)      
  210.         {     
  211.             if (argv[1] == "nmea") {  
  212.                 printf("Nmea Protocol\n");  
  213.                 nProtocol = 1;     
  214.             }else if (argv[1] == "sirf"){  
  215.                 printf("Sirf Protocol\n");  
  216.                 nProtocol = 0;     
  217.             }  
  218.         }     
  219.         //nProtocol = 1;  
  220.         printf("---GPS Dump Test---\n");     
  221.         //printf("---GPS Dump Test: %s ---\n", GPS_DataReceive(nProtocol) ? "SUCCEEDED" : "FAILED");     
  222.         int GetGpsInfoSuccess=0;  
  223.         while(!GetGpsInfoSuccess){  
  224.             if(GPS_DataReceive(nProtocol)){  
  225.                 GetGpsInfoSuccess=1;  
  226.                 printf("Get Gps Info Success! \n\n");  
  227.             }  
  228.             sleep(5);  
  229.             printf("Sorry, Get Gps Info Failed! \n\n\n\n\n");  
  230.         }  
  231.         return g_nErrorCode;     
  232.     }</span>  
  1. <span style="font-family:'Courier New';">  
  2.   
  3. </span>  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
s3c2440是一款由三星公司开发的嵌入式处理器,广泛应用于嵌入式系统和开发板中。以下是s3c2440开发板的一些主要功能和特点: 1. 处理器:s3c2440采用ARM920T核心,工作频率可达到400MHz。它具有较高的计算性能和低功耗特性,适合嵌入式应用。 2. 存储器:s3c2440开发板通常具有内置的Flash存储器和SDRAM,用于存储操作系统、应用程序和数据。Flash存储器可用于存储启动加载程序和操作系统映像。 3. 外设口:s3c2440开发板提供了多种外设口,以支持各种外部设备的连和扩展。这些口包括UART(串口)、USB、SPI、I2C、GPIO等,可用于连显示器、键盘、鼠标、传感器等外部设备。 4. 显示控制器:s3c2440具有强大的显示控制功能,支持LCD显示屏的驱动。它可以输出彩色图像和视频,适用于嵌入式显示应用。 5. 多媒体支持:s3c2440内置了音频编解码器和摄像头口,可用于实现音频播放、录制和视频捕捉功能。 6. 网络连s3c2440开发板通常具有以太网口,可用于实现网络连和数据传输。 7. 操作系统支持:s3c2440开发板可以运行各种操作系统,如Linux、Windows CE等。这使得开发者可以使用标准的开发工具和软件库进行应用程序开发。 请注意,s3c2440开发板的具体功能和特点可能因不同的厂商和型号而有所差异。建议您参考所使用的具体开发板的官方文档以获取更详细的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值