GPS模块
- Gps模块引出四个管脚(1,2,3,5)分别为(Vcc,Tx,Rx,GND)只需要这四个管脚就可以了
- 四个管脚接至Uart的serial port 2(挨着温度模块)
- 模块接上天线(天线最好原装,使用桥梁Gps的天线发现不行)
备注:有可能出现无法读取到Gps数据,原因可能为管脚之间已经短路
- #include <stdio.h>
-
#include <string.h>
-
#include <stdlib.h>
-
-
#include <fcntl.h> //文件控制定义
-
#include <unistd.h> //Unix 标准函数定义
-
#include <termios.h> //PPSIX 终端控制定义 //结构体termios,
-
#include <errno.h> //错误号定义
-
/* struct termios{
-
unsigned short c_iflag; //输入模式标志
-
unsigned short c_cflag; //控制模式标志
-
unsigned short c_lflag; //本地模式标志
-
unsigned char c_line; //控制协议
-
unsigned char c_cc[NCCS] //控制字符
-
} */
-
-
static int g_nErrorCode = 0;
-
-
char TestMode4[15] = //切换sirf至mode 4
-
{0xA0, 0xA2,
-
0x00, 0x07, // payload length
-
0x96, // 150,switch operation mode
-
0x1E ,0x54, // test mode 4
-
0x00, 0x14, // SvID on simulator (21..) MUST match simulator
-
0x00, 0x0A, // Period, tracking period 10 sec
-
0x01, 0x15, // checksum --> (add all payload) % 0x07ff
-
0xb0, 0xb3};
-
unsigned char SwitchToSirf[] = //切回nmea协议命令
-
{ 0xa0, // KILL THIS BYTE
-
0xa2,0x00,0x18, // start sequence and payload length
-
0x81, // message ID dec 129, switch to NMEA
-
0x02, // Mode
-
0x01,0x01, // GGA
-
0x00,0x01, // GLL
-
0x01,0x01, // GSA
-
0x05,0x01, // GSV
-
0x01,0x01, // RMC
-
0x00,0x01, // VTG
-
0x00,0x01, // MSS
-
0x00,0x01, // unused
-
0x00,0x01, // ZDA
-
0x00,0x01, // unused
-
0x12,0xc0, // baudrate 4800
-
0x01,0x67, // msg checksum
-
0xb0,0xb3 // end sequence
-
};
-
-
//切换至sirf协议的nmea报文(两种报文对应波特率不同)
-
char *SwitchToNMEA = "$PSRF100,0,57600,8,1,0*XX\r\n";
-
char *SwitchToNMEA4800 = "$PSRF100,0,4800,8,1,0*XX\r\n";
-
//char *pMsg;
-
-
-
int GPS_Info_Process(char* info)
-
{
-
int dwCNOMean = 0;
-
dwCNOMean=*((unsigned short*)&info[23]);
-
printf("CNOMean = %d\n", dwCNOMean);
-
return dwCNOMean;
-
}
-
-
-
static int GPS_DataReceive(int nProtocol)
-
{
-
-
-
struct termios options; //终端特性变量定义及初始化
-
int fd, Rt, recv_len, fd_sel;
-
//fd_set fd_gps, fd_sel;
-
struct timeval tv; //定义超时控制结构
-
fd_set fds; //文件描述符集合变量
-
char buf[1024];
-
int i, nReadIndex = 0, bHeaderOK = 0, nWriteIndex = 0;
-
-
static char m_sMessage_ID46Buffer[60];
-
-
const char acMessge2E[5] = { 0xa0, 0xa2, 0x00, 0x33, 0x2e }; // 2E Test Mode 3/4 - Message ID 46
-
long nSeconds = time(NULL);
-
printf("Start Time: %d\n", nSeconds);
-
long nBegin;
-
-
fd = open("/dev/tq2440_serial2", O_RDWR); //打开串口
-
printf("Get fd: %d\n", fd);
-
if (-1 == fd) // 不能打开串口一
-
{
-
perror("Can't Open Port!\n");
-
g_nErrorCode = 1;
-
}
-
-
-
if (tcgetattr ( fd, &options) == -1) //获取当前设备方式
-
{
-
printf("Cannot get GPS configuration!\n");
-
g_nErrorCode = 2;
-
return 0;
-
}
-
cfsetispeed(&options,B4800); //设置输入为4800Bps
-
cfsetospeed(&options,B4800); //设置输出为4800Bps
-
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
-
options.c_oflag &= ~OPOST;
-
if (tcsetattr ( fd, TCSANOW, &options) == -1) //设置波特率
-
{
-
printf("Cannot set GPS configuration!\n");
-
g_nErrorCode = 3;
-
return 0;
-
}
-
-
-
-
tv.tv_sec = 5;
-
tv.tv_usec = 0;
-
-
FD_ZERO(&fds);
-
FD_SET(fd, &fds);
-
-
/*fd_sel = select(fd+1, &fds, NULL, NULL, &tv);
-
if (fd_sel == 0)
-
{
-
printf("Select Failed!\n");
-
g_nErrorCode = 4;
-
return 0;
-
}*/
-
-
-
-
Rt = write(fd, SwitchToSirf, strlen(SwitchToSirf));
-
-
if(Rt == -1)
-
{
-
printf("Switch to Sirf failed!\n");
-
g_nErrorCode= 5 ;
-
return 0;
-
}
-
printf("Switch OK\n");
-
nBegin = time(NULL);
-
while (time(NULL) - nBegin>1);
-
-
-
Rt = write(fd, TestMode4, strlen(TestMode4));
-
-
if(Rt == -1)
-
{
-
printf("Switch test mode 4 failed!\n");
-
g_nErrorCode = 6;
-
return 0;
-
}
-
printf("Mode 4 OK\n");
-
nBegin = time(NULL);
-
while (time(NULL) - nBegin>1);
-
-
while(time(NULL) - nSeconds<35)
-
{
-
printf("Time: %d\n", time(NULL));
-
if (FD_ISSET(fd, &fds))
-
{
-
printf("Data in\n");
-
recv_len = read(fd, buf, 200);
-
printf("%s\n", buf);
-
printf("length: %d", recv_len);
-
if (recv_len > 0)
-
{
-
for (i = 0;i<recv_len;i++)
-
{
-
m_sMessage_ID46Buffer[i + nWriteIndex] = buf[i];
-
}
-
nWriteIndex += recv_len;
-
-
while (!bHeaderOK && (nReadIndex != nWriteIndex))
-
{
-
if (m_sMessage_ID46Buffer[nReadIndex] == acMessge2E[nReadIndex])
-
{
-
nReadIndex++;
-
if (nReadIndex == 5)
-
bHeaderOK = 1;
-
}
-
else {
-
for (i = 0;i<(nWriteIndex - nReadIndex + 1);i++)
-
{
-
m_sMessage_ID46Buffer[i] = m_sMessage_ID46Buffer[nReadIndex + i];
-
}
-
nReadIndex = 0;
-
nWriteIndex = i;
-
break;
-
}
-
}
-
if (nWriteIndex - 8 >= 0x33)
-
{
-
if (m_sMessage_ID46Buffer[58] != 0xB3 || m_sMessage_ID46Buffer[57] != 0xB0)
-
{
-
printf("Receiving Error gps data.\n");
-
g_nErrorCode = 7;
-
break;
-
}
-
else{
-
return GPS_Info_Process(m_sMessage_ID46Buffer) >= 43.5;
-
}
-
}
-
}
-
}
-
printf("No data\n");
-
}
-
-
if(time(NULL) - nSeconds > 35) g_nErrorCode = 8; // timeout
-
sleep(1);
-
close(fd);
-
return 0;
-
}
-
-
int main(int argc,char *argv[])
-
{
-
int nProtocol = 0;
-
if (argc >= 1)
-
{
-
if (argv[1] == "nmea") {
-
printf("Nmea Protocol\n");
-
nProtocol = 1;
-
}else if (argv[1] == "sirf"){
-
printf("Sirf Protocol\n");
-
nProtocol = 0;
-
}
-
}
-
//nProtocol = 1;
-
printf("---GPS Dump Test---\n");
-
//printf("---GPS Dump Test: %s ---\n", GPS_DataReceive(nProtocol) ? "SUCCEEDED" : "FAILED");
-
int GetGpsInfoSuccess=0;
-
while(!GetGpsInfoSuccess){
-
if(GPS_DataReceive(nProtocol)){
-
GetGpsInfoSuccess=1;
-
printf("Get Gps Info Success! \n\n");
-
}
-
sleep(5);
-
printf("Sorry, Get Gps Info Failed! \n\n\n\n\n");
-
}
-
return g_nErrorCode;
-
}
阅读(767) | 评论(0) | 转发(0) |