Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Thread&time critical port processing

Posted on 2005-03-21
7
Medium Priority
?
319 Views
Last Modified: 2008-02-01
Hi!

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
begin
  - some non time critical processing.
  .
  .
  TimeCriticalProcedure(str);
end  
 

TimeCriticalProcedure:

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

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

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....
0
Comment
Question by:Roza
7 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 13590460

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

mokule
0
 
LVL 17

Accepted Solution

by:
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

mokule
0
 
LVL 2

Author Comment

by:Roza
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Expert Comment

by:Molando
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 :)

Molando.

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

Expert Comment

by:mokule
ID: 13594095
@Molando
But TimeSetEvent still allows milliseconds resolution whilst QueryPerformanceCounter allows microseconds resolution.

mokule
0
 
LVL 12

Expert Comment

by:Lee_Nover
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
0
 
LVL 2

Author Comment

by:Roza
ID: 13629118
Hi!

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
QueryPerformanceCounter(time2)
while (restoftime>time2) do QueryPerformanceCounter(time2)

Is that good way or not?

I will also have a look at TimeSetEvent...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

571 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