S3C2440 GPS串口配置以及数据读写
生活随笔
收集整理的這篇文章主要介紹了
S3C2440 GPS串口配置以及数据读写
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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_ispeed和c_ospeed使用cfsetispeed和cfsetospeed設置值,cfgetispeed和cfgetospeed獲取值。
它們的值可以是:
波特率設置示例: 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);}
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
| c_cflag | 控制選項 |
| c_lflag | 行選項 |
| c_iflag | 輸入選項 |
| c_oflag | 輸出選項 |
| c_cc | 控制字符 |
| c_ispeed | 輸入波特率(NEW) |
| c_ospeed | 輸出波特率(NEW) |
| 常量 | 描述 |
| 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) |
下面給出完整程序,詳細解釋可以看: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);}
總結
以上是生活随笔為你收集整理的S3C2440 GPS串口配置以及数据读写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 约他没去玩 他却更爱好跟男共事一伏
- 下一篇: python qq群文件_python