Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,
Can someone please tell me how to plot serial port data on Zedgaph. I tried modifying the sample given in code project sample code but din succeed. Please help.

Code:
VB.NET
Imports ZedGraph
Imports System.IO.Ports
Imports System.Threading


Public Class Form1
    Public data As Integer
    Public data_buffer(4) As Integer
    Public data_store As Integer
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SetSize()
    End Sub

    Public Sub CreateGraph(ByVal zgc As ZedGraphControl)
        Dim myPane As GraphPane = zgc.GraphPane

        ' Set the titles and axis labels
        myPane.Title.Text = "My Test Date Graph"
        myPane.XAxis.Title.Text = "X Value"
        myPane.YAxis.Title.Text = "My Y Axis"

        ' Make up some data points from the Sine function
        Dim list = New PointPairList()
        Dim x As Integer, y As Integer
        For x = 0 To 100
        Next x
       
        y = data_buffer(1)
        list.Add(x, y)


        ' Generate a blue curve with circle symbols, and "My Curve 2" in the legend
        Dim myCurve As LineItem = myPane.AddCurve("My Curve", list, Color.Blue, SymbolType.None)

        ' Fill the area under the curve with a white-red gradient at 45 degrees
        myCurve.Line.Fill = New Fill(Color.White, Color.Red, 45.0F)

        ' Make the symbols opaque by filling them with white
        myCurve.Symbol.Fill = New Fill(Color.White)

        ' Fill the axis background with a color gradient
        myPane.Chart.Fill = New Fill(Color.White, Color.LightGoldenrodYellow, 45.0F)

        ' Fill the pane background with a color gradient
        myPane.Fill = New Fill(Color.White, Color.FromArgb(220, 220, 255), 45.0F)

        ' Calculate the Axis Scale Ranges
        zgc.AxisChange()
    End Sub

    Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        SetSize()
    End Sub

    Private Sub SetSize()
        zg1.Location = New Point(10, 10)
        ' Leave a small margin around the outside of the control
        zg1.Size = New Size(ClientRectangle.Width - 20, ClientRectangle.Height - 20)
    End Sub

    Private Sub cmd_open_port(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If SerialPort1.IsOpen = False Then
                SerialPort1.Open()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub
    Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Do
            Try
                data = SerialPort1.ReadByte ' read first byte and check if it is 64
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

        Loop While (data <> 64) 'if data is 64 (new frame starts) , so come out of the loop and start reading and storing data in an array

        data_buffer(0) = data ' 64 is stored inside the databuffer


        data = SerialPort1.ReadByte  'data is read from serialport 
        data_buffer(1) = data ' push data to buffer

        Me.Invoke(New EventHandler(AddressOf UpdateControl))
        data = SerialPort1.ReadByte  '  * data 
        data_buffer(2) = data ' push to buffer
        
        data = SerialPort1.ReadByte  ' CHECKSUM 
        data_buffer(3) = data ' push to buffer


        data = SerialPort1.ReadByte ' CHECKSUM 
        data_buffer(4) = data ' push to buffer
        Dim i As Integer
        For i = 0 To 4
            Console.WriteLine(data_buffer(i))
        Next

    End Sub

    Public Sub UpdateControl(ByVal sender As Object, ByVal e As EventArgs)

        'Do any UI code here on the main thread
        CreateGraph(zg1)

    End Sub

  
End Class
Posted
Updated 13-Apr-12 20:23pm
v3
Comments
Nelek 14-Apr-12 9:23am    
If you get the code from an article in code project, you should ask in the message board at the bottom of the article. The best one to help you is the autor, and doing it so, he/she will get a notification.

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