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
> 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
"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).
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.
"Sven Peeze Binkhorst" <sven...@hotmail.com> wrote in message
news:6ea3ba4.04120...@posting.google.com...
> 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