Page 1 of 1

Pic to Excel Visual Basic

Posted: Tue Nov 08, 2011 10:57 am
by Desdewit
Hi

I'm not sure if I'm allowed to ask a Vb question on this forum but please see if you can help?
I've programmed a pic16f877 to send data to an excel spreadsheet via com port 1 on the pc
the communication is working and all the data are recieved in the spread sheet like expected.
The Protocol format is: # A A S D D D D D D D D P CR LF
where: # = indicates start of message
: AA = Instrument address. ASCII 00 to 99
: S = sign (polarity) (ASCII "+" or "-" ).
: D = data bits (data for 8 numerals).
: P = decimal point position. ASCII 0 to 8.
: CR = ASCII carriage return.
: LF = ASCII line feed.

How can I display only the polarity sign & the 8 digits from the string
& remove the leading zero’s if there are any.
It also shows a square where it receives the CR or LF how can I remove it.

Re: Pic to Excel Visual Basic

Posted: Tue Nov 08, 2011 12:59 pm
by dbasnett
Assuming VBA... You can use the MID function to get rid of the last two chars (CR LF), and also to get groups of characters. The leading zeros can be handled, without actually removing, by formatting the cell correctly.

Re: Pic to Excel Visual Basic

Posted: Tue Nov 08, 2011 3:03 pm
by Desdewit
Hi dbasnett

Thanx for the reply, I tried the Mid function but then I only recieve blank spaces.
please look at my code and see if you can help.
It's my first prog in Vb so I'm still learning

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 'get the rest of the data 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 and plot data into excel sheet.

Sheet1.Cells(22, 1) = "Test Complete"

End Sub

Re: Pic to Excel Visual Basic

Posted: Wed Nov 09, 2011 2:51 pm
by dbasnett
My VBA skills are rusty at best but after the data is in buffer(with CR LF)

buffer=mid(buffer,1,len(buffer)-2)

should get rid of CR LF.

The instrument address would be at mid(buffer,2,2) assuming that # is first character.

For VBA help see

http://social.msdn.microsoft.com/Forums ... ba/threads

Re: Pic to Excel Visual Basic

Posted: Wed Nov 09, 2011 7:01 pm
by Desdewit
Hi dbasnett

Thanks for your help. It worked like a charm.
I did play around with the mid function before but being a noob in VB I had the brackets all wrong. :oops: