tags:

views:

139

answers:

0

I have written a simple program which receive data from serial port i.e (temperature) and display on a line graph. but i require accumlate the graph data upto 10 minutes on screen. after that it again accumlate next 10 minutes data.

The Code of VB.Net program is below :

Imports System.Drawing.Image Imports System.IO.Ports

Public Class frmGraph

Dim WithEvents port2 As SerialPort = _ New SerialPort("COM2", 19200, Parity.Even, 7, StopBits.One)

' Variables to store the changing values to be charted (previous and current) Private OldValue As Single = 0 Private NewValue As Single = 0

Dim XMove As Integer = 4
Dim Chunks As Integer = 10

Private Sub frmGraph_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load SwitchMT500() ReadLineAddress()

    txtMin.Text = 620
    txtMax.Text = 630
    '  Paint Guidelines on picGraph
    picGraph.Image = DisplayGuidelines(picGraph, Chunks)
    '  Paint Guidelines and Numbers on picValues
    picValues.Image = Me.DisplayVerticalValues(picValues, Chunks, CInt(txtMin.Text), CInt(txtMax.Text))
    ReadTemperature()

End Sub

Private Function DisplayGuidelines(ByVal PicBox As PictureBox, ByVal chunks As Integer) As Bitmap ' Step 1 ' Create a bitmap to draw on and grab its Graphics Object Dim bm As New Bitmap(PicBox.Width, PicBox.Height) Dim gr As Graphics = Graphics.FromImage(bm)

    '  Step 2
    ' Draw guidelines on main chart.
    '  Get the total height available and split it into  chunks
    Dim total As Integer = PicBox.Height
    Dim chunk As Single = total / chunks

    '  Step 3
    For i As Single = chunk To total Step chunk
        gr.DrawLine(Pens.WhiteSmoke, 0, i, PicBox.Width, i)
    Next i

    '  Step 4
    ' return the results.
    Return bm

    '  Step 5
    gr.Dispose()
End Function

Private Function DisplayVerticalValues(ByVal PB As PictureBox, ByVal HowManyChunks As Single, _ ByVal MinValue As Single, ByVal MaxValue As Single) As Bitmap

    '  Step 1
    Dim bmp As New Bitmap(PB.Width, PB.Height)
    Dim gv As Graphics = Graphics.FromImage(bmp)

    '  Step 2
    '  Draw guidelines on values strip
    '  Get the total height available and split it into  chunks
    '  This value represents a number of pixels
    Dim TotalPixels As Integer = PB.Height
    Dim SingleChunk As Single = TotalPixels / HowManyChunks

    For i As Single = SingleChunk To TotalPixels Step SingleChunk
        gv.DrawLine(Pens.WhiteSmoke, 0, i, PB.Width, i)
    Next i

    '  Step 3
    '  Draw Numbers as Text, correctly spaced vertically
    '  Begin with the highest value allowed
    Dim NextMarker As Integer = MaxValue
    '  Calculate the plottable range
    Dim ValueRange As Integer = MaxValue - MinValue

    '  Draw the numbers, decrementing values proportionately each time through the loop
    For i As Single = 0 To TotalPixels Step SingleChunk
        gv.DrawString(CStr(NextMarker), New Font("Verdana", 8, FontStyle.Regular), Brushes.Black, 1, i)
        NextMarker -= (ValueRange / HowManyChunks)
    Next

    '  Step 4
    Return bmp

    '  Step 5
    gv.Dispose()

End Function

Private Function DisplayGuidelinesAndChart(ByVal PicBox As PictureBox, ByVal chunks As Integer, _ ByVal XMove As Integer, ByVal NewValue As Single, ByVal Min As Single, ByVal Max As Single) As Bitmap

    '  Step 1
    '  Grab the current image (the latest version of the chart)
    Dim bm As New Bitmap(PicBox.Width, PicBox.Height)
    Dim gr As Graphics = Graphics.FromImage(bm)

    '  Step 2
    '  Get the total height available and split it into  chunks
    Dim total As Integer = PicBox.Height
    Dim chunk As Single = total / chunks
    ' Tack missing guidelines to right hand side on the Graphics object.
    For i As Single = chunk To total Step chunk
        gr.DrawLine(Pens.WhiteSmoke, PicBox.Width - XMove, i, PicBox.Width, i)
    Next i

    '  Step 3
    '  Draw this grabbed image, placing it XMove pixels to the left 

    If Not IsNothing(PicBox.Image) Then
        gr.DrawImage(PicBox.Image, -XMove, 0)
    End If

    '  Step 4
    '  Plot the new value.  
    '  Calculate the scaling required to make full use of the height of
    '  the PictureBox
    Dim ValueRange As Single = Max - Min
    Dim vScale As Single = PicBox.Height / ValueRange
    '  Apply the scale to the current value
    NewValue *= vScale
    '  Step 5
    '  Shift start point from top left to bottom left.  
    gr.TranslateTransform(0, PicBox.Height)

    '  Step 6
    '  Draw the next line segment on the Graphics object.

    '  If Min is > 0 then you need to shift the drawing down once again,
    '  this time to put the Min value on the horizontal axis
    If Min > 0 Then gr.TranslateTransform(0, Min * vScale)

    Dim p As Pen = New Pen(Color.Navy, 1)

    gr.DrawLine(p, _
             PicBox.Width - 1 - XMove, -OldValue, _
             PicBox.Width - 1, -NewValue)
    OldValue = NewValue

    '   Step 7
    '  Return the Bitmap .
    Return bm


    '  Calculate length of baseline drawn by the code above
    BaseLineLength = PicBox.Width
    '  Calculate the width of each line segment
    LineWidth = (BaseLineLength / Sales.ToArray.Length)
    '   Step 8
    '  All done
    gr.Dispose()

End Function

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick picValues.Image = Me.DisplayVerticalValues(picValues, Chunks, CInt(txtMin.Text), CInt(txtMax.Text)) ReadTemperature()

End Sub

Private Function ReadTemperature() Dim strValue as string strValue = port2.ReadExisting 'picGraph.Image = DisplayGuidelinesAndChart(picGraph, Chunks, XMove, strvalue, CInt(txtMin.Text), CInt(txtMax.Text)) picGraph.Image = DisplayGuidelinesAndChart(picGraph, Chunks, XMove, strvalue, CInt(txtMin.Text), CInt(txtMax.Text))

        lblValue.Text = strvalue

End Function

End Class

Kindly provide code or functions so that i accumlate reading data from serial port on graph upto 10 minutes.