BASIC Stamp, Microchip PIC, 8051, and Remote Control Projects

  BASIC Stamp, Microchip PIC, 8051, and Remote Control Mailing-List"Micro-News"BASIC Stamp, Microchip PIC, 8051, and Remote Control Mailing-List
  Micro-Mailing-List

Understanding and Using Visual Basic Part 4
By: Jared Hoylman -

Advanced String Parsing

In the last article we showed the four major string parsing functions along with a few simple examples.  In this article we are going to kill two birds with one stone.  PBASIC is not very friendly when it comes to conditional statements.  Without the If...ElseIf...Else...EndIf kind of conditional statements, beginners find it difficult to program a Basic Stamp to do exactly what they want.  If the PBASIC language had the If...ElseIf...Else...EndIf kind of conditional statements I believe it would be much easier for beginners to learn and program the Basic Stamp.  So in this article we are going to use our VB string parsing functions to make a complete application that will manipulate true If...ElseIf...Else...EndIf VB style conditionals into functioning PBASIC code..!

Lets take for example a VB type If conditional...

If [Condition1=True] Then
  DoCode#1
ElseIf [Condition2=True] Then
  DoCode#2
Else
  DoCode#3
End If

This could be transformed into PBASIC code by doing the following...

If [Condition1=True] Then DoCode1
If [Condition2=True] Then DoCode2
DoCode#3
EndIf:

' End of Main Program

' Start Extra Subs
DoCode1:
  DoCode#1
Goto EndIf

DoCode2:
  DoCode#2
Goto EndIf

This code would flow the exact same way as the VB typeIf conditional, only it looks a little more complex...

Creating The VB Program

Now you need to open up a New Standard EXE project. Add two labels (Label1 and Label2), two Textboxes (txtVB and txtPBASIC), and a command button (cmdConvert) to your form. Your form should look like the one below...

Now we need to set a few properties before we continue...

txtVB and txtPBASIC
Multiline = True
ScrollBars = 2 - Vertical
Text = "" ("" means nothing)

cmdConvert
Caption = "Convert"


And you can change the Label captions to whatever you wish.

How To Go About It

OK. The easiest way that I can think of doing it would be to loop through every line one at a time in the VB type code and convert it to the PBASIC type code. So we need a function to extract a specific line from txtVB. I went ahead and wrote one for you. It is a little confusing if you are not experienced in VB programming so I added LOTS of comments to help you out. Add This code to your form.

Private Function GetLineText(txtBox As TextBox, _
                        lLine As Long ) As String
Dim x As Long
Dim sText As String     ' Holds Textbox Text
Dim lLineStart As Long  ' Chr That Begins Line
Dim lLineEnd As Long     ' Chr That Ends Line
Dim lLength As Long      ' Length of line
sText = txtBox.Text

' We need to make sure that the text ends in a
' vbCrlf so...

If Right$(sText, 2) <> vbCrLf Then
    sText = sText & vbCrLf
End If

' If you want the first line of the textbox you
' know that the first character of the line
' will be the first character of the TextBox.

If lLine = 1 Then
    lLineStart = 1
Else

' If it isn't line 1 then we must find the first
' character of the line.  We know that each line
' is seperated by a vbCrLf (carriage return and
' line feed). So to find the second line starting
' position we find the 1st vbCrLf.  And to find
' the end of the second line we find the 3rd
' vbCrLf.

' This next little bit of code finds each vbCrlf
' up to (lLine - 1) which is the one that we need.

    lLineStart = 1  ' Initialize Offset
    For x = 1 To lLine - 1
    lLineStart = InStr(lLineStart, sText, vbCrLf)

    ' Compensate for the 2 characters in vbCrLf
    lLineStart = lLineStart + 2
    Next x
End If

' Now we need to find the end of the line.  We
' know that it is the very next vbCrLf after
' lLineStart, so...

lLineEnd = InStr(lLineStart, sText, vbCrLf)

' Get Line Length
lLength = lLineEnd - lLineStart
    
' Now we have the starting and ending characters
' for the line that we are trying to find.  Do
' you remember the Mid$ statement from the
' previous article..?

GetLineText = Mid$(sText, lLineStart, lLength)
End Function

OK. Now with that out of the way we need to discuss how we are going to convert the VB code to PBASIC code. Lets take the example code that I gave above.

If [Condition1=True] Then
  DoCode#1
ElseIf [Condition2=True] Then
  DoCode#2
Else
  DoCode#3
End If

And the PBASIC equivalent...

If [Condition1=True] Then DoCode1
If [Condition2=True] Then DoCode2
DoCode#3
EndIf:

' End of Main Program

' Start Extra Subs
DoCode1:
  DoCode#1
Goto EndIf

DoCode2:
  DoCode#2
Goto EndIf

Really it's not that difficult to do. If a line is like If [condition] Then or ElseIf [condition] Then we simple copy the line and add a Label to the end of the line. All of the code in between these statements gets put into a subroutine at the end of the program. If we find that a line equals Else then we simply copy the rest of the lines of code as they are. And finally if a line equals End If then we convert it to the Label EndIf:.

