Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

timebeginperiod, sleep and just no 1 ms

0 views
Skip to first unread message

Sven Peeze Binkhorst

unread,
Nov 29, 2004, 7:21:22 PM11/29/04
to
What's wrong with this code? It's for VB6.

I should think there would be a 500Hz square wave on the LPT port.
However, the frequency is only about half of that, which means
Sleep(1) is more like Sleep(2).

I've been reading through several threads and they all claim
timeBeginPeriod(1) will make Sleep(1) a true Sleep(1).

But it doesn't.

Is there a way to solve this?

Thanks!

Sven

'-----------------------------------------------------------------------
' in module
'-----------------------------------------------------------------------

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal uPeriod As
Integer) As Integer
Declare Function timeEndPeriod Lib "winmm.dll" (ByVal uPeriod As
Integer) As Integer

'-----------------------------------------------------------------------
' in form
'-----------------------------------------------------------------------

Private Sub Form_Paint()

Dim k As Long

Call timeBeginPeriod(1)

For k = 0 To 1000
Call LPT_DataReg_Pin_High(BIT0)
Call Sleep(1)
Call LPT_DataReg_Pin_Low(BIT0)
Call Sleep(1)
Next k

Call timeEndPeriod(1)

End Sub

Craig Kelly

unread,
Nov 29, 2004, 8:34:32 PM11/29/04
to
"Sven Peeze Binkhorst" wrote:

> What's wrong with this code? It's for VB6.
>
> I should think there would be a 500Hz square wave on the LPT port.
> However, the frequency is only about half of that, which means
> Sleep(1) is more like Sleep(2).
>
> I've been reading through several threads and they all claim
> timeBeginPeriod(1) will make Sleep(1) a true Sleep(1).
>
> But it doesn't.
>
> Is there a way to solve this?
>
> Thanks!
>
> Sven

<code snipped>

Sven,

Sleep() isn't guaranteed to return after the specified number of
milliseconds: you're giving up your current time slice and so Sleep(1) is
pretty much guaranteed to wait at least one millisecond but probably more.
You can use timeGetTime with the timeXXXPeriod functions and busy loop. For
finer resolution, check out the QueryPerformanceCounter and
QueryPerformanceFrequency functions.

Craig


Arkady Frenkel

unread,
Nov 30, 2004, 9:28:52 AM11/30/04
to
Check what timeBeginPeriod(1) returned
Arkady


"Sven Peeze Binkhorst" <sven...@hotmail.com> wrote in message
news:6ea3ba4.04112...@posting.google.com...


> What's wrong with this code? It's for VB6.
>
> I should think there would be a 500Hz square wave on the LPT port.
> However, the frequency is only about half of that, which means
> Sleep(1) is more like Sleep(2).
>
> I've been reading through several threads and they all claim

> cwill make Sleep(1) a true Sleep(1).

Sven Peeze Binkhorst

unread,
Dec 1, 2004, 6:50:29 PM12/1/04
to
"Craig Kelly" <cnkelly...@nospam.net> wrote in message news:<I6Qqd.1000669$Gx4.9...@bgtnsc04-news.ops.worldnet.att.net>...

> Sven,
>
> Sleep() isn't guaranteed to return after the specified number of
> milliseconds: you're giving up your current time slice and so Sleep(1) is
> pretty much guaranteed to wait at least one millisecond but probably more.
> You can use timeGetTime with the timeXXXPeriod functions and busy loop. For
> finer resolution, check out the QueryPerformanceCounter and
> QueryPerformanceFrequency functions.
>
> Craig

OK, thanks. Too bad...

Anyway, QueryPerformanceX uses a 64 bit var and that's not easy to handle in VB.

"Arkady Frenkel" <arkadyf@hotmailxdotxcom> wrote in message news:<OZ2mWmu1...@TK2MSFTNGP10.phx.gbl>...


> Check what timeBeginPeriod(1) returned
> Arkady

The returnvalue (in a MsgBox) is '0', I think that means no err.

Arkady Frenkel

unread,
Dec 2, 2004, 5:18:10 PM12/2/04
to
Really because granularity of system is 10 ms , Sleep cant give less ,
opposite to WinCE where granularity is 1 msec so Sleep can return after 1-2
msec , but with timeBeginPeriod(1) you reprogramming 10 to 1 so that have to
wait 1-2 msec
Arkady

"Sven Peeze Binkhorst" <sven...@hotmail.com> wrote in message

news:6ea3ba4.04120...@posting.google.com...

Craig Kelly

unread,
Dec 2, 2004, 11:35:17 PM12/2/04
to
"Sven Peeze Binkhorst" wrote:

> Anyway, QueryPerformanceX uses a 64 bit var and that's not
> easy to handle in VB.

It's a hack and I don't like it, but you can use Currency in place of
LARGE_INTEGER with VB 6...


Option Explicit

Private Declare Function QueryPerformanceCounter _
Lib "kernel32" ( _
ByRef perfCount As Currency) _
As Long

Private Declare Function QueryPerformanceFrequency _
Lib "kernel32" ( _
ByRef perfFreq As Currency) _
As Long

Private Declare Sub Sleep _
Lib "kernel32" ( _
ByVal ms As Long)

Public Sub Main()
Dim b As Currency
Dim e As Currency
Dim start As Currency
Dim n As Currency

'"Prime" the frequency check
TimeDiffMS 1, 1

CheckAPI QueryPerformanceCounter(b)
Sleep 55
CheckAPI QueryPerformanceCounter(e)
MsgBox "Sleep(55) = " & TimeDiffMS(b, e) & " ms"

CheckAPI QueryPerformanceCounter(b)
Sleep 1
CheckAPI QueryPerformanceCounter(e)
MsgBox "Sleep(1) = " & TimeDiffMS(b, e) & " ms"

CheckAPI QueryPerformanceCounter(b)
QueryPerformanceCounter start
QueryPerformanceCounter n
Do While TimeDiffMS(start, n) < 1@
QueryPerformanceCounter n
Loop
CheckAPI QueryPerformanceCounter(e)
MsgBox "Busy Loop for 1ms = " & TimeDiffMS(b, e) & " ms"
End Sub

Private Function TimeDiffMS( _
ByVal t1 As Currency, _
ByVal t2 As Currency) _
As Currency

Static freq As Currency
Static adj As Currency

Dim a1 As Currency
Dim a2 As Currency

If freq = 0@ Then
CheckAPI QueryPerformanceFrequency(freq)
freq = freq / 1000@

CheckAPI QueryPerformanceCounter(a1)
CheckAPI QueryPerformanceCounter(a2)
adj = a2 - a1
End If

TimeDiffMS = ((t2 - t1) - adj) / freq
End Function

Private Sub CheckAPI(ByVal lngRes As Long)
If lngRes = 0 Then
Err.Raise vbObjectError, "", "API Error: " & Err.LastDllError
End If
End Sub

0 new messages