S3C2440 GPS串口配置以及数据读写
参考文章:http://www.cnblogs.com/jason-lu/articles/3173988.html
http://www.cnblogs.com/chengmin/p/3818133.html
gps模块用串口线与S3C2440的ttyS1串口连接
串口操作需要的头文件:
<span style="font-size:18px;">#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix 标准函数定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX 终端控制定义*/
#include <errno.h> /*错误号定义*/</span>
打开串口的方式:
<span style="font-size:18px;">int fd;
/*以读写方式打开串口*/
fd = open( "/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (-1 == fd){
/* 不能打开串口一*/
perror(" 提示错误!");
}</span>
设置串口参数:
串口参数使用termios结构体保存
struct termios
该结构体使用
tcgetattr函数填充,使用tcsetattr函数回填给硬件。
c_cflag | 控制选项 |
c_lflag | 行选项 |
c_iflag | 输入选项 |
c_oflag | 输出选项 |
c_cc | 控制字符 |
c_ispeed | 输入波特率(NEW) |
c_ospeed | 输出波特率(NEW) |
其中,c_ispeed和c_ospeed使用cfsetispeed和cfsetospeed设置值,cfgetispeed和cfgetospeed获取值。
它们的值可以是:
常量 | 描述 |
CBAUD | Bit mask for baud rate |
B0 | 0 baud (drop DTR) |
B50 | 50 baud |
B75 | 75 baud |
B110 | 110 baud |
B134 | 134.5 baud |
B150 | 150 baud |
B200 | 200 baud |
B300 | 300 baud |
B600 | 600 baud |
B1200 | 1200 baud |
B1800 | 1800 baud |
B2400 | 2400 baud |
B4800 | 4800 baud |
B9600 | 9600 baud |
B19200 | 19200 baud |
B38400 | 38400 baud |
B57600 | 57,600 baud |
B76800 | 76,800 baud |
B115200 | 115,200 baud |
EXTA | External rate clock |
EXTB | External rate clock |
CSIZE | Bit mask for data bits |
CS5 | 5 data bits |
CS6 | 6 data bits |
CS7 | 7 data bits |
CS8 | 8 data bits |
CSTOPB | 2 stop bits (1 otherwise) |
CREAD | Enable receiver |
PARENB | Enable parity bit |
PARODD | Use odd parity instead of even |
HUPCL | Hangup (drop DTR) on last close |
CLOCAL | Local line - do not change "owner" of port |
LOBLK | Block job control output |
CNEW_RTSCTS/CRTSCTS | Enable hardware flow control (not supported on all platforms) |
struct termios options;
/*
* Get the current options for the port...
*/
tcgetattr(fd, &options);
/*
* Set the baud rates to 19200...
*/
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);
/*
* Enable the receiver and set local mode...
*/
options.c_cflag |= (CLOCAL | CREAD);
/*
* Set the new options for the port...
*/
tcsetattr(fd, TCSANOW, &options);
“
常量TCSANOW标志所有改变必须立刻生效而不用等到数据传输结束.其他另一些常数可以保证等待数据结束或者刷新输入输出之后再生效.”
下面给出完整程序,详细解释可以看:http://www.cnblogs.com/jason-lu/articles/3173988.html
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<termios.h>
#include<string.h>
typedef struct _tim
{
int hh;
int mm;
float ss;
}tim;
typedef struct _addr
{
int dd;
float mm;
char pos;
}addr;
typedef struct _date
{
int year;
int month;
int day;
}date;
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;
if ( tcgetattr( fd,&oldtio) != 0) {
perror("SetupSerial 1");
return -1;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= (CLOCAL | CREAD);
newtio.c_cflag &= ~CSIZE;
switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch( nEvent )
{
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
newtio.c_cflag &= ~PARENB;
break;
}
switch( nSpeed )
{
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
case 460800:
cfsetispeed(&newtio, B460800);
cfsetospeed(&newtio, B460800);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )
newtio.c_cflag &= ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;//重要
newtio.c_cc[VMIN] = 0;//返回的最小值 重要
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
// printf("set done!\n\r");
return 0;
}
void analyser(char buf[]);
int main(void)
{
int fd1,nset1,nread;
char buf[1024];
fd1 = open("/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);//打开串口
if (fd1 == -1)
exit(1);
nset1 = set_opt(fd1,4800, 8, 'N', 1);//设置串口属性
if (nset1 == -1)
exit(1);
while (1)
{
memset(buf,0,1024);
nread = read(fd1, buf, 1024);//读串口
if (nread > 0){
// printf("\n GPS DATALen=%d\n",nread);
buf[nread] = '\0';
// printf( "GPS %s\n", buf); //输出所读数据
break;
}
sleep(2);//睡眠,等待数据多一点
}
close(fd1);
analyser(buf);
return 0;
}
void analyser(char buf[1024])
{
char *new_buf;
tim cur_time;
addr cur_lat;
addr cur_lng;
date cur_date;
float speed;
float heading;
char is;
new_buf = strstr(buf, "RMC");
new_buf += 4;
sscanf(new_buf, "%2d%2d%f,%c,%2d%f,%c,%3d%f,%c,%f,%f,%2d%2d%2d",\
&cur_time.hh,&cur_time.mm,&cur_time.ss,\
&is,\
&cur_lat.dd,&cur_lat.mm,&cur_lat.pos,\
&cur_lng.dd,&cur_lng.mm,&cur_lng.pos,\
&speed,&heading,\
&cur_date.day,&cur_date.month,&cur_date.year);
printf("当前时间:%d时%d分%.2f秒\n", cur_time.hh, cur_time.mm, cur_time.ss);
printf("当前位置:%s %d度%.4f分, %s %d度%.4f分\n", cur_lat.pos == 'N'?"北纬":"南纬", cur_lat.dd, cur_lat.mm\
,cur_lng.pos == 'E'?"东经":"西经", cur_lng.dd, cur_lng.mm);
printf("当前日期:20%d年%d月%d日\n", cur_date.year, cur_date.month, cur_date.day);
}