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

c_cflag 控制选项
c_lflag 行选项
c_iflag 输入选项
c_oflag 输出选项
c_cc 控制字符
c_ispeed 输入波特率(NEW)
c_ospeed 输出波特率(NEW)

该结构体使用 tcgetattr函数填充,使用tcsetattr函数回填给硬件。

其中,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);}

S3C2440 GPS串口配置以及数据读写相关推荐

  1. 硬件系列(九)--------串口扫码头数据读写

    一.前言 最近需求做扫码支付,但是扫码的扫码头是串口的,需要进行开发才能调起扫码头扫码,头疼了一段时间,在度娘上搜索了一波,做了下总结,也写了一个demo,顺便写一篇博文记录一下. 二.串口管理类 * ...

  2. C# 串口+示波+数据存储+截图

    小菜鸡学习C#一个星期的心得 因学校课设需要,小学期期间花了几天学了一下C#,因为没有人带,纯野生的那种,就算是现在的作品也有不少的Bug,但应付一下答辩什么的还是可以的,费话不多说,先讲讲我的处女作 ...

  3. AC695X_独立3路串口UART收发数据配置

    SDK:AC695N_soundbox_sdk_release_3.1.0 AC695X目前这个SDK是最新的,普通音响的软件包. 其实AC608_AC696X_也可以类似配置,也可以3路串口独立收发 ...

  4. STC15系列单片机通过串口多字节数据读写EEPROM操作

    STC15系列单片机通过串口多字节数据读写EEPROM操作

  5. STM32CubeMX系列教程8:配置工程模板(串口+不定长数据收发+DMA+IDLE中断+软中断)

    文章目录 摘要 生成工程 配置外设 1.配置时钟与Debug 2.配置串口与DMA 3.配置定时器与中断 配置时钟树 配置工程设置 点击`GENERATE CODE`生成工程 修改源码 配置软中断 配 ...

  6. C/C++串口通信原理及读写与操作

    http://wangbaiyuan.cn/c-serial-communication-write-reading.html [展开]文章目录 在工业控制中,工控机(一般都基于Windows平台)经 ...

  7. 学习 STM32之九轴姿态传感器(BWT901CL)串口通信读取数据

    由于个人应用到3轴传感器,所以买了直接买了一个9轴的,用于学习STM32Core平台串口2连接维特智能串口Normal协议,然后通过串口1直接打印数据,接收传感器数据和与传感器进行通信:需要看产品文档 ...

  8. 小草手把手教你 LabVIEW 串口仪器控制——VISA 串口配置

    建议大家按我发帖子的顺序来看,方便大家理解.请不要跳跃式的阅读.很多人现在看书,都跳跃式的看,选择性的看,导致有些细节的部分没有掌握到,然后又因为某个细节耽误很多时间.以上只是个人建议,高手可以略过本 ...

  9. 树莓派串口配置(c++)

    文章目录 前言 一.wiringPi库是什么? 二.使用步骤 1.安装库和接口介绍 2.C++串口配置 总结 注意点: 前言 最近做项目,在树莓派上用到串口来实现通信功能.所以用c++写了个串口的配置 ...

最新文章

  1. matlab 博客,matlab
  2. Linux入门笔记——echo
  3. Qt Model/View/Delegate浅谈 - QAbstractListModel
  4. VLOOKUP函数返回错误值#N/A的两种解决方法
  5. Unity2D开发小细节
  6. php烟花效果,用p5.js制作烟花特效的示例代码
  7. CHR-6dm datasheet 中文翻译
  8. 树莓派安装TPLINK_WN725n v2网卡驱动
  9. 台湾普瑞Parade PS8625| PS8625芯片方案|EDP转LVDS方案| 替代与兼容PS8625
  10. Redis trouble21 -- aof持久化导致redis命令阻塞
  11. 如何禁止搜索引擎收录WordPress站点某个分类的文章?
  12. vo、dto、bo、do、po的概念理解以及与controller、service、dao层的对应关系
  13. CyberCat赛博猫,进阶版AXIE
  14. 怎么让win10隐藏任务栏不会在程序有消息时自动弹出
  15. Android中上下文菜单选项--ContextMenu
  16. Altium Desinger 20概述-安装及卸载
  17. 地震勘探专业词汇(2)FWI专题
  18. 亚马逊的物联网试水--Amazon Echo
  19. 骂程序员恋尸癖?程序员可没那么好惹!
  20. PAT Basic Level 1062 最简分数(辗转相除法)

热门文章

  1. 【中创算力】第六届优秀员工表彰大会暨四月中创生日会
  2. DVI接口关于技术性的知识导论
  3. Matlab--优化工具箱
  4. java 获取docker ip_docker容器内部获取宿主机ip地址方法以及报错解决
  5. adb常用的命令【杭州多测师_王sir】【杭州多测师】
  6. 基于单片机体温心率脉搏检测仪系统设计-毕设资料
  7. Bitmap的加载和Cache
  8. 「环卫吸粪车」天河区抽化粪池抽泥浆抽污水来了,解决黑臭水体!
  9. Modern Robotics读书笔记(一)
  10. W-2 Grub4dos硬盘安装BackTrack