Advanced String Parsing

mach278

Newcomer
Joined
Jun 9, 2003
Messages
4
Hey guys, I was wondering if you could provide any tips for the following code I want to write. Basically, I'm building a serial interface with a microcontroller. The interface will read strings from the microcontroller and place the value in the appropriate status box. For example, the microcontroller will send 'A=100' telling me that 100 goes in the 'Amp' textbox. Right now, my code (located below) is capable of sending the right value to the right textbox, but only one string at a time. While values aren't going to be sent constantly, there may be times when the buffer may have more than one string. Say the buffer has "A=100, V=12, P=T, A=121, V=10, ....". How would I read the buffer and separate all the values between the commas? I would then need the separated terms to be put into a new variable for further parsing. I would appreciate any help you guys can provide. Thanks for all the help, this forum has really helped me learn VB on my own!

Xavier

Code:
Private Sub AxMSComm1_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxMSComm1.OnComm
    Dim a() As String
    Dim Temp1, Temp2 As String
    Dim String1 As String
    If (AxMSComm1.CommEvent = 2) Then
        String1 = AxMSComm1.Input 'Save buffer into String1
        'Split String1 where '=' is present
        a = Microsoft.VisualBasic.Split(String1, "=") 
        Temp1 = a(0) 'Right side of '='
        Temp2 = a(1) 'Left side of '='
        If (Temp1 = "A") Then
            TextBox1.Text = Temp2 'Send to Amps TextBox
        ElseIf (Temp1 = "V") Then
            TextBox2.Text = Temp2 'Send to Volts TextBox
        ElseIf (Temp1 = "P") Then
            If (Temp2 = "T") Then 'If power is on...
                '...Put  checkmark on Power status box
                CheckBox1.CheckState =CheckState.Checked                       
            ElseIf (Temp2 = "F") Then 'If power is off...
                '...Remove checkmark from Power status box
                CheckBox1.CheckState = CheckState.Unchecked 
            End If
        End If
     End If
 
Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strBuffer As String = "A=100,V=12,P=T,A=121,V=10"
        Dim strArray() As String = Split(strBuffer, ",")
        Dim strLeft As String
        Dim x As Integer

        For x = 0 To UBound(strArray)
            strLeft = Split(strArray(x), "=")(0)
            Select Case strLeft
                Case "A"
                    MessageBox.Show("A has a value of: " & Split(strArray(x), "=")(1))
                    '// your code here for checkboxes etc...
                Case "V"
                    MessageBox.Show("V has a value of: " & Split(strArray(x), "=")(1))
                Case "P"
                    MessageBox.Show("P has a value of: " & Split(strArray(x), "=")(1))
            End Select
        Next
    End Sub
hope that helps a little.
 
You could also use Regular expressions to find groups and matches, though what you have seems fine, especially for such simple code. I'd keep it simple and clean since it sounds like you've already got a lot going on with reading from a microcontroller. Why messy up working, readable code with regular expressions (powerful, but not very intuitive).

For the record, a string object has a built in Split method. You can use the following instead of the Split function:
Visual Basic:
Dim s As String
s = "A=100, B=200"
Dim a() As String
a = s.Split(",")
' instead of a = Split(s, ",")

If you want to try out regular expressions, let me know.

-Nerseus
 
The only problem with using Regular Expressions in a case such as this would be speed issues. You'd have to perform tests to ensure that the efficiency of the parser is acceptable and optimized as much as possible if you were to go this route.
 
Thanks for all the help guys, I got it to work! Nerseus, if you can provide any extra info, it would be great. Since I'm learning VB on my own through this forum and the couple of books I have, any extra info would be great! Thanks again!
 
Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strString() As String = "A=100,P=200,V=300,A=50,P=60,V=78".Split(",") '///add each value between the "," to a string array.
        Dim i As Integer
        For i = LBound(strString) To UBound(strString) '///LBound is the bottom value of the string ,UBound is upper value.
            MessageBox.Show(strString(i)) '/// show the individual items in the array.
        Next
    End Sub
 
Back
Top