Solved

Still Need Microsecons...

Posted on 2002-05-17
12
189 Views
Last Modified: 2009-07-29
Hi fellows,
I tried at the advice of RichW the following to achieve a short delay of a few miliseconds, but unfortunately it kocks my computer during the time.
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sleep 0.05

Any other ideas how to wait just about 50-60, say, at most 200 microseconds? I might need to make this value variable in the run-time too.
Thank you.
0
Comment
Question by:robertut
12 Comments
 
LVL 2

Expert Comment

by:ventond
ID: 7017673
You can always set a timer control to go off after x amout of time.

Timer.interval = x
timer.enabled = true

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 7017681
to ventond,

the finest time step for a timer is 1 millisecond = 1000 microseconds.  That WILL NOT meet the needs of this question.
0
 
LVL 4

Expert Comment

by:RichW
ID: 7017697
He originally said that he doesn't want to use a timer object.

Robertut, what do you mean it kocks your computer?

You declared the API in a .bas module and enter the Sleep 0.50 statement right before your function?  then what happens?

RichW
0
 
LVL 4

Expert Comment

by:mcoop
ID: 7018058
Try searching under high resolution timer

You're right sleep() does stop your thread until the delay is over, and is not practical for anything much less than 50mS.

With a hi-res timer (there are a few around), you can get reliable timing well below 1mS (I can't remember the low limit).

Good luck
0
 

Author Comment

by:robertut
ID: 7018067
To RichW,
yes.
but, the performance monitor in the windows task manager jumps up to 100% during the whole routine.
It works, but the main problem is that i can hardly switch the routine off, my program responds very hard andeven stops responding. If i comment the sleep, it doesn't(but it doesn't read accurately)
I thougt about using soemthing like DoEvents(which does work well), but the duration of that cannot be predicted.
I am using the DLPortIO driver(www.sstnet.com) to access the LPT directly, this is a driver specially created for data aquisition, it is very fast.
Here's my code:

For i = 0 To 1000
DlPortWritePortUchar &H378, 0  'select hw channel 1
DlPortWritePortUchar &H37A, 0  'start hw follow
DlPortWritePortUchar &H37A, 1  'start hw memory
DlPortWritePortUchar &H37A, 3  'init hw A/D conversion
DlPortWritePortUchar &H37A, 1  'start hw A/D conversion
Sleep wait   'wait until conversion complete
DlPortWritePortUchar &H37A, 0  'select hw output mux1
puncteb1(i) = DlPortReadPortUchar(&H379)  'read value
DlPortWritePortUchar &H37A, 4  'select hw output mux2
puncteb2(i) = DlPortReadPortUchar(&H379)  'read value
Next i

where wait = 0.1 tipically, but must be set with a scrollbar.
Thx
0
 

Author Comment

by:robertut
ID: 7018068
To RichW,
yes.
but, the performance monitor in the windows task manager jumps up to 100% during the whole routine.
It works, but the main problem is that i can hardly switch the routine off, my program responds very hard andeven stops responding. If i comment the sleep, it doesn't(but it doesn't read accurately)
I thougt about using soemthing like DoEvents(which does work well), but the duration of that cannot be predicted.
I am using the DLPortIO driver(www.sstnet.com) to access the LPT directly, this is a driver specially created for data aquisition, it is very fast.
Here's my code:

For i = 0 To 1000
DlPortWritePortUchar &H378, 0  'select hw channel 1
DlPortWritePortUchar &H37A, 0  'start hw follow
DlPortWritePortUchar &H37A, 1  'start hw memory
DlPortWritePortUchar &H37A, 3  'init hw A/D conversion
DlPortWritePortUchar &H37A, 1  'start hw A/D conversion
Sleep wait   'wait until conversion complete
DlPortWritePortUchar &H37A, 0  'select hw output mux1
puncteb1(i) = DlPortReadPortUchar(&H379)  'read value
DlPortWritePortUchar &H37A, 4  'select hw output mux2
puncteb2(i) = DlPortReadPortUchar(&H379)  'read value
Next i

