Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys, I'm facing a big problem here, I'm developing a app to read data from a Weight. And everything is working perfectly, but the result is not what I expected, when reading the data from the scale, it keeps printing the data without stopping and I would like it to read a single line and whenever there is any change in the scale, just change the value and not add a new line...
How is printing:

My code:
VB.NET
  1  Public Class Form1
  2      Dim Q As Queue(Of String) = New Queue(Of String)
  3   
  4      Private Sub Form1_Load(ByVal sender As System.Object, _
  5              ByVal e As System.EventArgs) Handles MyBase.Load
  6          For Each s In System.IO.Ports.SerialPort.GetPortNames()
  7              ComboBox1.Items.Add(s)
  8          Next s
  9      End Sub
 10   
 11   
 12      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 13          Try
 14              If ComboBox1.SelectedIndex = -1 Then
 15                  MsgBox("Please select a port")
 16                  Exit Sub
 17              Else
 18                  SerialPort1.BaudRate = 9600
 19                  SerialPort1.DataBits = 8
 20                  SerialPort1.Parity = IO.Ports.Parity.None
 21                  SerialPort1.StopBits = IO.Ports.StopBits.One
 22                  SerialPort1.PortName = ComboBox1.SelectedItem.ToString
 23                  SerialPort1.Open()
 24                  Timer1.Start()
 25              End If
 26          Catch ex As Exception
 27              MsgBox(ex.Message)
 28          End Try
 29      End Sub
 30   
 31   
 32      Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
 33          ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
 34          Handles SerialPort1.DataReceived
 35          Q.Enqueue(SerialPort1.ReadExisting())
 36      End Sub
 37      Private Sub Timer1_Tick(ByVal sender As System.Object, _
 38             ByVal e As System.EventArgs) Handles Timer1.Tick
 39          SyncLock Q
 40              While Q.Count > 0
 41                  TextBox1.Text &= Q.Dequeue
 42              End While
 43          End SyncLock
 44      End Sub
 45   
 46      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
 47          SerialPort1.Close()
 48          Timer1.Stop()
 49      End Sub
 50  End Class


What I have tried:

I tried to implement Delegate
VB
Public Delegate Sub myDelegate()
Private Sub port_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
End Sub
Posted
Updated 18-Aug-21 1:58am
v2
Comments
Al Halabi Ahmad 25-Apr-23 9:57am    
do you have a form for this ?

The only way you are going to do that is to keep a "last value" and check it against the currently received one. If they are the same, discard it. If they aren't, use it and set the last value from that.

It may be possible to configure your specific scale to do it for you, but you'd have to talk to the manufacturers to find out if it possible, and if so how.
 
Share this answer
 
Fount the soluction:

Public Class Form5

    Delegate Sub SetTextCallback(ByVal data As String)

    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
            TextBox1.Text = theText
        End If
    End Sub

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

                           UpdateLabel(returnStr)
                       End Sub)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SerialPort1.Open()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        SerialPort1.Close()
    End Sub
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900