简体   繁体   English

QT中的串行端口和OpenCV线程

[英]Serial Port and OpenCV threading with in QT

I'm making a program (C++) that must use paralell processing, it will call some functions, and some windows will interacts with my Hardware through an emulated Serial Port (qserialport). 我正在编写一个必须使用并行处理的程序(C ++),它将调用某些函数,并且某些Windows将通过模拟的串行端口(qserialport)与我的硬件进行交互。

I have serious problems defining the basic structure of my software (principally because I don't know how to work with parallelization). 我在定义软件的基本结构时遇到了严重的问题(主要是因为我不知道如何使用并行化)。

There is a serial port connected to it that is talking continuously, also the hardware has cameras connected through USB, using the OpenCV libraries to deal with the video. 连接有一个连续通讯的串行端口,并且硬件具有通过USB连接的摄像头,使用OpenCV库处理视频。

I need synchronize and do these tasks in paralell and I have serious confusions. 我需要同步并在paralell中执行这些任务,我感到很困惑。

I have a class that will make the serial port work. 我有一个使串口工作的类。 I have a class that will works with cameras and do recognition tasks. 我有一堂课,将与相机一起工作并执行识别任务。 I think I will have a thread that synchronize these tasks. 我想我会有一个同步这些任务的线程。 I have a main window, that will interact with these tasks. 我有一个主窗口,它将与这些任务交互。 For now, all of these pieces are running in the main thread. 目前,所有这些部分都在主线程中运行。

What is the best way for the main thread to communicate with the others? 主线程与其他线程进行通信的最佳方式是什么? And to synchronize? 和同步? (it is very complicated to me, because I use some data in the main thread, such as images or configure the serial port). (这对我来说非常复杂,因为我在主线程中使用了一些数据,例如图像或配置串行端口)。 What is the "correct" way to do this (I understand that is a very abstract question, also I think that my the problem is very common, then again, all the suggestions will be very well received)? 做到这一点的“正确”方法是什么(我理解这是一个非常抽象的问题,我也认为我的问题很普遍,所以所有建议都将受到欢迎)? How many threads do I need? 我需要多少个线程?

PS: For now, I have an structure which is defined in the main window and it maintains pointers to other initialized objects (such as Vision and SerialPort), it works but I need to lock until the software recognize or the hardware make movements and it is a big problem! PS:现在,我有一个在主窗口中定义的结构,它维护着指向其他初始化对象(例如Vision和SerialPort)的指针,它可以工作,但是我需要锁定直到软件能够识别或硬件进行移动为止。是个大问题! If I try to move these structs to a thread, the program crashes. 如果我尝试将这些结构移动到线程中,程序将崩溃。

Thanks very much! 非常感谢!

I see from your tag that you are using Qt. 我从您的标签中看到您正在使用Qt。 As such I would recommend using QThreads if you are not already as they make a lot of things easier. 因此,如果您尚未使用QThreads ,我建议您使用它们,因为它们使很多事情变得容易。 When using QThreads you make a class that inherits QObject for each helper thread. 使用QThreads时,您将创建一个继承每个帮助程序线程QObject的类。

As for number of threads I would think that you would want at least 3 (main or ui, video, and serial port). 至于线程数,我认为您至少需要3个(主或ui,视频和串行端口)。 You could do more but I wouldn't unless you had a specific need for it (for example I often use 2 helper threads for video, one for capture and one for processing). 您可以做更多的事情,但是除非您有特定的需求,否则我不会做的(例如,我经常使用2个辅助线程进行视频,一个用于捕获,一个用于处理)。

I would recommend communicating between threads with signals and slots rather than calling functions directly. 我建议使用信号和插槽在线程之间进行通信,而不是直接调用函数。 There are a number of ways that you can connect between threads in Qt using different ConnectionTypes . 您可以使用多种方法使用不同的ConnectionTypes在Qt中的线程之间进行连接 You can also use QMetaObject::invokeMethod if you want to call something on the fly. 如果您想即时调用某些内容,也可以使用QMetaObject :: invokeMethod If you interact this way (with the exception of DirectConnection) you will call the function in the thread the object lives in reducing a lot of the difficulty with keeping things thread safe in your classes. 如果以这种方式进行交互(DirectConnection除外),则将在对象中存在的线程中调用该函数,从而减少了在类中保持线程安全的许多困难。

Synchronization pretty vague and therefore will be pretty dependent on what you need. 同步非常模糊,因此将完全取决于您的需求。 Sometimes you can achieve this with a BlockingQueuedConnection which will block in the calling thread until it is completed in the other thread (effectively making them the same thread until it returns). 有时,您可以使用BlockingQueuedConnection来实现此目的,该方法将阻塞在调用线程中,直到在另一个线程中完成(有效地使它们成为同一线程,直到返回)。 Other times just sending notification signals when certain events occur (frame ready for instance) will keep things close enough for it not to matter. 其他时候,仅在某些事件发生时发送通知信号(例如,帧已准备就绪)将使事情保持足够近的程度,无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM