Results 1 to 3 of 3

Thread: Plotting grapgh on VB net

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Posts
    30

    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

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    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:
    1. Public Class Form1
    2.  
    3.     'create a datatable to hold the values that will bind to the chart series
    4.     Dim dT As DataTable
    5.  
    6.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    7.  
    8.         Call initializeDataTable()
    9.         Call initializeChart()
    10.  
    11.         'set the datasource for the chart....
    12.         Chart1.DataSource = dT
    13.  
    14.  
    15.         Timer1.Interval = 1000
    16.         Timer1.Start()
    17.  
    18.     End Sub
    19.  
    20.  
    21.     Private Sub initializeDataTable()
    22.  
    23.         dT = New DataTable
    24.         dT.Columns.Add(New DataColumn("time", GetType(DateTime)))
    25.         dT.Columns.Add(New DataColumn("current", GetType(Double)))
    26.  
    27.  
    28.  
    29.     End Sub
    30.  
    31.     Private Sub initializeChart()
    32.  
    33.         Chart1.Series.Clear()
    34.         'create the new charting series....
    35.         Dim newSeries As New DataVisualization.Charting.Series
    36.         With newSeries
    37.             'assign the series to a chart area....
    38.             .ChartArea = Chart1.ChartAreas(0).Name
    39.  
    40.             'define the xvalue member. It must match the datatable's column name
    41.             .XValueMember = "time"
    42.             .XValueType = DataVisualization.Charting.ChartValueType.DateTime
    43.             'define the yValue meber. It too must match the datatable column name
    44.             .YValueMembers = "current"
    45.             .XValueType = DataVisualization.Charting.ChartValueType.Double
    46.  
    47.             'select the chart type
    48.             .ChartType = DataVisualization.Charting.SeriesChartType.Line
    49.         End With
    50.  
    51.         'turn off the xAxis labels
    52.         Chart1.ChartAreas(0).AxisX.LabelStyle.Enabled = False
    53.  
    54.         'add the series to the chart
    55.         Chart1.Series.Add(newSeries)
    56.  
    57.     End Sub
    58.  
    59.     Dim rnd As New Random
    60.  
    61.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    62.  
    63.         'add a new point
    64.         Dim current As Double = rnd.NextDouble
    65.         Dim r As DataRow = dT.NewRow
    66.         r("time") = Now
    67.         r("current") = current
    68.         'add the row
    69.         dT.Rows.Add(r)
    70.  
    71.         'remove any row older than 1 minute
    72.         Dim oldestTime As DateTime = Now.AddMinutes(-1)
    73.         Do While DirectCast(dT.Rows(0)("time"), DateTime) < oldestTime
    74.  
    75.             dT.Rows.RemoveAt(0)
    76.  
    77.         Loop
    78.         'finally bind the chart....
    79.         Chart1.DataBind()
    80.  
    81.     End Sub
    82.  
    83.  
    84. 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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Posts
    30

    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
  •  



Click Here to Expand Forum to Full Width