But first we need to go over another very useful string operator. It is the Like operator. The Like operator is used to compare two strings, usually using wildcards. So lets see an example...

Dim bResult As Boolean
bResult = "If X=1 Then" Like "If * Then"

In this example bResult would equal True because the wildcard * can be any number of characters, so the pattern matches.

So with that out of the way here is the core part of the program. Again I added TONS of comments.

Private Sub cmdConvert_Click()
Dim sLine As String     ' Holds the line text
Dim sPBASIC As String   ' Hold converted string
Dim sSubs As String     ' Hold subroutines
Dim lLabelCount As Long ' Unique label counter
Dim lCurrentLine As Long     ' Current Line
Dim sCurrentLabelText As String
Dim bSubInitialized As Boolean
Dim bMakingElse As Boolean
Dim lSubsAdded As Long  ' Counts subs added

' We will simply loop through every line of txtVB
' until we find a line that equals "End If"

lCurrentLine = 0    ' Initialize line counter
lLabelCount = 0     ' Initialize the label count
Do
lCurrentLine = lCurrentLine + 1 ' Get next line
' Get current line text
sLine = GetLineText(txtVB, lCurrentLine)

' Convert line to lowercase for comparison reasons
sLine = LCase(sLine)

' remove all leading and trailing spaces
sLine = Trim(sLine)

If sLine Like "if * then" Then
    
    
    ' we have a line like "If [Condition} Then
    
    ' Increment LabelCount and set label name
    lLabelCount = lLabelCount + 1
    sCurrentLabelText = "Label" & lLabelCount
    
    ' Add the line
    sPBASIC = sPBASIC & sLine
    
    ' Then add the label and a vbCrLf
    sPBASIC = sPBASIC & " " & sCurrentLabelText
    sPBASIC = sPBASIC & vbCrLf
    
    ' We are now making a subroutine
    bSubInitialized = False
    
ElseIf sLine Like "elseif * then" Then

    
    ' we have a line like "ElseIf [Condition} Then
    ' we also need to take off the first 4 letters
    ' of the line to be vaid PBASIC
    
    sLine = Right$(sLine, Len(sLine) - 4)
    
    ' Increment LabelCount and set label name
    lLabelCount = lLabelCount + 1
    sCurrentLabelText = "Label" & lLabelCount
    
    ' Add the line
    sPBASIC = sPBASIC & sLine
    
    ' Then add the label and a vbCrLf
    sPBASIC = sPBASIC & " " & sCurrentLabelText
    sPBASIC = sPBASIC & vbCrLf
    
    ' We are now making a subroutine
    bSubInitialized = False
    
ElseIf sLine = "else" Then
    ' we are now making the Else Part
    ' set the flag = True
    bMakingElse = True
    
ElseIf sLine = "end if" Then
    ' simply add the "EndIf:" label
    sPBASIC = sPBASIC & "EndIf:" & vbCrLf
    
    ' we are done so exit the loop
    Exit Do
    
ElseIf bMakingElse = True Then

    ' simply copy the lines
    sPBASIC = sPBASIC & sLine & vbCrLf

ElseIf bSubInitialized = False Then
    bSubInitialized = True
    
' if this is not the first sub then we need to
' end the previous sub

    If lSubsAdded > 0 Then
        sSubs = sSubs & "Goto EndIf" & vbCrLf
        sSubs = sSubs & vbCrLf
    End If

    ' Increment counter
    lSubsAdded = lSubsAdded + 1
    
' Now add the new sub name and the current line

    sSubs = sSubs & sCurrentLabelText & ":"
    sSubs = sSubs & vbCrLf
    sSubs = sSubs & sLine & vbCrLf
    
Else
    ' none of the other criteria above were met so
    ' we must be adding the line to the end of the
    ' Subroutines
    
    sSubs = sSubs & sLine & vbCrLf
End If

Loop     ' Do next line

' We found the last line and exited the loop
' so now we must END the program and finish up
' the subroutines

sPBASIC = sPBASIC & "End" & vbCrLf & vbCrLf

If lSubsAdded > 0 Then
    ' subs were added so we need to end them
    sSubs = sSubs & "Goto EndIf" & vbCrLf
End If

' Now we just have to combine sPBASIC and sSubs
' and show them in txtPBASIC

txtPBASIC = sPBASIC & sSubs

End Sub

I know that is really long, but if you take your time and go through the code line by line it is not that hard to figure out the concept that is being used. If you just can't figure it out or just want to download the project already made...then click here.

Be sure to try it out as it might make programming you Basic Stamp just a little bit easier. And if you study this code and fully understand it, you are on your way to becoming a very good VB programmer. ;-)

Limitations

This code does have some limitations..!

- You can not nest If blocks
- There is no error checking. Try typing "End If" as "EndIf" and see what happens.
- There is no syntax checking. You can type anything you want in between the "If" lines and it will copy them anyways.

Anyways I hope that you learn something from this article. There are a lot of neat little tricks in this code that took me years to figure out. Learn from someone that has learned the hard way and make it easy on your self. ;-)

| << Parsing Strings | Intro | Sending Data to a Microcontroller >> |

 

Copyright © 1999-2007
Reynolds Electronics

| Contact Information |