FUNCAO DELAY

USUARIO.EXCLUIDOS 30/03/2005 13:49:47
#75783
Gostaria de saber como faço para o programa fazer um Delay? Não é um Sleep, onde o programa inteiro para, mas sim apenas uma contagem de tempo para receber uma resposta via serial.

Ex.

Envio um comando para o meu dispositivo externo.

Se receber sem problemas ele me retorna um handshaking OK.

Mas preciso de um delay de 0.5 seg. para que se não obtiver uma resposta dentro de 500 milesegundos, eu possa abortar a função ou mandar novamente.

Grato
USUARIO.EXCLUIDOS 30/03/2005 14:14:19
#75787
Talvez, uma possível saída:
Crie uma variável global que informe se está esperando 5 segundos ou não.
Adicione um objeto timer com intervalo de 1000.
Quando necessário iniciar a espera, altere a variável global para true
Na sub do timer (objTimer_timer) faça algo parecido com isso:

Private Sub Timer1_Timer()
If Second(Now) Mod 5 <> 0 Then
VarGlobal = true
Else
VarGlobal = false
End If
End Sub

E o processo ficaria esperando a variável voltar para false. Quando voltasse, continuaria o processo...

Mais ou menos assim:

Dim contAux As Integer

Private Sub Timer1_Timer()
If Second(Now) Mod 5 <> 0 Then
contAux = contAux + 1
varGlobal = True
Else
contAux = 0
varGlobal = False
Timer1.Enabled = False
End If
End Sub


Private Sub SuaFuncao()
...
contAux = 1
Timer1.Enabled = true
varGlobal = True
While varGlobal
....
Wend
'Continua processo
...
End Sub


Espero ter ajudado
USUARIO.EXCLUIDOS 30/03/2005 14:18:19
#75789
Resposta escolhida
Punish...
ai vai

'//--------------------------------------------------------------------
'// PROPOSITO:
'// Dar Pause or delay em um PROCEDIMENTO especificando os segundos
'//
'// ARGUMENTS:
'// Number of seconds. May use fractions in a decimal format (#.##)
'//
'// COMMENTS:
'// Timer() returns a Single value rounded to the nearest 1/100 of a
'// second like a stopwatch. Also, Timer() has a "bug" - it resets
'// itself at midnight. Therefore we need to adjust for this, using
'// some sort of counter. The simplest way is to concatenate the day
'// in front of it with Day(Date) but then the days get reset when the
'// month changes, and of course we need to adjust when the months are
'// reset by the changing year. Fortunately that's as far as we have
'// to go. To avoid an extremely large number by concatenating one in
'// front of the other, we add the different parts of the Date together
'// and then concatenate with the sum.
'//--------------------------------------------------------------------
Public Sub EventPause(sngSeconds As Single)

Dim dblTotal As Double, dblDateCounter As Double, sngStart As Single
Dim dblReset As Double, sngTotalSecs As Single, intTemp As Integer
'// For our purposes, it's better to concatenate five zeros onto the
'// end of our date counter, then ADD any Timer values to it.
dblDateCounter = ((Year(Date) + Month(Date) + Day(Date)) _
& 0 & 0 & 0 & 0 & 0)
'// Initialize start time.
sngStart = Timer
'// We also need to adjust for the possible resetting of Timer()
'// (such as if the Time happens to be just before midnight) when
'// adding the Pause time onto the Start time. The folowing formula
'// takes ANY value of the total seconds, whether it's above or below
'// the 86400 limit, and converts it to a format compatible to the
'// date counter.
sngTotalSecs = (sngStart + sngSeconds)
intTemp = (sngTotalSecs \ 86400) '// Return the integer portion only
dblReset = (intTemp * 100000) + (sngTotalSecs - (intTemp * 86400))
'// Now we can initialize our total time.
dblTotal = dblDateCounter + dblReset

'// Timer loop
Do
DoEvents '// Make sure any other tasks get some attention
'// For this to work properly, we cannot create a variable with the
'// concatenated expression and plug it in unless we reset the variable
'// during the loop. Much better to do it like this:
Loop While (dblDateCounter + Timer) < dblTotal

End Sub


Tá um pouco comentando de mais...
Mas é o que vc precisa...
Tópico encerrado , respostas não são mais permitidas