HKComputer
asked on
DoEvents, CPU Time, MsComm, and (Timer, Pause, Sleep, Wait)
When using the MsComm control, it's often necessary to send commands to the modem and then wait for a response.
I'm really really confused. In order to better manage my code, I'm using functions and sub programs that get called from here and there. (Sounds like managed code?) But sometimes the results are most unexpected. The problem is that you must pause to wait for responses from the modem, but while pausing, you must accept OnComm events and handle them. After the pause is over, my code checks to the state of some variables that get set during OnComm events. Am I making sense? So, yes, we need to pause but continue to handle OnComm events. But I don't want any other functions runnning away.
Here's the code I'm using. It is working but the CPU does run high. And I'm not really understanding where I should use DoEvents and where I shouldn't. Does DoEvents ever need to be run more than one time inside a single procedure?
'Should I have DoEvents up here?
comm1.Output = "AT+VCID=1" + vbCr
subPause (1)
Private Sub subPause(lngHowLong As Long)
Dim lngStart As Long
lngStart = Timer
DoEvents
Do While Timer < lngStart + lngHowLong
Loop
End Sub
I'm really really confused. In order to better manage my code, I'm using functions and sub programs that get called from here and there. (Sounds like managed code?) But sometimes the results are most unexpected. The problem is that you must pause to wait for responses from the modem, but while pausing, you must accept OnComm events and handle them. After the pause is over, my code checks to the state of some variables that get set during OnComm events. Am I making sense? So, yes, we need to pause but continue to handle OnComm events. But I don't want any other functions runnning away.
Here's the code I'm using. It is working but the CPU does run high. And I'm not really understanding where I should use DoEvents and where I shouldn't. Does DoEvents ever need to be run more than one time inside a single procedure?
'Should I have DoEvents up here?
comm1.Output = "AT+VCID=1" + vbCr
subPause (1)
Private Sub subPause(lngHowLong As Long)
Dim lngStart As Long
lngStart = Timer
DoEvents
Do While Timer < lngStart + lngHowLong
Loop
End Sub
the Sleep api works much better than an internal VBA loop
http://www.mvps.org/access/api/api0021.htm
http://www.mvps.org/access/api/api0021.htm
ASKER
I've tried using the Sleep API. But there's something I don't really understand.
comm1.Output = "AT+VCID=1" + vbCr
Sleep (1000)
If bleOK = True Then bleCallerID = True
bleOK will get set to tru if the AT+VCID=1 command returns true from the modem. But in order for this to happen, my comm1.OnComm event must fire and be able to call my ProcessEvent function to read the event data and set variables to true or false accordingly. But in my tests, when I ran Sleep, my modem events failed to fire until Sleep was finished. At this point, bleOK was always false even if the modem returned an "OK".
Am I missing something here?
comm1.Output = "AT+VCID=1" + vbCr
Sleep (1000)
If bleOK = True Then bleCallerID = True
bleOK will get set to tru if the AT+VCID=1 command returns true from the modem. But in order for this to happen, my comm1.OnComm event must fire and be able to call my ProcessEvent function to read the event data and set variables to true or false accordingly. But in my tests, when I ran Sleep, my modem events failed to fire until Sleep was finished. At this point, bleOK was always false even if the modem returned an "OK".
Am I missing something here?
I've never worked with modems...you've exceeded my knowledge base.
Mike
Mike
ASKER
I stopped using the sleep function because it didn't work as I needed. It seemed that the Sleep function kept my modem events from ocurring.
I wrote my own pause function. Please see:
https://www.experts-exchange.com/questions/22050442/Trying-to-understand-Timer-in-VBA-Code.html
I'm going to request a close and refund unless there are any objections. -HK
I wrote my own pause function. Please see:
https://www.experts-exchange.com/questions/22050442/Trying-to-understand-Timer-in-VBA-Code.html
I'm going to request a close and refund unless there are any objections. -HK
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Do While Timer < lngStart + lngHowLong
DoEvents
Loop
Mike