I have a problem with signal acquisition. I have a class called rawdataprovider that is responsible for acquiring data from the serial port. This class is in a thread with the highest priority, and every 25 ms it extracts the data from serial port, through the QExtSerialPort class. While only that is done, the program works correctly, that is, every 25ms the data is extracted from the serial port and sent to the class in charge of managing them. The problem starts when I navigate through the interface, particularly when I move through the Qwidget of the main interface. The processing associated with focusing a Qwidget and painting a frame on it, makes the signal that should be sent every 25 ms to extract the data from the serial port be delayed. Any ideas to solve this problem?
I find it difficult to understand that the simple fact of focusing a Qwidget delays so much a thread. I have tried other ways to extract data from the serial port. I tried to connect the readyRead () signal, which is sent when there is data available on the serial port, with the method responsible for processing the data, but the same problem occurs.
Related
I know very similar questions have been asked before, but I am unable to find an answer for my specific problem. I have a main (GUI) thread which upon button press initializes a worker thread to perform some analysis. I am using signals and slots to communicate between my worker thread and my GUI thread (i.e. when the thread starts and when it finishes), but I need to go deeper than that. My worker thread actually calls another class in a separate implementation file which then iterates through a series of calculations which are sent to std::cout for each iteration (as the code used to be a console application for which I am now writing a GUI). I am trying to feed those outputs for each iteration back into my GUI thread so that my text browser is updated in real time as my code iterates. The problem is, when I emit a signal from the class my worker thread calls, it is not picked up by the GUI thread. I do not get any errors. Does anyone have any suggestions on how to transmit a signal to the GUI from a class that my worker thread is calling? I can post code as required, but I'm not sure what would be most helpful to see and my code is quite extensive (it's an aircraft performance application). Any help would be greatly appreciated. Thank you kindly!
1) Make sure the connect() call for connecting you signal returns true. If it does not, qdebug output usually tells you what is wrong
2) You should use the QueuedConnection type (the default (Auto) should work also)
System / software Details :
Qt Version 4.7.4
Linux
Kernel : 2.6.31 (Custom kernel built for IMX25)
Peripherals :
Graphic LCD (64x128)
Quectel (M 12) GPRS module
Thermal printer
Database : Sqlite3
I am a beginner don't have much experience either in Qt or programming with Linux. I have developed an application where user enter some data manually and that data gets saved in sqlite database. So what I am trying to do is that after certain time lets say 90 seconds data from database should get transferred to the server using the GPRS.
So I am using the Qt's signal and slots mechanism to do the timed data transfer. I have created a slot which gets fired every 90 seconds and as the slot gets fired I am creating/launching a POSIX thread which is suppose to transfer the data to the server.
So what that thread does is it launches the "pppd" and once the "pppd" is up it queries the database for the data and sends the data to the server. And once the data transfer is done I kill the "pppd". The functionality works fine.
But the problem is that the "pppd" takes time to launch so I had to introduce some delay. i.e sleep of 12 seconds is there in order to let the pppd launch successfully. But as the sleep is blocking it makes the main program/thread non responsive until the "pppd" is launched (i.e. it halts/stops all the activities like printing etc.). And as the "pppd" is launched the main thread becomes responsive again.
So please suggest me some solution in order to keep the main thread responsive when "pppd" is launching or please suggest me if there is any other alternative for the same. Also guide me if there's anything wrong with my approach..
Thanks in advance. And I am sorry if I have not followed your standards..
There are several options available to you. It looks like you're using a thread, but then calling sleep in the main thread, which is basically the same as not using thread to do your work at all! You need to leverage Qt processes as illustrated below:
You can use a QProcess in which you start() and use the signal finished() to notify the main thread that it is complete. This is probably what you want to use. All you have to do is:
QProcess *proc = new QProcess;
proc->start("theProcess", QStringList() << "the" << "arguments");
connect(proc, SIGNAL(finished()), this, SLOT(someSlotToUse()));
This sequence of code will start a new process with your arguments, and then call someSlotToUse() when the process is complete.
You can use a QThread along the same pattern.
Basically, what you're trying to do is do the work in another thread, keeping the GUI reactor free to process GUI events rather than long queries. This is a classic problem in Qt and there is a lot of literature out there.
Alternately, you can use a QProcess::concurrent() call, which allows you to run a function in another process, but I've never tested it.
Here are some references for you to look at: Qt Concurrent, QProcess, and QThread
So I realize my explanantion wasn't too clear last time so I will try again.
I have a program that has a sensor class that gets values from the Lego NXT sensor every 40 miliseconds or so. I also have another draw class (i believe running in the same thread) that draws and moves cars every 1 milisecond using opengl in a virtual world. Now the problem is whenever my sensors are getting information the cars are very laggy and move slowly, but when the sensors are off and not getting and input data the cars run smoothly. How do I fix this problem, I believe i need to create another thread but im not sure how to do that?
note: The sensors have nothing to do with the cars.
It's not so easy to give a generic answer to threading. Qt gives you various classes to implement threading so it's good to take a look at examples. In general you do not need threading with Qt except if you have a blocking api which I guess your sensor module/class is.
Here is a nice video showing live how to create an object that runs on another thread. You can try to implement the LenghtyOperation in the demo as your class that reads data from your sensors and then emit signals to update your GUI thread.
Here is another link: Threading without the headache
I am trying to make a multi-player network game. Each player is represented by a rectangle on the screen. I am using OpenGL for the graphics and also the user input (commands like MOVE-LEFT, MOVE-RIGHT etc ) will be handled by it (or GLUT or sumthing).
I have the following architecture for the game.
There are 4 players(nodes) in the game. Each player is sending and receiving data using UDP. Each player can send data to any other player.
Data is required to be sent by a player if there is any input from the corresponding user. (For example MOVE-LEFT command etc).
Whenever a player (say p1) receives any data from any other player(say p2) (like new position of the player p2 on the screen), the player p1 's screen should be updated immediately.
I am thinking on the following lines :
Create one thread for handling graphics.
Create 2 more threads , 1 each for receiving and sending data, using UDP.
Whenever the graphics thread gets input for 'myposition' from the user, it updates the shared global variable 'myposition'. The network-send thread, which is waiting on this variable, gets activated and tells every other player about its new position.
Similarly whenever 'position' updates are received from any other player 'i', the network-receive thread updates the global variable player[i].position. The graphics thread will now redraw the scene with the updated positions.
Is this design correct. If yes, How good is this design and how can i improve it
Network Game Programming is a monster of a topic, so it is hard to say "yes, this is how you design a network architecture". It is completely dependent on your game requirements. What sort of volume of packets do you plan on sending? Will there be a packet sent every frame that indicates Player A is holding the left key? Is this traffic confined to a LAN?
For something as simple as synchronizing movement between amongst 4 clients, putting send, receive, and rendering in separate threads seems like overkill. Perhaps as a starting point, you should begin with a more simple design and make the move to multithreading when you feel that your packets are not going out fast enough.
For example, you may wish to have a game loop like the following (all within the same thread):
while (running):
readUpdSocketForIncomingPackets();
updateGameObjects();
renderGameObjects();
sendPacketsToPeers();
At the start of each frame, you can read your udp socket for incoming packets and update positions (and whatever else you are sending to your peers), then draw. As game input is processed, packets are created and accumulated in a packet queue. Doing it this way, allows you to perform optimizations, such as cramming/merging multiple messages into one packet, removing duplicate messages (e.g. only send the latest position update), etc. So at the end of each game loop, the final queue of packets are processed and send to the peers.
But again, this is a big topic and I've glossing over a lot of details.
Take a look at gaffer's blog:
http://gafferongames.com/networking-for-game-programmers/what-every-programmer-needs-to-know-about-game-networking/
He's got some great articles that address some fundamentals of network game programming.
Here is what I need to do.
-I receive log data through a udp connection
-I stack relevant data in a qlist
-I have a timer running in the main thread that, on timeout, unstacks this data, updates some arrays then calls widget->update
-The widget re-implements paintEvent and uses these arrays to draw charts.
What would be the best way to do this in order to not have any bugs.
this task is basically three processes, two of which are done in the main thread
1-I have a qthread that asks for and receives log through udp packets. This thread also stacks the data in a qlist.
2-I have a qtimer that on timeout, unstacks these events, preps the chart arrays and then calls update
3-I have a reimplementation of the paintEvent method on that widget.
I have mutexes to synchronise and protect data. Is this a bad way of doing it? Some suggestion for a "SAFE" way of doing would be appreciated.
On a side note the paintEvent is dont on a customwidget which is inside my mainwindow. I do have a second thread (concurent function) that periodically refreshes some data then emits a signal to update label fields that are outside the custom widget but inside the mainwindow. could this have a bad side effect on everything?
Overall, I think you've got the foundation for a solid program. The only suggestion I might make is to move task #2 to its own thread also. You can take advantage of the fact that you can draw on a QImage outside of the main UI thread to prep the chart arrays in a different thread. This would remove the biggest potential bottleneck to UI responsiveness from your code, and wouldn't add much more to the complexity, since you already have threads in your program. The updating on QTimer could work there just as well. When a new image is ready, you could send it either via signal or via posted event to your UI, which could then copy the image and update its display.