如何从电子秤上移除 WN00 VB.net

How to remove the WN00 from digital scale VB.net

我正在使用 Nport 设备将数据从数字秤获取到 Vb.Net 应用程序,但我在从输出中修剪字符时遇到问题。

输出原始数据=WN0015.15公斤(例子)

我尝试使用:

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

但有时会打印原始数据的值+换行符,如:

15.15 kg
WN0015.15 kg

并且当值=到0.000.50时打印.50.00

我的代码:

    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

您可以尝试更像:

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