Thread&time critical port processing

Posted on 2005-03-21
Medium Priority
Last Modified: 2008-02-01

I am developing application which controls DTR singnal on com port or set some values on LPT port.

I am using delphi6&7.

For com port handling I use CPortLib, for LPT SmallPort.

The problem I don't know how to make processing accurate as it can be.

My tehnique:

On Execute:

while not self.terminated do
  - some non time critical processing.


start of loop which calculates times based on some values passed in string...
  - CalculateTimeDTR should be on
  calculate TimeDTR should be off
  SetDTR (false, stoptime, durationtime)
end of loop

procedure TMyThread.SetDTR(onoff: boolean;var StopTime: Cardinal;DurationTime: Cardinal);
   while Windows.GetTickCount<StopTime do Sleep(1);
   if onoff then ComPort.SetDTR(true)
   else ComPort.SetDTR(false);

Priority of thread is set to tpTimeCritical.

What to do to maximum accurracy of port output.
ComPort&LPT components are created in thread.

Basicaly my thread does not need processor all the time, but only when the right time comes.

Some questions will be posted later. Thanks for any hints/ideas....
Question by:Roza
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 17

Expert Comment

ID: 13590460

Are You aware that GetTickCount has resolution 10ms or 55ms depending on Win version
Is it enough for You?

LVL 17

Accepted Solution

mokule earned 2000 total points
ID: 13590481
If You need more accuracy use
QueryPerformanceCounter and QueryPerformanceFrequency functions
In general Win is not good OS for these tasks


Author Comment

ID: 13590959
Hm. I did not know that about GetTickCount's resolution.

I know Win is not good for these tasks, but I know it should be ok for my task.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 13592943
GetTickCount is not your friend. A TTimer is painfull, as ends up being a windows message.

The best you could  use would be to go for the windowsAPI functions TimeSetEvent and TimeKillEvent. These functions work on callback events, rather than windows messages. Just a warning, the finer you set the timer, the more you will slow other things down.

Oh and in true microsoft stylie these sorts of timers are known multimedia timers, as multimedia programs need very accurate timers :)


Hit f1 in delphi on TimerSetEvent, they are fairly straight forward.
LVL 17

Expert Comment

ID: 13594095
But TimeSetEvent still allows milliseconds resolution whilst QueryPerformanceCounter allows microseconds resolution.

LVL 12

Expert Comment

ID: 13595103
don't use a tight loop like that in a TimeCritical thread !
instead calculate the time needed to wait and wait for it
like: WaitForSingleObject(Handle, StopTime - GetTickCount)
leave it at normal priority .. you really won't benefit much with using a timecritical thread
in the case of you current design you're only slowing everything else down and not gaining anything

Author Comment

ID: 13629118

I have analyzed my problem and figured out that 1ms precision is not good enough since smallest time to have DTR unchanged is 40ms, could be even less.

Setting process priority to TimeCritical means a lot. If I set it to normal there is significant distortion (special when doing some other operation on system - like browsing&file copying) in signals going out if not there is qute an improvement, but not good enough.

Probably there is next step to try combine WaitForSingleObject(time*0.9) and then last few ms's doing
while (restoftime>time2) do QueryPerformanceCounter(time2)

Is that good way or not?

I will also have a look at TimeSetEvent...

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question