Results 1 to 13 of 13

Thread: Application is... Stalling???

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Application is... Stalling???

    My program reads data coming in from a serial port, this data includes battery voltages, and a load cell reading. If the "logging" button has been activated, then it should place some of this data in the chart below the graph (the graph is update regardless). For some reason, once I added the battery voltages, I can not get the graph and chart to update at the same time. If I stop the logging, then the graph runs fine, if I start the logging it starts to slow down and the program barely runs?

    Screenshot below

    Code:
    Code:
    Private Sub GetData()
            Try
                If InvokeRequired Then
                    Me.Invoke(New MethodInvoker(AddressOf GetData))
                Else
    
                    'get voltage data from loadcell, main batt, and ampbatt
                    inputstring = SerialPort1.ReadLine
                    
                    loadcell = Val(inputstring.Substring(0, 4)) / 1024 * 10
                    inForce = (loadcell - zerovalue) * forcescale
                    Datachart.Series("DataSeries").Points.AddXY(Timer.Elapsed.TotalSeconds, inForce)
                    Datachart.ChartAreas(0).AxisX.Minimum = Datachart.ChartAreas(0).AxisX.Maximum - 5
                    SerialPort1.Write("1")
    
                    If logging = 1 Then
                        DataGrid.RowCount = DataGrid.RowCount + 1
                        row = DataGrid.RowCount - 1
                        DataGrid.Rows(row).Cells(1).Value = Math.Round(Timer.Elapsed.TotalSeconds, 4)
                        DataGrid.Rows(row).Cells(2).Value = Math.Round(loadcell, 4)
                        DataGrid.Rows(row).Cells(3).Value = Math.Round(inForce, 4)
                        If Not DataGrid.Rows(row).Displayed Then
                            DataGrid.FirstDisplayedScrollingRowIndex = row - 2
                        End If
                    End If
    
                    Try
                        mainbatt = Val(inputstring.Substring(4, 4)) / 1024 * 10
                        ampbatt = Val(inputstring.Substring(8, 4)) / 1024 * 10
                        Label_AmpBatt.Text = Math.Round(ampbatt, 3).ToString + "V"
                        Label_MainBatt.Text = Math.Round(mainbatt, 3).ToString + "V"
                    Catch ex As Exception
    
                    End Try
    
                     End If
            Catch ex As Exception
    
            End Try
            
        End Sub
    Care to help?
    Thanks!
    Ryan

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Application is... Stalling???

    Name:  Untitled.jpg
Views: 229
Size:  270.2 KB

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Application is... Stalling???

    Is this the same GetData that is the DataReceived event handler? Which part of this is logging? If this is real-time data acquisition you should separate the Serial port reads / writes from the UI updating.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Application is... Stalling???

    Yes, same GetData... Was not sure how to separate them... Told you I'd be back soon lol...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Application is... Stalling???

    By "logging" all its doing is storing the voltage, time, and force values into the DataGrid...

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Application is... Stalling???

    How often do you have a line arriving? How many rows in the datagrid?

    One thing you should do is change the method signature to

    Code:
        Private Sub GetData(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Application is... Stalling???

    Line arrives 50/s, rows are added as needed...

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Application is... Stalling???

    Quote Originally Posted by rpmaurer View Post
    Line arrives 50/s, rows are added as needed...
    So as a test just have one row and overwrite the data as it comes in.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Application is... Stalling???

    It runs fine then... so get rid of that loop?

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Application is... Stalling???

    Quote Originally Posted by rpmaurer View Post
    It runs fine then... so get rid of that loop?
    Well I don't think you can add rows indefinitely.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Application is... Stalling???

    Hmmm, what if I added... 500? Then when those got used up, add 500 more?

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Application is... Stalling???

    Quote Originally Posted by rpmaurer View Post
    Hmmm, what if I added... 500? Then when those got used up, add 500 more?
    Check the count of rows before adding new rows. If the count is > 500, delete row 0, then add new row. Try that.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Application is... Stalling???

    So here is an example of how I would approach this, based on what I know.

    Code:
    Public Class Form1
    
        Private WithEvents aTimer As New Windows.Forms.Timer
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            aTimer.Interval = 10
            aTimer.Start()
            initSP()
        End Sub
    
        Private Sub initSP() 'initialize serial communication
            'add handler
            AddHandler SerialPort1.DataReceived, AddressOf GetData
            'change the following lines as needed
            Dim selectedport As String = "COM27"
            SerialPort1.DtrEnable = True
            SerialPort1.RtsEnable = True
            SerialPort1.BaudRate = 9600
            SerialPort1.PortName = selectedport
            SerialPort1.Open()
        End Sub
    
        Dim datastrm As New System.Text.StringBuilder
        Dim dataLock As New Object
    
        Private Sub GetData(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs)
            'just read the bytes coming into the serial port
            Dim btr As Integer = Me.SerialPort1.BytesToRead
            Dim buf(btr - 1) As Byte
            SerialPort1.Read(buf, 0, btr)
            'add the bytes as a string to the data stream
            'the data stream is processed in a timer
            Threading.Monitor.Enter(dataLock)
            datastrm.Append(SerialPort1.Encoding.GetChars(buf, 0, btr))
            Threading.Monitor.Exit(dataLock)
        End Sub
    
        Private Sub aTimer_Tick(sender As Object, e As EventArgs) Handles aTimer.Tick
            aTimer.Enabled = False
            'this runs on the UI
            If datastrm.Length >= 8 Then
                Dim i As Integer = datastrm.ToString.IndexOf(ControlChars.CrLf) 'check for CR LF
                If i > 0 Then
                    Threading.Monitor.Enter(dataLock)
                    Dim inputstring As String = datastrm.ToString.Substring(0, i) 'same as inputstring = SerialPort1.ReadLine
                    datastrm.Remove(0, i + ControlChars.CrLf.Length)
                    Threading.Monitor.Exit(dataLock)
    
                    'process the input string here
    
                    Debug.Write(inputstring)
                    Debug.WriteLine("")
    
                    'SerialPort1.Write("1") 'do you need this???
                End If
            End If
            aTimer.Enabled = True
        End Sub
    End Class
    My concern with this is that datastrm would exceed memory if there is a lot of processing.
    Last edited by dbasnett; Feb 18th, 2013 at 11:00 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width