7,763
社区成员
发帖
与我相关
我的任务
分享
Call DataSend(&H67, MSComm1)
Call Sleep(t67 * 1000)
Call DataSend(&H6A, MSComm1)
Call Sleep(t6a * 1000)
Call DataSend(&H6C, MSComm1)
Call Sleep(t6c * 1000)
Call DataSend(&H67, MSComm1)
Call Sleep(t67 * 1000)
执行到这里面来了,想退出这里面就不行,一直要等这4个sleep完了,才能退出,有什么其他的办法或者代替这个sleep?
Option Explicit
'执行command控件开始执行Timer5
Private Sub Timer5_Timer()
Timer5.Enabled = False
Dim currentdate As Date
Dim currentdate1 As Date
currentdate = Now
currentdate1 = currentdate
Do
Do
If Not isStarted Then
Exit Do
Else
Call DataSend(&H66, MSComm1)
Call subDelay(t66 * 1000)
Call DataSend(&H7E, MSComm1)
Call subDelay(t7e * 1000)
End If
Loop While Not DateDiff("s", currentdate, Now) > lLoopTime * 60
If Not isStarted Then
Exit Do
Else
Call DataSend(&H67, MSComm1)
Call subDelay(t67 * 1000)
Call DataSend(&H6A, MSComm1)
Call subDelay(t6a * 1000)
Call DataSend(&H6C, MSComm1)
Call subDelay(t6c * 1000)
Call DataSend(&H67, MSComm1)
Call subDelay(t67 * 1000)
currentdate = Now
End If
Loop While Not DateDiff("s", currentdate1, Now) > mLoopTime * 60
isStarted = False
Call EndSend '执行结束程序,发送结束协议
End Sub
Public Sub EndSend()
Call DataSend(&H7E, MSComm1) '结束协议,直接结束
End Sub
subDelay过程,参见我在10楼的代码,把它添加到一个标准模块中
'执行command控件开始执行Timer5
Private Sub Timer5_Timer()
Timer5.Enabled = False
Dim currentdate As Date
Dim currentdate1 As Date
currentdate = Now
currentdate1 = currentdate
Do
Do
If Not isStarted Then
Exit Do
Else
Call DataSend(&H66, MSComm1)
DoEvents
Call Sleep(t66 * 1000)
Call DataSend(&H7E, MSComm1)
DoEvents
Call Sleep(t7e * 1000)
End If
DoEvents
Loop While Not DateDiff("s", currentdate, Now) > lLoopTime * 60
If Not isStarted Then
Exit Do
Else
Call DataSend(&H67, MSComm1)
'默认设置timer6,tiemr7,tiemr8都是false
timer6.enable=true
Call DataSend(&H6A, MSComm1)
tiemr7.enable=true
Call DataSend(&H6C, MSComm1)
tiemr8.enable=true
Call DataSend(&H67, MSComm1)
tiemr9.enable=true
currentdate = Now
End If
DoEvents
Loop While Not DateDiff("s", currentdate1, Now) > mLoopTime * 60
isStarted = False
Call EndSend '执行结束程序,发送结束协议
End Sub
Public Sub EndSend()
Call DataSend(&H7E, MSComm1)'结束协议,直接结束
End Sub
'在from_load里写好timer6和其他的timer的频率,timer6.inteval=10000 ‘10秒
'以下手写代码 不规范
private sub timer6_timer
timer6.enable=false
end sub
'执行command控件开始执行Timer5
Private Sub Timer5_Timer()
Timer5.Enabled = False
Dim currentdate As Date
Dim currentdate1 As Date
currentdate = Now
currentdate1 = currentdate
Do
Do
If Not isStarted Then
Exit Do
Else
Call DataSend(&H66, MSComm1)
DoEvents
Call Sleep(t66 * 1000)
Call DataSend(&H7E, MSComm1)
DoEvents
Call Sleep(t7e * 1000)
End If
DoEvents
Loop While Not DateDiff("s", currentdate, Now) > lLoopTime * 60
If Not isStarted Then
Exit Do
Else
Call DataSend(&H67, MSComm1)
DoEvents
Call Sleep(t67 * 1000)
Call DataSend(&H6A, MSComm1)
DoEvents
Call Sleep(t6a * 1000)
Call DataSend(&H6C, MSComm1)
DoEvents
Call Sleep(t6c * 1000)
Call DataSend(&H67, MSComm1)
DoEvents
Call Sleep(t67 * 1000)
currentdate = Now
End If
DoEvents
Loop While Not DateDiff("s", currentdate1, Now) > mLoopTime * 60
isStarted = False
Call EndSend '执行结束程序,发送结束协议
End Sub
Public Sub EndSend()
Call DataSend(&H7E, MSComm1)'结束协议,直接结束
End Sub
Private Sub Timer5_Timer()
Timer5.Enabled = False
Dim currentdate As Date
Dim currentdate1 As Date
currentdate = Now
currentdate1 = currentdate
Do
Do
If Not isStarted Then
Exit Do
Else
Call DataSend(&H66, MSComm1)
If Delay(t66 * 1000) Then Call DataSend(&H7E, MSComm1) '第二句开始,就判断返回值.
bExtiDelay = True
Call Delay(t7e * 1000)
End If
Loop While Not DateDiff("s", currentdate, Now) > lLoopTime * 60
If Not isStarted Then
Exit Do
Else
Call DataSend(&H67, MSComm1)
'Call Delay(t67 * 1000)
If Delay(t67 * 1000) Then Call DataSend(&H6A, MSComm1)
bExtiDelay = True
'Call Delay(t6a * 1000)
If Delay(t6a * 1000) Then Call DataSend(&H6C, MSComm1)
bExtiDelay = False
'Call Delay(t6c * 1000)
If Delay(t6c * 1000) Then Call DataSend(&H67, MSComm1)
bExtiDelay = True
Call Delay(t67 * 1000)
currentdate = Now
End If
Loop While Not DateDiff("s", currentdate1, Now) > mLoopTime * 60
isStarted = False
Call EndSend '执行结束程序,发送结束协议
End Sub
dim bExtiDelay as boolean
private function Delay(byval lTime as long) as boolean
dim I as long
i=gettickcount
do
sleep 1
doevents
if (bExtiDelay) or (i+ltime<gettickcount) then
Delay=bExtiDelay
exit do
end if
loop
end function
Call DataSend(&H66, MSComm1) '第一句SEND,不作判断
if not Delay(3000) then Call DataSend(&H7E, MSComm1) '第二句开始,就判断返回值.
Option Explicit
Public Declare Function GetTickCount Lib "kernel32" () As Long
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'过程功能:延迟指定时间,单位毫秒
'参数说明:lngDelay:要延迟的毫秒数
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Public Sub subDelay(ByVal lngDelay As Long)
Dim lngS As Long
On Error GoTo errSub
If lngDelay <= 0 Then Exit Sub
lngS = GetTickCount
Do
DoEvents
Loop Until GetTickCount - lngS >= lngDelay
Exit Sub
errSub:
End Sub
dim bExtiDelay as boolean
private sub Delay(byval lTime as long)
dim I as long
i=gettickcount
do
sleep 1
doevents
if (bExtiDelay) or (i+ltime<gettickcount) then exit do
loop
bExtiDelay=false
end sub
'开始
Private Sub Timer5_Timer()
Timer5.Enabled = False
Dim currentdate As Date
Dim currentdate1 As Date
currentdate = Now
currentdate1 = currentdate
Do
Do
If Not isStarted Then
Exit Do
Else
Call DataSend(&H66, MSComm1)
DoEvents
Call Sleep(t66 * 1000)
Call DataSend(&H7E, MSComm1)
DoEvents
Call Sleep(t7e * 1000)
End If
DoEvents
Loop While Not DateDiff("s", currentdate, Now) > lLoopTime * 60
If Not isStarted Then
Exit Do
Else
Call DataSend(&H67, MSComm1)
DoEvents
Call Sleep(t67 * 1000)
Call DataSend(&H6A, MSComm1)
DoEvents
Call Sleep(t6a * 1000)
Call DataSend(&H6C, MSComm1)
DoEvents
Call Sleep(t6c * 1000)
Call DataSend(&H67, MSComm1)
DoEvents
Call Sleep(t67 * 1000)
currentdate = Now
End If
DoEvents
Loop While Not DateDiff("s", currentdate1, Now) > mLoopTime * 60
isStarted = False
Call EndSend '执行结束程序
End Sub
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Sub delay(dt as long)
Dim Savetime As Double
' "timeGetTime begin"
Savetime = timeGetTime '记下开始时的时间
While timeGetTime < Savetime + dt '循环等待
DoEvents '转让控制权,以便让操作系统处理其它的事件。
Wend
' "timeGetTime end"
End Sub