where wait = 0.1 tipically, but must be set with a scrollbar.
Thx
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:robertut
ID: 7018069
To RichW,
yes.
but, the performance monitor in the windows task manager jumps up to 100% during the whole routine.
It works, but the main problem is that i can hardly switch the routine off, my program responds very hard andeven stops responding. If i comment the sleep, it doesn't(but it doesn't read accurately)
I thougt about using soemthing like DoEvents(which does work well), but the duration of that cannot be predicted.
I am using the DLPortIO driver(www.sstnet.com) to access the LPT directly, this is a driver specially created for data aquisition, it is very fast.
Here's my code:

For i = 0 To 1000
DlPortWritePortUchar &H378, 0  'select hw channel 1
DlPortWritePortUchar &H37A, 0  'start hw follow
DlPortWritePortUchar &H37A, 1  'start hw memory
DlPortWritePortUchar &H37A, 3  'init hw A/D conversion
DlPortWritePortUchar &H37A, 1  'start hw A/D conversion
Sleep wait   'wait until conversion complete
DlPortWritePortUchar &H37A, 0  'select hw output mux1
puncteb1(i) = DlPortReadPortUchar(&H379)  'read value
DlPortWritePortUchar &H37A, 4  'select hw output mux2
puncteb2(i) = DlPortReadPortUchar(&H379)  'read value
Next i

where wait = 0.1 tipically, but must be set with a scrollbar.
Thx
0
 

Author Comment

by:robertut
ID: 7018090
sorry fellows, my internet connection really sucks... :(
0
 
LVL 4

Expert Comment

by:RichW
ID: 7018218
0
 
LVL 15

Expert Comment

by:ameba
ID: 7019168
>main problem is that i can hardly switch the routine off, my program responds very hard andeven stops responding

DoEvents is slow, it will allow execution of low priority tasks, like paint events.  But there are special functions for tight loops where execution speed is critical: GetInputState and GetQueueStatus

Here is the sample for loop, which can be interrupted by pressing "Stop" button using the keyboard.  To react to mouse events, change "QS_KEY" in "If GetQueueStatus(QS_KEY)" to some other value (e.g. QS_MOUSE)

' Form1, add button
Option Explicit
Private stopped As Boolean  ' flag
Private Declare Function GetInputState Lib "user32" () As Long
Private Declare Function GetQueueStatus Lib "user32" (ByVal fuFlags As Long) As Long
Private Const QS_HOTKEY = &H80
Private Const QS_KEY = &H1
Private Const QS_MOUSEBUTTON = &H4
Private Const QS_MOUSEMOVE = &H2
Private Const QS_PAINT = &H20
Private Const QS_TIMER = &H10
Private Const QS_MOUSE = (QS_MOUSEMOVE Or QS_MOUSEBUTTON)
Private Const QS_INPUT = (QS_MOUSE Or QS_KEY)
Private Const QS_POSTMESSAGE = &H8
Private Const QS_SENDMESSAGE = &H40
Private Const QS_ALLEVENTS = (QS_INPUT Or QS_POSTMESSAGE Or QS_TIMER _
    Or QS_PAINT Or QS_HOTKEY)
Private Const QS_ALLINPUT = (QS_SENDMESSAGE Or QS_PAINT Or QS_TIMER Or _
    QS_POSTMESSAGE Or QS_MOUSEBUTTON Or QS_MOUSEMOVE Or QS_HOTKEY Or QS_KEY)

Private Sub Command1_Click()
    Dim i As Long, x As Long
   
    If Command1.Caption = "Stop" Then
        stopped = True
    Else
        Command1.Caption = "Stop"
        stopped = False
       
        ' your loop
        For i = 1 To 20000000
            x = i + 1
            x = i - 1
            '...
           
            ' from time to time check if there is any input waiting
            If i Mod 100 = 0 Then
                If GetQueueStatus(QS_KEY) Then  ' key event is waiting?
                    DoEvents                    ' allow execution of event
                    If stopped Then Exit For
                End If
            End If
        Next
       
    End If
    Beep  ' done
    Command1.Caption = "Start task"
End Sub


If something is not clear, ask for clarification.
0
 

Author Comment

by:robertut
ID: 7044827
Hi!
I fixed the things this way:
instead of calling sleep(500) that locked my computer, i called sleep(10) 50 times in a for... next structure.
now it's ok, because I need a variable value of time.
Thx
Bye
0
 
LVL 4

Accepted Solution

by:
RichW earned 100 total points
ID: 7044956
Glad I could help with the Sleep API.
RichW
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now