Pic to excel VBA

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Desdewit
Posts: 130
Joined: Sat Feb 26, 2011 4:26 am
Location: South Africa
Has thanked: 25 times
Been thanked: 11 times
Contact:

Pic to excel VBA

Post by Desdewit »

Hi

I wrote a program to log data from a pic into excel. The program works fine but uses a lot of cpu power (50%) on small pc's when looping for new data.
what can I change to use less cpu power when waiting for data from the com port?

Code: Select all

 
Private Sub CommandButton3_Click()

Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
Dim lngStatus As Long
Dim strData As String
Dim out_of_time As Boolean
Dim time As Single
Dim buffer As String
Dim i As Integer 
For i = 1 To 20
buffer = ""
time = Timer() 
intPortID = 1

Do
lngStatus = CommRead(intPortID, strData, 1) 
Loop Until strData = "#" 'Read port until start bit is found.

Do
lngStatus = CommRead(intPortID, strData, 16)
buffer = buffer & strData
out_of_time = Timer() > time + 10
Loop Until strData = Chr(13) Or out_of_time     Loop until end character is found.
If out_of_time <> False Then
buffer = "Out Of Time"
End If

Sheet1.Cells(i, 1) = buffer
Next i ' take 20 readings from com port and plot data into excel sheet.

Sheet1.Cells(22, 1) = "Test Complete"   'Write test complete in A22

End Sub

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Pic to excel VBA

Post by JonnyW »

Ive never used VB in an Excel spreadsheet but VB6 had a 'DoEvents' that might help:

Code: Select all

Do
  lngStatus = CommRead(intPortID, strData, 1)
  DoEvents
Loop Until strData = "#" 'Read port until start bit is found.
This might be of use?

Jonny

Post Reply