如何绘制视觉基本实时图 [英] How to plot a visual basic real time graph

查看:66
本文介绍了如何绘制视觉基本实时图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够用串口数据绘制实时图表

但X轴时间刻度相同



例如5秒和30秒正在绘制相同的宽度。



任何帮助都会非常感激





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

推荐答案

使用新数据更新图表的唯一时间是com事件触发时。如果您希望图表每秒更新一次,请从计时器事件中调用图表更新。



您不应该在com事件中使用线程休眠,而是设置相应的你的com端口。
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


问题似乎在你的doupdate()子程序中。用'Now'替换X轴的'time'。



而不是





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(时间,vArray(1))

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


这篇关于如何绘制视觉基本实时图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