Solved

Still Need Microsecons...

Posted on 2002-05-17
12
194 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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
 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 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