Click here to Skip to main content
14,766,249 members
Please Sign up or sign in to vote.
3.53/5 (3 votes)
See more:
i'm able to plot a real time graph with the data from serial port
but the X axis time scale was the same

e.g 5sec and 30sec is plotting the same width.

any help will be much appreciate


Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
    Dim portdata As String
    Dim vArray As Array
    Dim time As String
    Dim engChart As New Series
    Dim pwrChart As New Series


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Chart1.Series.Clear()
        Chart1.Titles.Add("Wireless energy meter chart")
        engChart.Name = "Energy"
        pwrChart.Name = "Power"
        engChart.ChartType = SeriesChartType.Line
        pwrChart.ChartType = SeriesChartType.Line
        Chart1.Series.Add(engChart)
        'Chart1.Series.Add(pwrChart)
        If SerialPort1.IsOpen Then ' check if serial port is open
            SerialPort1.Close()     'close serial port of is open
        End If

        SerialPort1.PortName = "COM1"
        SerialPort1.BaudRate = 9600
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.DataBits = 8
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.RtsEnable = True
        SerialPort1.Open()

    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        System.Threading.Thread.Sleep(500)

        portdata = SerialPort1.ReadExisting

        vArray = Split(portdata, ",")
        Me.Invoke(New EventHandler(AddressOf DoUpdate))
        'My.Computer.FileSystem.WriteAllText("C:\TestFolder1\123.txt", Now, True)
        My.Computer.FileSystem.WriteAllText("C:\TestFolder1\123.txt", portdata, True)



    End Sub
    Public Sub DoUpdate()

        lblEnergyDsp.Text = vArray(0)
        lblPowerDsp.Text = vArray(1)


        engChart.Points.AddXY(time, vArray(0))
        pwrChart.Points.AddXY(time, vArray(1))




    End Sub
    Private Sub Form1_FormClosed(ByVal sender As System.Object, _
                   ByVal e As System.Windows.Forms.FormClosedEventArgs) _
                   Handles MyBase.FormClosed
        ' Close the Serial Port
        SerialPort1.Close()

    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        time = TimeOfDay


    End Sub
End Class
Posted
Updated 10-Jul-17 3:49am
Comments
Thava Rajan 4-Feb-14 0:43am
   
did show your port data please
S Houghtelin 4-Feb-14 8:23am
   
+5 for showing your code and for using formatting. You neeed to read up on using the serialport correctly, I see a number of issues and potential for locking the application up.

The only time your chart is updated with new data is when your com event fires. If you want your chart to update once per second call your chart update from your timer event.

You should not be using the thread sleep in your com event, rather set your com port accordingly.
'If you are expecting two bytes set the receive event to fire when you get them.
SerialPort1.ReceivedBytesThreshold = 2
'And if think it take a long time to get those two bytes...
SerialPort1.ReadTimeout = 500
   
The problem seems lie within your doupdate() subroutine. Replace 'time' for X axis with 'Now'.

Instead of


Quote:
engChart.Points.AddXY(time, vArray(0))
pwrChart.Points.AddXY(time, vArray(1))



Try

engChart.Points.AddXY(Now, vArray(0))
        pwrChart.Points.AddXY(Now, vArray(1))



Cheers
   
Comments
Richard Deeming 10-Jul-17 13:16pm
   
This question was asked and answered THREE YEARS AGO.

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