简体   繁体   中英

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).

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.

I need synchronize and do these tasks in paralell and I have serious confusions.

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! 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. As such I would recommend using QThreads if you are not already as they make a lot of things easier. When using QThreads you make a class that inherits QObject for each helper thread.

As for number of threads I would think that you would want at least 3 (main or ui, video, and serial port). 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).

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 . You can also use QMetaObject::invokeMethod if you want to call something on the fly. 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.

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). Other times just sending notification signals when certain events occur (frame ready for instance) will keep things close enough for it not to matter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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