Chinaunix首页 | 论坛 | 博客
  • 博客访问: 90236
  • 博文数量: 14
  • 博客积分: 1472
  • 博客等级: 上尉
  • 技术积分: 172
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-26 09:55
文章分类

全部博文(14)

文章存档

2011年(13)

2007年(1)

我的朋友

分类: LINUX

2011-02-13 11:42:17

#include
#include
#include
#include
#include
#include
#include    //如果不添加,printf,exit等会警告参数不匹配等
#include
void set_speed(int, int);
int main(int argc,char* args[])
{
        int fd,result,wr_num=0,rd_num=0,z=1,buff_num;
        struct termios term_param;//串口通信参数结构体
        speed_t baud_rate_i,baud_rate_o;        //无符号整数类型
        char buff[100],read_buf[1];
        //打开串口 (根据实际情况修改串口设备名)
        fd=open("/dev/s3c2410_serial1",O_RDWR|O_NONBLOCK);
        if(!fd){//打开串口失败
                perror("syd168:Can't open the COM1!\n");
                exit(-1);
        }
        else
                printf("syd168:Open COM1 ok!\n");
        //获取打开串口的参数
        result=tcgetattr(fd,&term_param); //取得原配置参数
        if(result==-1){
                perror("syd168:Get param error!\n");
                exit(-1);
        }
        baud_rate_i=cfgetispeed(&term_param);//读取输入速度
        baud_rate_o=cfgetospeed(&term_param);//读取发送速度
        printf("初始:输入波特率是%d,输出波特率是%d,文件描述符是%d\n",baud_rate_i,baud_rate_o,fd);
        set_speed(fd,4800);
        result=tcgetattr(fd,&term_param);
        if(result==-1){
                perror("syd168:Set param error!\n");
                exit(-1);
        }
        baud_rate_i=cfgetispeed(&term_param);
        baud_rate_o=cfgetospeed(&term_param);
        printf("设置后:输入波特率是%d,输出波特率是%d,文件描述符是%d\n",baud_rate_i,baud_rate_o,fd);
        buff_num=0;//缓存中字符数为0
        printf("syd168:Receive....\n");
        while(1)
        {
                //接收一个字符
                rd_num=read(fd,read_buf,sizeof(read_buf));
                if(rd_num>0){   //接收到字符
                        /*收到字符直接输出
                        printf("%c",read_buf[0]);
                        continue;
                        */
                        if(read_buf[0]=='$'){//如果该字符是$
                                if(buff[0]=='$'&&buff_num>0){//一个GPS信号接收已经完成,处理接受的GPS信息(打印)
                                        int j=0;
                                        while(j
                                        //清除缓存和缓存计数,继续接受GPS信号
                                        buff_num=0;
                                        memset(buff,0,sizeof(buff));
                                }
                                buff[buff_num++]=read_buf[0];//记录GPS信息开始符
                                goto delay;     //延时1秒钟
                        }
                        else{ //接收到非$字符
                                if(buff[0]=='$')//已经存在首字符,则添加后续字符
                                        buff[buff_num++]=read_buf[0];
                                continue;//如果开头的$不存在,放弃该字符
                        }
                }
                else{
                        //printf("Receive....\n");
                        continue;
                }
delay:
                sleep(1);
        }
}

int speed_arr[] = {B115200,B38400, B19200, B9600, B4800, B2400, B1200, B300,
                        B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200,38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
                        19200, 9600, 4800, 2400, 1200, 300, };
void set_speed(int fd, int speed){
    int   i;
    int   status;
    struct termios   Opt;
    tcgetattr(fd, &Opt);
    for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
        if (speed == name_arr[i]) {
                tcflush(fd, TCIOFLUSH);
                cfsetispeed(&Opt, speed_arr[i]);
                cfsetospeed(&Opt, speed_arr[i]);
                status = tcsetattr(fd, TCSANOW, &Opt);
                        if (status != 0) {
                                perror("tcsetattr fd1");
                                return;
                        }
                tcflush(fd,TCIOFLUSH);
        }
   }
}
 
 
=================采集数据举例===========================
 
 
Receive....
$GPGGA,003841.000,3604.9074,N,10427.3777,E,2,06,1.5,107.1,M,-14.7,M,2.8,0000*50
$GPRMC,003841.000,A,3604.9074,N,10427.3777,E,0.00,201.43,180211,,,D*65
$GPVTG,201.43,T,,M,0.00,N,0.0,K,D*0C
$GPGGA,003842.000,3604.9074,N,10427.3777,E,2,06,1.5,107.1,M,-14.7,M,1.8,0000*50
$GPGSV,3,1,12,04,69,352,19,10,61,194,23,17,51,116,25,02,45,285,15*7F
$GPRMC,003842.000,A,3604.9074,N,10427.3777,E,0.00,201.43,180211,,,D*66
阅读(5021) | 评论(2) | 转发(2) |
给主人留下些什么吧!~~

yxr11092014-06-04 15:50:39

很好!感谢分享! 我在我的设备(9600bps)上调试了一下,个人觉得代码 sleep(1);处 延时1秒钟 时间太长了,会影响产品的性能指标;我这里改成 延时0.1秒 再测测。

chinaunix网友2011-03-06 15:57:59

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com