gpt4 book ai didi

c++ - 使用带有事件循环和 QSocketNotifiers 的线程发出串行端口监视

转载 作者:太空宇宙 更新时间:2023-11-04 14:28:36 25 4
gpt4 key购买 nike

我昨天问了这个问题,因为我没有收到任何数据,但奇怪的是,当我在析构函数中使用 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 使用来自类线程的数据定期更新其小部件。

任何人都可以提出问题所在吗?以前有人这样做过吗。

最佳答案

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

关于c++ - 使用带有事件循环和 QSocketNotifiers 的线程发出串行端口监视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1042252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com