hi all, newbie wanna ask...

if i have string sent from serial comm, lets say the string is like this :

name1
name2
name3

how can i split that string into separate string, lets say i will put name1 into textbox1, name2 into textbox2, name3 into textbox3?

thanks for the help.

Recommended Answers

All 20 Replies

Assuming the lines are terminated with CRLF you can do

Dim lines() As String = text.Split(vbCrLf)

If you are not sure if they end with CRLF or just LF you can do

text = Replace(text,vbCr,"")
Dim lines As String = text.Split(vbLf)

@Jim, right idea, but wrong laguage. This forum is for VB4 through VB6, not VB.Net.

    Dim Lines() As String

    Lines = Split(linereadfromcomm, vbNewLine)

    ' determine number of text items extracted from linereadfromcomm
    ' so that you do not try to reference an array element that does
    ' not exist when assigning values to the textboxes

    Dim numlines As Integer
    numlines = UBound(Lines) + 1
    If numlines > 0 Then Text1.Text = Lines(0)
    If numlines > 1 Then Text2.Text = Lines(1)
    If numlines > 2 Then Text3.Text = Lines(2)

@ Reverend Jim : thanks for reply, still confused how to implement your code, any detail explanation for me, please...

@ TnTinMN : thanks for reply, i have tried your code, it work just a moment, i mean, this 3 lines string comes continuously, but in the splitted text boxt just appear the first value, didn't change like the data came. any suggestion for me?

you'll probably have to show the code you're using to get the string.

Hi,

Also, you have to post Sample data, which you are recieving from the Serial CommPort...

Regards
Veena

thanks for reply....

@ tinstaafl : here's my code (i put my received data from serial comm into textbox1, and i want to split it into textbox2,3 and 4):

Dim strInput As String
Dim Lines() As String
Lines = Split(Text1.Text, vbNewLine)
' determine number of text items extracted from linereadfromcomm
' so that you do not try to reference an array element that does
' not exist when assigning values to the textboxes
Dim numlines As Integer
numlines = UBound(Lines) + 1
If numlines > 0 Then Text2.Text = Lines(0)
If numlines > 1 Then Text3.Text = Lines(1)
If numlines > 2 Then Text4.Text = Lines(2)
  With MSComm1
    Select Case .CommEvent
    Case comEvReceive
        strInput = .Input
        temp = temp + strInput
    End Select
        Text1.Text = Text1.Text & temp
        Text1.SelStart = Len(Text1.Text)
  End With 

is it true? CMIIW, please...

@ QVeen72 : how can i do your suggestion?

sorry for my newbie.

it work just a moment, i mean, this 3 lines string comes continuously, but in the splitted text boxt just appear the first value, didn't change like the data came.

for this you should be determined about time duration for data coming . After that you have to put that time duration in interval property of timer control.

and finally i hope that you understand what i wanna say that is put your code within Private Sub Timer1_Timer() . . . End Sub

one more thing is that use your following code at the beginning of the event procedure

With MSComm1
    Select Case .CommEvent
    Case comEvReceive
        strInput = .Input
        temp = temp + strInput
    End Select
        Text1.Text = Text1.Text & temp
        Text1.SelStart = Len(Text1.Text)
  End With 

this will help you to solve the issue

thank's for reply....

@ rishif2 : i've set interval timer at 500, so the code just like this, is it same with your mean? :

Private Sub Timer1_Timer()

Dim strInput As String
Dim Lines() As String
Lines = Split(Text1.Text, vbNewLine)
' determine number of text items extracted from linereadfromcomm
' so that you do not try to reference an array element that does
' not exist when assigning values to the textboxes
Dim numlines As Integer
numlines = UBound(Lines) + 1
If numlines > 0 Then Text2.Text = Lines(0)
If numlines > 1 Then Text3.Text = Lines(1)
If numlines > 2 Then Text4.Text = Lines(2)

  With MSComm1
    Select Case .CommEvent
    Case comEvReceive
        strInput = .Input
        temp = temp + strInput
    End Select
        Text1.Text = Text1.Text & temp
        Text1.SelStart = Len(Text1.Text)
  End With 

