"Real time" operations

I have a program that communicates with a microcontroller.
Communication is performed periodically using a timer control.
I am trying to perform communication every 250mSec, but notice that even if I set the timer event trigger to this period, communication occurs every 1 second or so.
The communication data itself takes a fraction of the time (several mSec), The microcontroller itself can delay the communication by several more mSecs - so both do not get close to the 250mSec period.
Therefore I assume that since Windows is not a real time system, setting the timer period to sub-second periods does not really say at what rate it will actually operate.
Any way to give this control some kind of priority?
Any other methods to achieve close to real-time operation (I do not mind if the 250mSec will have a tollerance of 50-100 mSec)

Look into Threads. You can set process priority on those, and have it ‘sleep’ (with a defined time) between processes.

In the thread.run event, do something like:

[code]dim startTime as int64
dim sleepTime as int64

do
startTime = microseconds
[insert your communication code here]
sleepTime = 250 - (microseconds - startTime) // - these are microseconds, not milliseconds… so adjust this accordingly (using as example).
if sleepTime < 0 then sleepTime = 0
me.sleep(sleepTime, false)
loop[/code]

Thanks, will try that

Timers can easily handle sub-second work, down to 10 milliseconds or better, so I don’t understand what is going on in your project. Do you have some process that is tying up the main thread?

Timer resolution is very platform-specific. Thankfully Gilad doesn’t have any “hard” real time operations like audio processing where any lock will kill you. Threads may provide a reasonable workaround for him, but it entirely depends on the rest of the code in his application (and our framework) yielding to the thread often enough.

I am doing exactly what you are saying and have not had the problems you are indicating. Can you verify whether it is the timer or the communications where the issue is? Which device is in control, the PC App or micro? I suspect the issue is really a data read issue, rather than a “timing” issue.

In my experience the timer is very accurate, even at 250mS. To test this, strip out your communications and have the timer just add a line to a listbox. Have the line read the exact time and see if you getting four firings per second. That will at least verify the timer is functional.

You also need to be aware of the data events for the serial class. I always ReadAll, because it is not well document what actually triggers the DataAvailable event to fire, is it one byte, etc… Because you may be receiving messages from the microcontroller you will need to append any partial messages to the previously read data.

You all gave me some food for thought (and tinkering) - will look into this again before switching to threads