|
-
Feb 18th, 2013, 08:11 AM
#1
Thread Starter
Lively Member
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
-
Feb 18th, 2013, 08:48 AM
#2
Thread Starter
Lively Member
Re: Application is... Stalling???
-
Feb 18th, 2013, 08:59 AM
#3
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.
-
Feb 18th, 2013, 09:01 AM
#4
Thread Starter
Lively Member
Re: Application is... Stalling???
Yes, same GetData... Was not sure how to separate them... Told you I'd be back soon lol...
-
Feb 18th, 2013, 09:02 AM
#5
Thread Starter
Lively Member
Re: Application is... Stalling???
By "logging" all its doing is storing the voltage, time, and force values into the DataGrid...
-
Feb 18th, 2013, 09:31 AM
#6
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)
-
Feb 18th, 2013, 09:35 AM
#7
Thread Starter
Lively Member
Re: Application is... Stalling???
Line arrives 50/s, rows are added as needed...
-
Feb 18th, 2013, 09:55 AM
#8
Re: Application is... Stalling???
 Originally Posted by rpmaurer
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.
-
Feb 18th, 2013, 10:10 AM
#9
Thread Starter
Lively Member
Re: Application is... Stalling???
It runs fine then... so get rid of that loop?
-
Feb 18th, 2013, 10:17 AM
#10
Re: Application is... Stalling???
 Originally Posted by rpmaurer
It runs fine then... so get rid of that loop?
Well I don't think you can add rows indefinitely.
-
Feb 18th, 2013, 10:19 AM
#11
Thread Starter
Lively Member
Re: Application is... Stalling???
Hmmm, what if I added... 500? Then when those got used up, add 500 more?
-
Feb 18th, 2013, 10:29 AM
#12
Re: Application is... Stalling???
 Originally Posted by rpmaurer
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.
-
Feb 18th, 2013, 10:53 AM
#13
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|