免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1243 | 回复: 3
打印 上一主题 下一主题

读串口有问题,帮我看看哪里的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-13 11:28 |只看该作者 |倒序浏览
我的是USB转串口, 设备是  ttyUSB0 , 在开发板上运行了写程序后运行,

在PC上运行自己写的接收程序,读不出来? 但是我直接 cat /dev/ttyUSB0 , 可以读出数据, 这个是什么原因呢?

这个是PC读程序

int set_opt(int fd,int nspeed,int nbits,int nevent,int nstop)
{
        struct termios new;

        bzero(&new,sizeof(new));
        new.c_cflag=CLOCAL|CREAD;
        new.c_cflag&=~CSIZE;

        switch (nbits)
        {
        case 7:
                new.c_cflag|=CS7;
                break;
        case 8:
                new.c_cflag|=CS8;
                break;
        }
        switch(nevent)
        {
        case 'O':
                new.c_cflag|=PARENB;
                new.c_cflag|=PARODD;
                new.c_iflag|=INPCK|ISTRIP;
                break;
        case 'E':
                new.c_cflag|=PARENB;
                new.c_cflag|=~PARODD;
                new.c_iflag|=INPCK|ISTRIP;
                break;
        case 'N':
                new.c_cflag&=~PARENB;
                break;
        }

        switch(nspeed)
        {
        case 9600:
                cfsetispeed(&new,B9600);
                cfsetospeed(&new,B9600);
                break;
        case 115200:
                cfsetispeed(&new,B115200);
                cfsetospeed(&new,B115200);
                break;
        default:
                cfsetispeed(&new,B9600);
                cfsetospeed(&new,B9600);
                break;
        }
       
        switch(nstop)
        {
        case 1:
                new.c_cflag&=~CSTOPB;
                break;
        case 2:
                new.c_cflag|=CSTOPB;
                break;
        }
        new.c_cc[VTIME]=0;
        new.c_cc[VMIN]=0;
       
        tcflush(fd,TCIFLUSH);
       
        if (tcsetattr(fd,TCSANOW,&new)!=0){
                perror("set error");
                exit(0);
        }
               
        return 0;
}

int main(int arg,char **argc)
{
        struct termios oldsetting;
        char buf[10];
        int fd;
        fd_set fdset;
        if (fd=open("/dev/ttyUSB0",O_RDWR|O_NOCTTY|O_NDELAY)<0){
                perror("open");
                exit(0);
        }

        if (tcgetattr(fd,&oldsetting)!=0){
                perror("get setting");
                exit(0);
        }
        if (set_opt(fd,115200,8,'N',1)!=0){
                perror("setopt");
                exit(0);
        }

        FD_ZERO(&fdset);
        FD_SET(fd,&fdset);
        while(FD_ISSET(fd,&fdset)){

        if (select(fd+1,&fdset,NULL,NULL,NULL)<0)
                                perror("select");
        else {
                read(fd,buf,10);
                printf("the buff is %s\n",buf);
       
        }
        }
        printf("over");
        close(fd);
        return 0;
       
       


}

[[i] 本帖最后由 star316 于 2009-6-13 11:31 编辑 [/i]]

论坛徽章:
0
2 [报告]
发表于 2009-06-13 15:32 |只看该作者

re


  1. if (fd=open("/dev/ttyUSB0",O_RDWR|O_NOCTTY|O_NDELAY)<0)
  2. 这句是有问题的,你编译的时候加上-Wall就知道了
复制代码

[ 本帖最后由 奇门遁甲-lu 于 2009-6-13 15:33 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2009-06-13 16:55 |只看该作者
LS 兄弟的眼睛可真亮啊, 谢谢啊. 果然是这里出错了

论坛徽章:
0
4 [报告]
发表于 2009-06-14 11:48 |只看该作者
缺少个括号,呵呵
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP