Home > Net >  How to remove the WN00 from digital scale VB.net
How to remove the WN00 from digital scale VB.net

Time:03-29

I'm using Nport device to get data from a digital scale to a Vb.Net app and but i'm facing a problem trimming characters from the output.

The output raw data = WN0015.15 kg (example)

I tried to use:

Label11.Text = Thetext.Trim({"W"c, "N"c, "0"c, "k"c, "g"c})

But sometimes prints the value newline the raw data like:

15.15 kg
WN0015.15 kg

And when the value = to 0.00 or 0.50 print .50 or .00

My code:

    Imports System.Threading
    Imports System.IO.Ports
        Private Delegate Sub UpdateLabelDelegate(theText As String)
            Private Sub UpdateLabel(Thetext As String)
                If Me.InvokeRequired Then
                    Me.Invoke(New UpdateLabelDelegate(AddressOf UpdateLabel), Thetext)
                Else
                    Label11.Text = Thetext.Trim({"W"c, "N"c, "0"c, "k"c, "g"c})
                    netweight.Text = Label11.Text.Replace("kg", "").Trim()
                End If
        
            End Sub

Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim returnStr As String
        returnStr = SerialPort1.ReadExisting
        Me.BeginInvoke(Sub()
                           UpdateLabel(returnStr)

                       End Sub)
    End Sub

CodePudding user response:

You could try something more like:

Private Sub UpdateLabel(Thetext As String)
    If Me.InvokeRequired Then
        Me.Invoke(New UpdateLabelDelegate(AddressOf UpdateLabel), Thetext)
    Else
        Dim weight As String = Thetext.Trim.Split(Environment.NewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries).Last
        weight = weight.TrimStart("WN0".ToCharArray()).TrimEnd("kg".ToCharArray())
        netweight.Text = weight
    End If
End Sub
  • Related