Design advice - TcpServer, Timer
My Qt application is in 4 parts:
1: A Qt front end giving directions to the end user and displaying physiological signals in real time, a total of 6 + 12 graphs
2: A backend part communicating with a simulator solving differential equations with a 10ms time step and sending data to be plotted every 100ms for plotting the 6 graphs (1 X 6 points) listed above. The data is sent over TCP/IP to a socket
3: Another backend part communicating with a simulator solving 3 differential equations with a 1ms time step, but sending data only every 1s for plotting 12 graphs (50X12 points) listed above. The data is sent over a TCP/IP socket.
4: A 3rd backend part communicating with PIC based micro controllers, the micro controller data is sent over TCP/IP to the front end at 60 samples per second normally, at a higher rate when the ADC is activated.
Each runs on a separate thread to be non-blocking. The TCPServer sends data to all the 4 parts intermittently, each of the 4 parts can take 1s to respond, so timing is not for sending data from server to the 3 sockets critical.
Each part has been written individually and works. The critical aspect of the plotting is the 2 simulator data has to be plotted on the same time scale though they come from 2 diff codes.
Finally My Questions:
I start a timer when the program starts and communicate with the simulators based on:
- Time since simulation start.
- Input from micro controller.
What is the best way to accomplish this? I am so used to thinking in C I was going to set off different functions based on time elapsed since program start and input from micro controller. This seems very inelegant. But it works individually (I Have not integrated the 4 parts yet!).
Should I have 2 sockets for each simulator and micro controller since the information is sent only intermittently? I don't want to send just OK with each input to the clients and cause delays. Or should I design the server socket so that it signals when data will be send back and set off a blocking wait for input from server?
Sorry If I am rambling. I feel stuck when there are so many choices to be made and I am not a trained programmer, I have had to do coding as part of my work as a mech engineer and seem to be doing more and more of it of late!
Am I right in saying that the problem you seem to struggle most with is how to combine the data from the different back end due to the difference in timing?
Can it be solved by also exchanging timing information between the different parts?
One struggle is definitely the timing: The trouble is I don't know what exchanging timing information means! One simulator models a electrical system, the other the mechanical aspects. Both happen simultaneously. What timing do I exchange?
The other aspect is optimal use of timer and tcp sockets... I am writing my code without a understanding the classes fully. Reading the documents not helping.
Am I right in saying that, your simulators get data from the same source and that each data point is fed into the differential equations will result in a data point of a graph?
Are your simulators servers (do they accept tcp connections) or do they make connection to your system.
The simulators do get data from the front end, hence same source. I average data over 100 ms(10 points) for the first simulator and every point on 2nd simulator.
The front end Qt is the sever, the simulators are clients connecting to the front end and sending data.
Hope my response is clear :)
- The simulators will return data for the graph, so you should return data that contains both the time and the value: (time, value)
The simulators can then calculate and their own pace and just feed the front end data when they are done. The front needs to make sure to plot all the data correctly.
Doing things this way will problem remove the need for a timer.
- If your front end is the server, you need to be able to handle multiple incoming connections (which can stay alive simultaneously). This is quite straightforward using Qt.
- Thanks! I will keep track of timing from each sim by sending the current time from timer and adding the time step from the sims to get actual time of signal.
- I have already set up a multi-socket server, listening on 3 threads to 3 separate sockets. My doubt is should I have another 2/3 sockets to send data back to the sims and the processor?
Thanks for your insight!
The key here is to think about what timing information needs to be exchanged. If you do it well, in future if you add more simulators or change hardware, everything will keep working because you are making less assumptions.
When a get an incoming connection from the sims and the processor, you can use the same socket to send data. No need for extra socket.
A word of caution on the use of threads though, they can be quite tricky to get right. Qt API is asynchronous and in most cases you don't need separate threads to handle your sockets. Unless you already invested a lot of time and effort in this, I would recommend to try without threads first and just using signal/slots to handle incoming and outgoing data.
Thanks! I will finish trying and testing in 3 days time and then mark thread as [solved] or ask more questions....