giáo trình visual basic và pic phần 2 pps

13 305 0
giáo trình visual basic và pic phần 2 pps

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Visual Basic Parsing Strings Dim sText As String Dim sMidText As String sText = "Reynolds Electronics" sMidText = Mid$(sText, 3, 8) After running this code sMidText should equal "ynolds E", which starts at the 3rd letter and goes through the 11th (3+8=11). See how it works ? It should also be noted that if a length is not stated it will return all characters from the starting position to the end of the string, as you will see below Putting It All Together Now for some real fun ! Lets say that you are receiving data from a Basic Stamp. This data is stored in a buffer called sBuffer. Each set of data is separated by an ASCII 13. You want to be able to separate each of these sets of data one by one and display them in the debug window. Here goes Dim sBuffer As String Dim lEnd As Long Dim sData As String ' This is the data in your buffer sBuffer = "Data1" & Chr$(13) & "Data2" & Chr$(13) & "Data3" & Chr $(13) & "Data4" & Chr$(13) lEnd = InStr(sBuffer, Chr$(13)) ' Gets Starting position of ASCII 13 Do ' Starts the loop If lEnd > 0 Then ' If > 0 then we have an ASCII 13 in the buffer sData = Left$(sBuffer, lEnd - 1) ' sData will be all characters before lEnd sBuffer = Mid$(sBuffer, lEnd + 1) ' We want to delete the data that we just got from the buffer including the ASCII 13 Debug.Print sData ' Display the data lEnd = InStr(sBuffer, Chr$(13)) ' Gets Starting position of ASCII 13 in the new buffer End If Loop While lEnd > 0 ' Loop while ASCII 13 is still present in the buffer After running this code you should see Data1 through Data4 show up in your Debug Window. http://www.rentron.com/parsingstrings.htm (4 of 5)5/25/2004 8:47:51 PM Visual Basic Parsing Strings String manipulation is not really that hard. With the use of theses four functions you can parse any string that you want to. It just takes some planning as to how the data will be presented to you, and how you should go about getting the data that you want out of the big string. ;-) << Data Types | Intro | Advanced Parsing >> Copyright © 1999-2001 Reynolds Electronics | Contact Information | Reynolds Electronics 3101 Eastridge Lane Canon City, Co. 81212 Voice: (719) 269-3469 Fax: (719) 276-2853 http://www.rentron.com/parsingstrings.htm (5 of 5)5/25/2004 8:47:51 PM Advanced Parsing With Visual Basic "Micro-News" 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 http://www.rentron.com/adv_parsing.htm (1 of 11)5/25/2004 8:48:05 PM Advanced Parsing With Visual Basic 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 http://www.rentron.com/adv_parsing.htm (2 of 11)5/25/2004 8:48:05 PM Advanced Parsing With Visual Basic 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. http://www.rentron.com/adv_parsing.htm (3 of 11)5/25/2004 8:48:05 PM Advanced Parsing With Visual Basic 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 http://www.rentron.com/adv_parsing.htm (4 of 11)5/25/2004 8:48:05 PM Advanced Parsing With Visual Basic ' 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 http://www.rentron.com/adv_parsing.htm (5 of 11)5/25/2004 8:48:05 PM Advanced Parsing With Visual Basic 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. http://www.rentron.com/adv_parsing.htm (6 of 11)5/25/2004 8:48:05 PM Advanced Parsing With Visual Basic 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 http://www.rentron.com/adv_parsing.htm (7 of 11)5/25/2004 8:48:05 PM Advanced Parsing With Visual Basic 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 http://www.rentron.com/adv_parsing.htm (8 of 11)5/25/2004 8:48:05 PM [...]... Data to a Microcontroller >> | Copyright © 1999 -20 01 Reynolds Electronics | Contact Information | Reynolds Electronics 3101 Eastridge Lane Canon City, Co 8 121 2 Voice: (719) 26 9-3469 Fax: (719) 27 6 -28 53 http://www.rentron.com/adv_parsing.htm (10 of 11)5 /25 /20 04 8:48:05 PM Advanced Parsing With Visual Basic http://www.rentron.com/adv_parsing.htm (11 of 11)5 /25 /20 04 8:48:05 PM ... ' 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 http://www.rentron.com/adv_parsing.htm (9 of 11)5 /25 /20 04 8:48:05 PM Advanced Parsing With Visual Basic I know that is...Advanced Parsing With Visual Basic ' 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... 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 ! . sPBASIC and sSubs ' and show them in txtPBASIC txtPBASIC = sPBASIC & sSubs End Sub http://www.rentron.com/adv_parsing.htm (9 of 11)5 /25 /20 04 8:48:05 PM Advanced Parsing With Visual Basic I. 11)5 /25 /20 04 8:48:05 PM Advanced Parsing With Visual Basic This could be transformed into PBASIC code by doing the following If [Condition1=True] Then DoCode1 If [Condition2=True] Then DoCode2 DoCode#3 EndIf: '. Copyright © 1999 -20 01 Reynolds Electronics | Contact Information | Reynolds Electronics 3101 Eastridge Lane Canon City, Co. 8 121 2 Voice: (719) 26 9-3469 Fax: (719) 27 6 -28 53 http://www.rentron.com/parsingstrings.htm

Ngày đăng: 22/07/2014, 16:21

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan