Click here to Skip to main content
14,836,552 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ZedGraph on Form1 along with a button and a label. I'm reading serialinput data(data format:startbit data stopbit checksum checksum)Serial Input data is the values of pressure sensor.I want to plot Pressure(in cmH2o) on X axis and time on y axis(in milliseconds). i read serial input data in a thread and display it in label as well as plot it on graph.. i can display data in label but cant plot the graph.

Imports ZedGraph
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Public data As Double
    Public count As Integer
    Public data_buffer(4) As Integer
    Public data_available As Integer = 0
    Public p As Double
    Private Sub cmd_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_open.Click
            If SerialPort1.IsOpen = False Then
                Console.WriteLine("PORT OPEN SUCCEEDED.")
            End If
                data_available = 1
        Catch ex As Exception
        End Try
    End Sub
    Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

                data = SerialPort1.ReadByte ' read first byte and check if it is 64
            Catch ex As Exception
            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=startbit is stored inside the databuffer

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


        data = SerialPort1.ReadByte  '  *=stop bit data 
        data_buffer(2) = data ' push to buffer

        Me.Invoke(New EventHandler(AddressOf UpdateControls_label))
        Me.Invoke(New EventHandler(AddressOf UpdateControls_graph))
        data = SerialPort1.ReadByte  ' read checksum CHECKSUM 
        data_buffer(3) = data ' push to buffer

        data = SerialPort1.ReadByte ' CHECKSUM 
        data_buffer(4) = data ' push to buffer

        For i = 0 To 4

    End Sub

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

        Me.Label1.Text = data_buffer(1)
    End Sub

    Private Sub UpdateControls_graph(ByVal sender As Object, ByVal e As EventArgs)
        Dim zgc = New ZedGraphControl
        Dim myPane As GraphPane = zgc.GraphPane
        Dim list = New PointPairList()
        Dim x As Double, y As Double
        For x = 0 To 36 ' considering some points for trial
            y = data_buffer(1)
            list.Add(x, y)
        Next x

        ' 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.Circle)
        ' 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
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
End Class

Your help will be much appreciated.
Thanks and Regards,
Kadhambari K
Updated 12-Apr-12 23:26pm
Nelek 13-Apr-12 5:26am
Fixed code tags
kadhambarikeshava 13-Apr-12 6:25am
Hello Nelek,

I want to plot serial input data on my ZedGraph in i receive data from serialport . my application shud receive data and plot a line graph or curve graph.I can read the data from serial port, but while displaying, my label displays just few initial values of serial data and stops. and nothing is displayed in graph. Please help me plotting data on Zedgraph from serialport.

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