Z
Z
Zenker2013-03-01 22:32:42
Iron
Zenker, 2013-03-01 22:32:42

Execution of a section of code in real time, time tracking?

Here's a snippet of code:

t1 = timeGetTime();
    Device.sendRequest( Command1 );
    R1 = Device.readResponse();

    Device.sendRequest( Command2 );
    R2 = Device.readResponse();
t2 = timeGetTime();
cout << t2-t1 << " ms" << endl;

We send a request to some device via the RS-232 interface, then wait for a response and repeat everything again. Multimedia Timer is used to measure the operating time. There are two questions:
1) Is it generally correct to measure the operating time in this way? This timer, according to Microsoft, provides a resolution of 1 ms. In real conditions, this code is executed (according to this timer) in 20..50 ms, and the accuracy of this measurement plays a critical role in solving the problem.
2) Actually, the main question. We work in a multitasking environment. If the above measurement is run, say, 100 times in a row, the average values ​​are 20..30 ms, which more or less converges with theoretical calculations for a given transmission rate and message length. However, periodically there are emissions of 50..60 ms, and this moment is of fundamental importance. If my assumption is correct, somewhere in the process of waiting for a response or before sending a second request, the process may be considered waiting and the scheduler “wedges” some extraneous process into execution. Of course, it is possible that the matter is in the equipment itself, but I would like to exclude the influence of the OS. Actually, the question is: please tell me a competent way: how can I increase the priority for this section of code so that it is executed as monopolistically as possible,
I don’t have the opportunity to try it myself right now, but I managed to google about the SetPriorityClass() and SetThreadPriority() functions. Is this at least the right direction to help solve the problem?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Vladimir, 2013-03-01
@noonv

is Windows 7 a real time system? :)
set a higher priority - yes - it will help a little, but it won't get rid of the context switch.
if RT is important, then it is better to look towards RTOS, try Linux with an RT patch, etc.

@
@ntkt, 2013-03-02
_

Regarding timers in Windows, there is a good introductory article by Jan Wassenberg "Timing Pitfalls and Solutions"
github.com/0ad/0ad/raw/master/docs/timing_pitfalls.pdf

E
egorinsk, 2013-03-01
@egorinsk

Windows has more accurate methods for measuring time, e.g. QueryPerformanceCounter. To find out if your process has been preempted, you can look up the value of some context switch counter.

I
Ilya Evseev, 2013-03-02
@IlyaEvseev

Regarding the timer - have you already read stackoverflow.com/questions/1825720/c-high-precision-time-measurement-in-windows ?
This is the first link on Google for "windows high resolution timer".
As for the priorities, everything is written correctly, they write the same thing here.
Found by Google for "windows change process priority c++".
On a multi-core processor, the maximum priority will allow the process/thread to get _almost_ realtime.
If it still turns out to be insufficient, then it remains to either switch to a full-fledged (?) RT OS, or take a WDK and write your own driver :)

V
Vladimir Martyanov, 2013-03-02
@vilgeforce

As already mentioned above: Windows is not Real Time. Second: a serial port, at least in XP / 2K - a tricky thing and the time may depend on the implementation of Device.readResponse. And the most, after all, the main question: what do you measure? Real time from receiving a parcel to the end of the transmission (from the point of view of the device) or the time from transmission to reception on Windows? If the first - LED and oscilloscope. If the second is sadness.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question