LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1247|回复: 1

串口程序能写不能读

[复制链接]
发表于 2008-5-15 13:35:39 | 显示全部楼层 |阅读模式
直接先上代码:
void ReadSThread::run()     
{   
    int fdr,count,s;
    struct timeval timeout;
    fd_set rethread, wrthread;
   
    fdr = orde->dev;         串口文件描述符
    count = fdr + 1;   
   
    do
    {
     FD_ZERO(&wrthread);
     FD_SET(fdr,&wrthread);
     timeout.tv_sec = 0;
     timeout.tv_usec = 0;

     if(select(count, NULL, &wrthread, NULL, &timeout) > 0)
      write(fdr, orde->ctl, 9);  
      
     FD_ZERO(&rethread);
     FD_SET(fdr,&rethread);
     timeout.tv_sec = 0;
     timeout.tv_usec = 10000;
      
     s = select(count, &rethread, NULL, NULL, &timeout);
      qDebug("ll\n");
     //select(count, &rethread, NULL, NULL, &timeout);
     //if(FD_ISSET(fdr, &rethread))
     //if(select(count, &rethread, NULL, NULL, &timeout) > 0)
      if(s > 0)
      {  
         qDebug("read\n");
         memset(savebuf, 0, sizeof(savebuf));
         if(read(fdr, savebuf, sizeof(savebuf)) > 0)
         {
          printf("%s\n",savebuf);
          QListViewItem* item = new QListViewItem( win1->ListView1, item );
          item->setText( 0, "1");
          item->setText( 1,savebuf);
         }
      }else  
          qDebug("%d\n",s);
     msleep(orde->cyc);         
    }while(flag2);                这里flag2是为true的
}   

用于qt多线程编程读写串口....波特率115200,目的是先写串口发送命令出去,然后读串口的返回值....
在串口调试助手上收到了发送的命令..而且也设置了定时返回值给串口....
可总是读不到...其中的s值总为0,
qDebug("read\n");这句也没有打印出来.....
郁闷了好久找不到原因....
望大虾们不吝赐教了....
发表于 2008-6-1 16:45:30 | 显示全部楼层
给你一个不错的例子,附源码

http://www.b2tang.com/viewthread.php?tid=26&extra=page%3D1
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表