Solved

Still Need Microsecons...

Posted on 2002-05-17
12
205 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
[X]
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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

717 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