1

我昨天问了这个问题,因为我没有收到任何数据,但奇怪的是,当我在析构函数中使用 wait 时,我开始收到来自QSocketNotifier. 剩下的问题是一样的。有人可以提出一些建议吗?我创建了一个示例应用程序,从中启动单独的线程来读取和处理来自串行端口的数据。QSocketNotifier用于检测串口是否有数据到达。exec()我在线程的运行函数中使用语句启动事件循环。但是,当套接字通知器工作后才运行应用程序时,永远不会生成串行端口激活的信号。而且一旦生成,生成速度非常快,不等于发送设备的帧率。

这是串行通信器线程的简短代码示例:

SerialPortWatchOne.cpp

//constructor
klass::klass()
{
//setport configuration
//miscellaneous initialization
QSocketNotifier* notifier = new QSocketNotifier(mPort->GetFileDescriptor, QSocketNotifier::Read,this);
connect(notifier,SIGNAL(activated(int)),this,SLOT(ReadAndProcessData()));
}

void klass::run()
{
exec();  //this starts an event loop where by Qt signal handling is enabled
}

void klass::ReadAndProcessData()
{
      FlushBuf();      
      int bytes_read=mPort->ReadPort(mBuf,1000);
      if(bytes_read>0)
         //Process data
}

~klass::klass()
{
//desctruction code;
wait();  //so that thread cleanly releases all resources before exit
}

注意:klass 是一个线程,是 GUI 线程的成员,在 GUI 线程 c'tor 中实例化。GUI 会定期使用来自 klass 线程的数据更新其小部件。

任何人都可以建议问题是什么?以前有人这样做过吗。

4

1 回答 1

0

唯一想到的是,您使用的是没有在线程QSocketNotifier * socketNotifier中运行的事件循环。socketNotifier->thread()如果没有事件循环正在运行,您将不会得到任何事件处理。没有事件处理意味着没有从QSocketNotifier. 但是如果你使用一个单独的线程,你可能还是想使用阻塞 I/O 调用。

于 2009-07-21T16:45:56.853 回答