Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Long term periodic serial port communications in VB6 consumes CPU time

Status
Not open for further replies.

Mictel

Technical User
Feb 5, 2008
4
0
0
US
Can VB6 poll the serial port say every hour without sucking up 40 to 50% of the CPU time looping continuously waiting for a 60 minute timer interval to occur? For example, is there a way for a VB6 routine to run in "sleep mode" more efficiently until it is time to do a short data transfer via serial port, and go back to sleep until the next one?

Also having a problem that the PC power management sleeps or hibernates while VB6 is running (looping) waiting to poll the serial port once per hour. Can VB6 prevent PC power management or hibernation while a routine is running? The routine (which communicats via serial port and writes data to a file) works just fine (other than the CPU usage above) if polling is done every 5 minutes.

Thanks,
Mike
 
If this is local to one PC, running the compiled EXE from a windows scheduled task may be an alternative.

I'm by no means sure on this one but sometimes a well placed DoEvents in loops can help reduce CPU usage to more acceptable levels. It may not apply in this case.
 
>Can VB6 poll the serial port say every hour without sucking up 40 to 50% of the CPU...

Can you post your current 'main loop' code...
 
Hugh said:
Can you post your current 'main loop' code...
Would be nice once in a while eh Hugh? [wink]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
>Can VB6 poll the serial port say every hour without sucking up 40 to 50%

Sure. B ut, as the previous posts indicate, we need to see your current code ...
 
Here is the code for the serial port polling/communications that only occurs once per hour. I deleted as much non-essential, repeated steps as possible to shorten up the routine. Please feel free to suggest improvements, like with serial communications handshaking. Thanks to all who responded.

'Scan all DAS ADC, DAC and Port values then display
Public Sub ScanDAS()

Dim Sample As Integer
Dim StartTime As Double
Dim SampleInterval As Double
Dim File_Name As String
Dim WakeUpTime As Single

'Get file name from user form
File_Name = Text21.Text

On Error GoTo FileError
' Open file data logger output
'Open "C:\PICDASout.txt" For Output As #1
Open File_Name For Output As #1


'send DAS version command
send_RS232 ("VE")
'read DAS response
Text1.Text = read_RS232
' Print PICDAS version information to the output file
Print #1, "Version = " & Text1.Text

' Print header to output file
Print #1, ""
Print #1, "Sample Date Time ADC0"

SampleInterval = Text19.Text * 60 ' Get Sample Interval in minutes from user form and convert to seconds

' Bound the number of samples to be written to file
For Sample = 1 To 1000

'Enable the DAS by aserting the DTR line high
MSComm1.DTREnable = True
'Wait one-half second for turn on
WakeUpTime = Timer
Do While WakeUpTime + 0.5 > Timer
'Let the DAS initialize
DoEvents
Loop

'Get DAS Configuration Information
'configure digital port pin function input or output
Call ConfigDigitalIO

'Wait one-half second for for communications to finish
WakeUpTime = Timer
Do While WakeUpTime + 0.5 > Timer
'Let the DAS finish communications
DoEvents
Loop

'send DAS Analog Input 0 command
send_RS232 ("ain 0")
'read DAS response and convert ADC counts to voltage
ain0.Text = Val(read_RS232()) / 1000

Print #1, Format$(Sample, "000000"); " "; Format$(Now, "mm/dd/yyyy hh:mm:ss "); Format$(ain0, "0.000")

'Disable the DAS by deaserting the DTR line low
MSComm1.DTREnable = False

'Wait until sample interval has passed before takeing the next sample (Tek-tip folks: this is where the majority of time is wasted burning up 50% CPU time for 1 hour when SampleInterval = 60 minutes)
Do While (Timer - StartTime < SampleInterval)
DoEvents 'Permits command buttons to be monitored while looping
Loop


Next Sample

FileError:
Close #1
End

End Sub
 
>without sucking up 40 to 50% of the CPU time looping continuously

Um, it's the looping code we need to see really
 
>once in a while

That sounds like a a probable answer to the OP Harley!
 
[smile]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
>Um, it's the looping code we need to see really

OKkkkky; how about the part of the code that says:

'Wait until sample interval has passed before takeing the next sample (Tek-tip folks: this is where the majority of time is wasted burning up 50% CPU time for 1 hour when SampleInterval = 60 minutes)
Do While (Timer - StartTime < SampleInterval)
DoEvents 'Permits command buttons to be monitored while looping
Loop

This is just fine for very short delays and does work in my application for a 1 hour delay but I would hope there is a more efficient way to suspend processing for long periods without consuming 50% of the CPU with a Do While Loop wasting computing resources for an hour, every hour just to do a few milliseconds of serial port communications. Would be desirable if VB could some how suspend execution during the intervals in between serial port communiations. Unfortunately, it is the VB routine that must poll the serial port evey hour for data transfer. The serial port will not initiate the need for communication so interrupts are out.
 
Who let you in here Chrissie? [tongue]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
I only read as far as

>Here is the code for the serial port polling/communications that only occurs once per hour

and assumed that meant you'd posted code that only ran once per hour and not the code that ran the delaying loop. Should have read further.

As chrissie has suggested, a Timer is probably the best solution for this.
 
[rofl]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
I will start reading up on the Timer Control. Thanks to those who actually contributed rather than just using up bandwidth.

Mike
 
<I will convert all of you guys to VB.Net addicts

Start with strongm.
 
I have a related question, will start new thread "Event vs. Polling"

Thanks,
Bently
 
To answer the original question, the answer given to you is quite correct. A timer will work nicely. However, if you want to include your routine in a class or module, then you can not use a timer (as far as I know) because there is no form to drop it on.

In this case, use 'Sleep'. See next thread for a sample. "Raise Event vs. Polling"

Regards,
Bently
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top