我昨天问了这个问题,因为我没有收到任何数据,但奇怪的是,当我在析构函数中使用 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 线程的数据更新其小部件。
任何人都可以建议问题是什么?以前有人这样做过吗。