|
-
Sep 24th, 2014, 12:55 AM
#1
Thread Starter
Junior Member
Plotting grapgh on VB net
Dear all,
I am leaner . I wanted to plot graph time VS current. CUrrently i can get data serially from arduino. Now i wanted to plot graph for every 1 min w.r.t time.
As shown in below Link. I am facing problem in configuring the chart.If you can share link How he exactly uploaidng inputs will be help full.
http://www.dreamincode.net/forums/to...&#entry2055191
My code fine to this part.
Code:
Imports System
Imports System.IO.Ports
Imports System.ComponentModel
Imports System.Threading
Imports System.Drawing
Public Class Form1
Dim myPort As Array
Dim Distance As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
PortComboBox.Items.AddRange(myPort)
BaudComboBox.Items.Add(9600)
BaudComboBox.Items.Add(19200)
BaudComboBox.Items.Add(38400)
BaudComboBox.Items.Add(57600)
BaudComboBox.Items.Add(115200)
ConnectButton.Enabled = True
DisconnectButton.Enabled = False
End Sub
Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
SerialPort1.PortName = PortComboBox.Text
SerialPort1.BaudRate = BaudComboBox.Text
SerialPort1.Open()
Timer1.Start()
'lblMessage.Text = PortComboBox.Text & " Connected."
ConnectButton.Enabled = False
DisconnectButton.Enabled = True
End Sub
Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectButton.Click
SerialPort1.Close()
DisconnectButton.Enabled = False
ConnectButton.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
SerialPort1.Write("c")
System.Threading.Thread.Sleep(250)
Dim k As Double
Dim distance As String = SerialPort1.ReadLine()
k = CDbl(distance)
ListBoxSensor.Text = k
Catch ex As Exception
End Try
End Sub
Private Sub Relay_ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Relay_ON.Click
SerialPort1.Write("1")
End Sub
Private Sub Relay_Off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Relay_Off.Click
SerialPort1.Write("0")
End Sub
End Class
-
Sep 24th, 2014, 10:01 AM
#2
Re: Plotting grapgh on VB net
Here is an example of how to chart a random number every second while showing no more than the last 1 minute of data. Hopefully you can adapt it to your needs.
Start a new project, add a chart and a timer to the form and copy this following code.
VB Code:
Public Class Form1
'create a datatable to hold the values that will bind to the chart series
Dim dT As DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call initializeDataTable()
Call initializeChart()
'set the datasource for the chart....
Chart1.DataSource = dT
Timer1.Interval = 1000
Timer1.Start()
End Sub
Private Sub initializeDataTable()
dT = New DataTable
dT.Columns.Add(New DataColumn("time", GetType(DateTime)))
dT.Columns.Add(New DataColumn("current", GetType(Double)))
End Sub
Private Sub initializeChart()
Chart1.Series.Clear()
'create the new charting series....
Dim newSeries As New DataVisualization.Charting.Series
With newSeries
'assign the series to a chart area....
.ChartArea = Chart1.ChartAreas(0).Name
'define the xvalue member. It must match the datatable's column name
.XValueMember = "time"
.XValueType = DataVisualization.Charting.ChartValueType.DateTime
'define the yValue meber. It too must match the datatable column name
.YValueMembers = "current"
.XValueType = DataVisualization.Charting.ChartValueType.Double
'select the chart type
.ChartType = DataVisualization.Charting.SeriesChartType.Line
End With
'turn off the xAxis labels
Chart1.ChartAreas(0).AxisX.LabelStyle.Enabled = False
'add the series to the chart
Chart1.Series.Add(newSeries)
End Sub
Dim rnd As New Random
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'add a new point
Dim current As Double = rnd.NextDouble
Dim r As DataRow = dT.NewRow
r("time") = Now
r("current") = current
'add the row
dT.Rows.Add(r)
'remove any row older than 1 minute
Dim oldestTime As DateTime = Now.AddMinutes(-1)
Do While DirectCast(dT.Rows(0)("time"), DateTime) < oldestTime
dT.Rows.RemoveAt(0)
Loop
'finally bind the chart....
Chart1.DataBind()
End Sub
End Class
kevin
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Sep 25th, 2014, 12:22 AM
#3
Thread Starter
Junior Member
Re: Plotting grapgh on VB net
Code:
Imports System
Imports System.IO.Ports
Imports System.ComponentModel
Imports System.Threading
''Imports
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Public Class Form1
Dim dT As DataTable
Dim myPort As Array
Dim Distance As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call initializeDataTable()
Call initializeChart()
myPort = IO.Ports.SerialPort.GetPortNames()
PortComboBox.Items.AddRange(myPort)
BaudComboBox.Items.Add(9600)
BaudComboBox.Items.Add(19200)
BaudComboBox.Items.Add(38400)
BaudComboBox.Items.Add(57600)
BaudComboBox.Items.Add(115200)
PortComboBox.Enabled = True
DisconnectButton.Enabled = False
'set the datasource for the chart....
Chart1.DataSource = dT
Timer1.Interval = 1000
Timer1.Start()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
SerialPort1.PortName = PortComboBox.Text
SerialPort1.BaudRate = BaudComboBox.Text
SerialPort1.Open()
Timer1.Start()
'lblMessage.Text = PortComboBox.Text & " Connected."
ConnectButton.Enabled = False
DisconnectButton.Enabled = True
End Sub
Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectButton.Click
SerialPort1.Close()
DisconnectButton.Enabled = False
ConnectButton.Enabled = True
End Sub
Private Sub initializeDataTable()
dT = New DataTable
dT.Columns.Add(New DataColumn("time", GetType(DateTime)))
dT.Columns.Add(New DataColumn("current", GetType(Double)))
End Sub
Private Sub initializeChart()
Chart1.Series.Clear()
'create the new charting series....
Dim newSeries As New DataVisualization.Charting.Series
With newSeries
'assign the series to a chart area....
.ChartArea = Chart1.ChartAreas(0).Name
'define the xvalue member. It must match the datatable's column name
.XValueMember = "time"
.XValueType = DataVisualization.Charting.ChartValueType.DateTime
'define the yValue meber. It too must match the datatable column name
.YValueMembers = "current"
.XValueType = DataVisualization.Charting.ChartValueType.Double
'select the chart type
.ChartType = DataVisualization.Charting.SeriesChartType.Line
End With
'turn off the xAxis labels
Chart1.ChartAreas(0).AxisX.LabelStyle.Enabled = False
'add the series to the chart
Chart1.Series.Add(newSeries)
End Sub
Dim rnd As New Random
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Try
SerialPort1.Write("c")
System.Threading.Thread.Sleep(250)
Dim k As Double
Dim distance As String = SerialPort1.ReadLine()
k = CDbl(distance)
ListBoxSensor.Text = k
' Dim current As Double = rnd.NextDouble
Dim current As Double = k
Dim r As DataRow = dT.NewRow
r("time") = Now
r("current") = current
'add the row
dT.Rows.Add(r)
'remove any row older than 1 minute
Dim oldestTime As DateTime = Now.AddMinutes(-1)
Do While DirectCast(dT.Rows(0)("time"), DateTime) < oldestTime
dT.Rows.RemoveAt(0)
Loop
'finally bind the chart....
Chart1.DataBind()
Catch ex As Exception
End Try
'add a new point
End Sub
End Class
I changed the code as you said, I tried with assign value and taken your code . But in my above code i am not getting data on ListBoxSensor . How i make ensure data read from serial and displayed on curve are accurate???
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
|