End Sub

if it true, the result is same like before, or may be you have detail explanation for newbie like me.

you didnt put code(what specified in my reply) at the beginning.
did you set text1 's MultiLine property to True ?

And are you sure that you code you will return 3 strings each followed by newline as i've never worked with MSComm1 object?

you didnt put code(what specified in my reply) at the beginning

still confuse with this sentence... :(

did you set text1 's MultiLine property to True ?

yes, i did...

And are you sure that you code you will return 3 strings each followed by newline as i've never worked with MSComm1 object?

yes, i am, this 3 lines strings generated from serial comm, from arduino

try this:

        Private Sub Timer1_Timer()
            Dim strInput As String
            Dim Lines() As String
            With MSComm1
                Select Case .CommEvent
                Case comEvReceive
                    strInput = .Input
                    temp = temp + strInput
                End Select
                Text1.Text = Text1.Text & temp
                Text1.SelStart = Len(Text1.Text)
            End With        
            Lines = Split(Text1.Text, vbNewLine)
            ' determine number of text items extracted from linereadfromcomm
            ' so that you do not try to reference an array element that does
            ' not exist when assigning values to the textboxes
            Dim numlines As Integer
            numlines = UBound(Lines) + 1
            If numlines > 0 Then Text2.Text = Lines(0)
            If numlines > 1 Then Text3.Text = Lines(1)
            If numlines > 2 Then Text4.Text = Lines(2)
        End Sub

yes , this is what i said

thanks for reply,

@ tinstaafl : i've tried this/your code, but still have the same result, not update in the split box

what do you mean by split box?

f74a54cf4526e2171338fa4f4e707a8f @ tinstaafl : text box that i use to show the splitted string, 3 lines string ----> 1 lines string per text box, this 3 lines change every 1 sec, but in the splitted text box didn't update like the 3 lines string

Hi,

Check this :

With MSComm1
    Select Case .CommEvent
        Case comEvReceive
            strInput = .Input
            temp = temp + strInput
    End Select
    If Left(strInput,4) = "ping" Then
        Text1.Text = strInput
    ElseIf Left(strInput,4) = "dist" Then
        Text2.Text = strInput
    ElseIf Left(strInput,4) = "high" Then
        Text3.Text = strInput
    End If
End With 

Regards
Veena

in your some previous reply i Noticed that

i've set interval timer at 500

And now you are saying

this 3 lines change every 1 sec

So i think that you have to set interval property with value 1000

Now lets come to topic
Delete textbox 's contents(having MultiLine property True) every time when Timer1_Timer() executes
so try this

Private Sub Timer1_Timer()
            Dim strInput As String
            Dim Lines() As String
            Text1.Text = ""
            With MSComm1
                Select Case .CommEvent
                Case comEvReceive
                    strInput = .Input
                    temp = temp + strInput
                End Select
                Text1.Text = Text1.Text & temp
                Text1.SelStart = Len(Text1.Text)
            End With        
            Lines = Split(Text1.Text, vbNewLine)
            ' determine number of text items extracted from linereadfromcomm
            ' so that you do not try to reference an array element that does
            ' not exist when assigning values to the textboxes
            Dim numlines As Integer
            numlines = UBound(Lines) + 1
            If numlines > 0 Then Text2.Text = Lines(0)
            If numlines > 1 Then Text3.Text = Lines(1)
            If numlines > 2 Then Text4.Text = Lines(2)
        End Sub

hope now it will work and you will close this thread by marking this as solved . . .

@ QVeen72 : thanks for reply, i've tried your code, but in the splitted textbox just appear string in the "___" (eg. : "ping"), any another suggestion?

Hi,

I Guess, you have to write the code in "MSComm1_OnComm" event...

Regards
Veena

rishif2's code should work for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.