Need your urgent help , i have connected weighing machine with serial port ,now am getting data to a rich textbox
in a string form , problem is i dont know how to get the only desired figures.
the output string coming in this format from rtbDisplay _A0+0000002 Kg054__A0+0000002 Kg054__ ineed only
three digits and want to cut A0+000 from left and 02 from right to get remaiing 000 from this string to show in a text box which are actual ouputs of weight.

Private Sub cmdOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpen.Click
        comm.Parity = cboParity.Text
        comm.StopBits = cboStop.Text
        comm.DataBits = cboData.Text
        comm.BaudRate = cboBaud.Text
        comm.DisplayWindow = rtbDisplay-----------------> Output richtextbox
        comm.OpenPort()

        cmdOpen.Enabled = False
        cmdClose.Enabled = True
        cmdSend.Enabled = True
    End Sub
PLEASE HELP ME REGARDING THIS

Recommended Answers

All 3 Replies

From what I notice if your string is equal to

_A0+0000002 Kg054__A0+0000002 Kg054__

if you cut out A0+000 from left and 02 from the right then all its left is

00

but assumming that your string is

_A0+00000002 Kg054__A0+00000002 Kg054__

You could Parse the rtbDisplay see how to split string here

or if the string is always in this format you could do something like this.

I will change 000 to 111 so we dont get dizzy.

Dim myweight as String = "_A0+00011102 Kg054__A0+00011102 Kg054__"

Put this code in a button to get the desired digit 111

TextBox1.Text = ParseString(myweight)

Create the Function to Get your DesiredString

Public Function ParseString(ByVal DesiredString As String)

    Dim NewFormat As String
    Dim counter As Integer = 0


    For Each Character As Char In DesiredString

        If counter >= 1 And counter < 4 Then

            counter += 1       =====> 'Here we are removing the 000 from A0+000

        ElseIf Character.ToString = "+" Then

            counter += 1

        ElseIf counter >= 4 And counter <= 6 Then

            NewFormat = NewFormat & Character   ======> 'We collect the desired digit
                                                ======> ' 111 and ignore the rest of
            counter += 1                        ======> ' the Characters in the String

        End If



    Next


    Return NewFormat

End Function

If the strings are always the same size and the data you need always in the same place the string.subString function will do that much easier.

string.subString(startLocation, NoOfCharactersToReturn)
' for your particulat string
dim part = string.subString(7,3)

If the string contains arbitrary numbers of substrings of the form "Kg###" then you can extract them all using a regular expression as follows

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim test As String = "_A0+0000002 Kg054__A0+0000002 Kg055__ "
        Dim rexp As New Regex("Kg(\d*)")

        For Each m As Match In rexp.Matches(test)
            Debug.WriteLine(m.Value)
        Next

    End Sub

End Class

The output of the above code is

Kg054
Kg055

You can strip off the leading Kg by either substringing or replacing "Kg" with ""

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.