• 4607阅读
  • 5回复

串口应用在多线程[已附代码] [复制链接]

上一主题 下一主题
离线butcherty
 

只看楼主 倒序阅读 楼主  发表于: 2010-05-11
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
串口通信程序写到次线程中对应串口终端无响应是什么原因啊?未加到线程中时可正常通信

注:有个warning提示串口文件描述符 fd 未初始化


补充代码: 是子线程run()调用的一个函数


void MyMainWindow::MessageBox_1()
{
  //  int fd;
  int nwrite, j;
  char buf[] = "hello!Good morning everyone!";
  char buf1[24];
  
  MThread thread;
  QMessageBox msgBox;
  msgBox.setText(tr("MAKE SURE"));
  msgBox.setIconPixmap(QPixmap("./456.jpg"));
  msgBox.setStandardButtons(QMessageBox::Ok|QMessageBox::Cancel);
  msgBox.setDefaultButton(QMessageBox::Ok);
  int ret = msgBox.exec();
  
  /*  if((fd = open_port(fd, 1))<0)
    {
      perror("open_port error");
      return -1;
    }

  if((j = set_opt(fd, 9600, 8, 'N', 1))<0)
    {
      perror("set_opt error");
      return -1;
    }
  */
  switch(ret)
    {
    case QMessageBox::Ok:

      if((thread.fd = thread.open_port(thread.fd, 1)) < 0)
    {
      perror("open_port error");
      return;
    }
      
      if((j = thread.set_opt(thread.fd, 9600, 8, 'N', 1)) <0)
    {
      perror("set_opt error");
      return;
    }
    
      printf("fd = %d\n",thread.fd);

      nwrite = write(thread.fd, buf, sizeof(buf));
      printf("nwrite = %d\n", nwrite);

      bzero(buf1, sizeof(buf1));
      while( !read(thread.fd, buf1, sizeof(buf1)));
      label -> setText(tr(buf1));
      //      MThread::close(fd);
      break;

    case QMessageBox::Cancel:
      label -> setText("Cancel");
    break;
    default:
    break;
    }
}

还有个问题就是close()函数怎么调用到子线程中
[ 此帖被butcherty在2010-05-11 11:37重新编辑 ]
离线butcherty

只看该作者 1楼 发表于: 2010-05-11
来个人帮忙啊,急呀
离线butcherty

只看该作者 2楼 发表于: 2010-05-11
自己顶起来,等达人
离线butcherty

只看该作者 3楼 发表于: 2010-05-11
没人回我一直顶
离线butcherty

只看该作者 4楼 发表于: 2010-05-11
还顶
离线butcherty

只看该作者 5楼 发表于: 2010-05-12
真的没人回??急啊
快速回复
限100 字节
 
上一个 下一个