启用自动滚动vb.net [英] enable auto scroling vb.net

查看:83
本文介绍了启用自动滚动vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,制作了节目。在这一个aarduino发送6个模拟信号,vb.net在屏幕上制作图形。但是在总和时间(2,3分钟)之后,这个程序变得缓慢并且图形不是实时工作。有时间显示模拟信号变化。我怎么解决这个问题。如何为图表编程启用自动滚动?请帮助我

谢谢你



这是我的代码





选项严格在
选项推断在


''' < 摘要 >
'''串口演示Ellen Ramcke 2012
'''< / summary > ;
'''< 备注 > < / remarks >
公共类mainForm

Dim val(9)As Integer
Dim count As Integer
Dim temp_val As Integer
Dim old_text As String
Dim ack As Boolean

Private readBuffer As String = String.Empty
Privat e Bytenumber As Integer
Private ByteToRead As Integer
Private byteEnd(2)As Char
Private comOpen As Boolean





#Regionform events

私有财产CharToRead As Integer

'''< 摘要 >
'''关闭应用程序和COM端口
'' '< / summary >
Private Sub Form1_FormClosed(ByVal sender As System.Object,_
ByVal e As System.Windows.Forms.FormClosedEventArgs)_
Handles MyBase.FormClosed
if comOpen Then SerialPort1.Close( )
结束次级

'''< 摘要 < span class =code-keyword>>
'''打开Windows窗体
'''< / summary >
Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load
Label2 .ForeColor = Color.Black
Label2.Text =等待同步




'读取可用的COM端口:
Dim Portnames As String()= System.IO.Ports.SerialPort.GetPortNames
如果Portnames为Nothing那么
MsgBox(没有检测到Com端口!)
Me.Close()
结束如果
cboComPort.Items.AddRange(Portnames)
cboComPort.Text = Portnames(0)
cboBaudRate.Text =9600

End Sub

'''< 摘要 >
'''打开Com端口这里
'''< / summary >
Private Sub btnComOpen_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnComOpen.Click
如果是comOpen则SerialPort1.WriteLine(r)
'device params
使用SerialPort1

.ParityReplace =& H3B'replace;发生奇偶校验错误时
.PortName = cboComPort.Text
.BaudRate = CInt(cboBaudRate.Text)
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.Handshake = IO.Ports.Handshake.None
.RtsEnable = False
.ReceivedBytesThreshold = 1'threshold:缓冲区中的一个字节> event被触发
.NewLine = vbCr'CR必须是帧中的最后一个char。这将终止SerialPort.readLine
.ReadTimeout = 10000

结束使用

'检查设备是否可用:
尝试
SerialPort1。 Open()
comOpen = SerialPort1.IsOpen
Catch ex As Exception
comOpen = False
MsgBox(Error Open:& ex.Message)
picOpen。 BackColor = Color.Red
结束尝试

如果comOpen则
picOpen.BackColor = Color.Green
cboComPort.Enabled = False
cboBaudRate.Enabled =假
结束如果
如果comOpen则SerialPort1.WriteLine(r)
结束次级

'''< ; 摘要 >
'''关闭ComPort
' ''< / summary >
Private Sub Button_Close_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnComClose.Click
如果comOpen则
'清除输入缓冲区
SerialPort1.DiscardInBuffer()
SerialPort1.Close()
结束如果
comOpen = False
picOpen.BackColor = Color.Red
picDataReceived .BackColor = Color.Gray
cboComPort.Enabled = True
cboBaudRate.Enabled = True
End Sub

'''< 摘要 >
'''clear TextBoxes
'''< / summary >
Private Sub Button_clear_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnClear.Click
tbRx.Text = String.Empty
tbTx.Text = String.Empty
End Sub

'''< 摘要 >
'''将文本框内容写入端口
'''< span class =code-keyword>< / summary >
Private Sub button_send_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnSend.Click
如果是comOpen则SerialPort1.WriteLine(tbTx.Text)
End Sub

'''< 摘要 >
'''关闭app
'''< / summary >
Private Sub Button_ende_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnExit.Click
如果是comOpen那么
'清除输入缓冲区
SerialPort1.DiscardInBuffer()
SerialPort1.Close()
结束如果
comOpen = False
Me.Close()
End Sub

'''< 摘要 >
'''将控制面板密钥发送到com端口
'''< / summary >
'''< span class =code-keyword>< param name = sender > 返回键名< / param >


'''< 摘要 >
'''计时器datareceived事件
'''< / summary >
Private Sub Timer1_Tick(ByVal sender As System.Object ,ByVal e As System.EventArgs)处理Timer1.Tick
picDataReceived.BackColor = Color.Gray
Timer1.Enabled = False

End Sub

#End Region

#RegionComPort读取数据

'''< 摘要 >
'''辅助线程上的异步读取
'''< / summary >
Private Sub SerialPort1_DataReceived ( ByVal sender As System.Object,_
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)_
Handles SerialPort1.DataReceived
if comOpen Then
try
byteEnd = SerialPort1.NewLine.ToCharArray

'获取缓冲区中的字节数
'Bytenumber = SerialPort1.BytesToRead

'从缓冲区读取一个字节
'ByteToRead = SerialPort1.ReadByte()

'从缓冲区中读取一个字符
CharToRead = SerialPort1.ReadChar()

'读取字符串90
'readBuffer1 = SerialPort1.ReadTo(90)

'读取整个字符串直到.Newline
readBuffer = SerialPort1.ReadLine()

'数据到UI线程
Me.Invoke(New EventHandler( AddressOf DoUpdate))

Catch ex As Exception
MsgBox(read& ex.Message)
结束尝试
结束如果
结束次级

'''< < span class =code-leadattribute>摘要 >
'''在UI中更新收到的字符串
'''< span class =code-keyword>< / summary >
'''< 备注 > < /备注 >
Public Sub DoUpdate(ByVal sender As Object,ByVal e As System.EventArgs)

tbRx.Text = readBuffer
readBuffer.ToString()

' temp_val = CInt(readBuffer)
尝试
temp_val = Integer.Parse(tbRx.Text)

Catch ex As Exception

End尝试



尝试
如果temp_val = 1那么
ack = True
readBuffer =
如果是comOpen然后是SerialPort1 .WriteLine(2)
count = 0
结束如果


Catch ex As Exception

结束尝试

如果ack = True则

尝试
val(count)= Integer.Parse(tbRx.Text)
count = count + 1
如果计数= 7然后
count = 0
结束如果


TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear( )
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
TextBox7.Clear()


TextBox1 .Text = CStr(val(0))
TextBox2.Text = CStr(val(1))
TextBox3.Text = CStr(val(2))
TextBox4.Text = CStr(val(3))
TextBox5.Text = CStr(val(4))
TextBox6.Text = CStr (val(5))
TextBox7.Text = CStr(val(6))

Chart1.Series(Val1)。Points.AddY(val(0))
Chart1.Series(Val2)。Points.AddY(val(1))
Chart1.Series(Val3)。Points.AddY(val(2))
Chart1.Series( Val4)。Points.AddY(val(3))
Chart1.Series(Val5)。Points.AddY(val(4))
Chart1.Series(Val6)。点数。 AddY(val(5))
Chart1.Series(Val7)。Points.AddY(val(6))




' Chart1.ChartAreas(Val1)。CursorX.AutoScroll = True

Dim FILE_NAME As String =d:\ txt.txt

If System.IO.File .Exists(FILE_NAME)= True然后

Dim objWriter As New System.IO.S treamWriter(FILE_NAME,True)
objWriter.Write(Now.TimeOfDay)
索引As Integer = 0到6
objWriter.Write(val(index)& vbTab)
下一个

objWriter.Write(vbCrLf)


objWriter.Close()
'MsgBox(文本写入文件)

否则

MsgBox(文件不存在)

结束如果



Catch ex As Exception

结束尝试

结束如果





$ b End Sub

#End Region

Private Sub Button10_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles Button10.Click
If comOpen然后SerialPort1.WriteLine(E)

End Sub

Private Sub Button11_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Button11.Click
System.Diagnostics.Process.Start(C:\ Users \ AMAL \Downloads\Music\a.mp3)
End Sub

Private Sub Ť imer2_Tick(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Timer2.Tick









End Sub

Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)



结束子
结束类

解决方案

如果你只是自动滚动它,它仍然会很慢目前,或实际上较慢,因为控件将定期失效,以更新屏幕上实际显示的数据。此外,如果你等待更长的时间,你的应用程序最终将耗尽系统内存。



解决方案是:首先,你必须意识到那里不是奇迹。您无法无限地添加数据。因此,不使用滚动或常规滚动,或控制滚动以显示最新数据。但是您需要按特定大小的块移除旧数据(您可能希望使此块可配置,或者只有一个非滚动控件可以容纳的点数),删除旧数据。您还可能希望将所有数据转储到某个文件中,并定期附加该文件。稍后,您的查看者可以显示数据历史的任何片段。



-SA


um made program. on this one aarduino send 6 analog signals and vb.net is make graph in the screen . but after sum times(2,3mins) this program gone to slow and graph not real time work .it get some time for display analog signal changes. how i solve this one. how enable auto scrolling for this programmed for chart? pls help me
thank u

this is my code


Option Strict On
Option Infer On


''' <summary>
''' Serial Port Demo Ellen Ramcke 2012
''' </summary>
''' <remarks></remarks>
Public Class mainForm

    Dim val(9) As Integer
    Dim count As Integer
    Dim temp_val As Integer
    Dim old_text As String
    Dim ack As Boolean

    Private readBuffer As String = String.Empty
    Private Bytenumber As Integer
    Private ByteToRead As Integer
    Private byteEnd(2) As Char
    Private comOpen As Boolean





#Region "form events"

    Private Property CharToRead As Integer

    ''' <summary>
    ''' close application and COM Port
    ''' </summary>
    Private Sub Form1_FormClosed(ByVal sender As System.Object, _
                                 ByVal e As System.Windows.Forms.FormClosedEventArgs) _
                                 Handles MyBase.FormClosed
        If comOpen Then SerialPort1.Close()
    End Sub

    ''' <summary>
    ''' open Windows Form
    ''' </summary>
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label2.ForeColor = Color.Black
        Label2.Text = "waiting for sync"




        ' read avaiable COM Ports:
        Dim Portnames As String() = System.IO.Ports.SerialPort.GetPortNames
        If Portnames Is Nothing Then
            MsgBox("There are no Com Ports detected!")
            Me.Close()
        End If
        cboComPort.Items.AddRange(Portnames)
        cboComPort.Text = Portnames(0)
        cboBaudRate.Text = "9600"

    End Sub

    ''' <summary>
    ''' Open Com Port here
    ''' </summary>
    Private Sub btnComOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComOpen.Click
        If comOpen Then SerialPort1.WriteLine("r")
        ' device params
        With SerialPort1

            .ParityReplace = &H3B                    ' replace ";" when parity error occurs
            .PortName = cboComPort.Text
            .BaudRate = CInt(cboBaudRate.Text)
            .Parity = IO.Ports.Parity.None
            .DataBits = 8
            .StopBits = IO.Ports.StopBits.One
            .Handshake = IO.Ports.Handshake.None
            .RtsEnable = False
            .ReceivedBytesThreshold = 1             'threshold: one byte in buffer > event is fired
            .NewLine = vbCr         ' CR must be the last char in frame. This terminates the SerialPort.readLine
            .ReadTimeout = 10000

        End With

        ' check whether device is avaiable:
        Try
            SerialPort1.Open()
            comOpen = SerialPort1.IsOpen
        Catch ex As Exception
            comOpen = False
            MsgBox("Error Open: " & ex.Message)
            picOpen.BackColor = Color.Red
        End Try

        If comOpen Then
            picOpen.BackColor = Color.Green
            cboComPort.Enabled = False
            cboBaudRate.Enabled = False
        End If
        If comOpen Then SerialPort1.WriteLine("r")
    End Sub

    ''' <summary>
    ''' close ComPort
    ''' </summary>
    Private Sub Button_Close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComClose.Click
        If comOpen Then
            ' clear input buffer
            SerialPort1.DiscardInBuffer()
            SerialPort1.Close()
        End If
        comOpen = False
        picOpen.BackColor = Color.Red
        picDataReceived.BackColor = Color.Gray
        cboComPort.Enabled = True
        cboBaudRate.Enabled = True
    End Sub

    ''' <summary>
    ''' clear TextBoxes
    ''' </summary>
    Private Sub Button_clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        tbRx.Text = String.Empty
        tbTx.Text = String.Empty
    End Sub

    ''' <summary>
    ''' write content of Textbox to Port
    ''' </summary>
    Private Sub button_send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        If comOpen Then SerialPort1.WriteLine(tbTx.Text)
    End Sub

    ''' <summary>
    ''' close app
    ''' </summary>
    Private Sub Button_ende_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        If comOpen Then
            ' clear input buffer
            SerialPort1.DiscardInBuffer()
            SerialPort1.Close()
        End If
        comOpen = False
        Me.Close()
    End Sub

    ''' <summary>
    ''' send control panel key to com port
    ''' </summary>
    ''' <param name="sender">return key name</param>


    ''' <summary>
    ''' Timer datareceived event
    ''' </summary>
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        picDataReceived.BackColor = Color.Gray
        Timer1.Enabled = False

    End Sub

#End Region

#Region "ComPort read data"

    ''' <summary>
    ''' async read on secondary thread
    ''' </summary>
    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
                                         ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
                                         Handles SerialPort1.DataReceived
        If comOpen Then
            Try
                byteEnd = SerialPort1.NewLine.ToCharArray

                ' get number off bytes in buffer
                'Bytenumber = SerialPort1.BytesToRead

                ' read one byte from buffer
                'ByteToRead = SerialPort1.ReadByte()

                ' read one char from buffer
                CharToRead = SerialPort1.ReadChar()

                ' read until string "90"
                'readBuffer1 = SerialPort1.ReadTo("90")

                ' read entire string until .Newline
                readBuffer = SerialPort1.ReadLine()

                'data to UI thread
                Me.Invoke(New EventHandler(AddressOf DoUpdate))

            Catch ex As Exception
                MsgBox("read " & ex.Message)
            End Try
        End If
    End Sub

    ''' <summary>
    ''' update received string in UI
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)

        tbRx.Text = readBuffer
        readBuffer.ToString()

        '  temp_val = CInt(readBuffer)
        Try
            temp_val = Integer.Parse(tbRx.Text)

        Catch ex As Exception

        End Try



        Try
            If temp_val = 1 Then
                ack = True
                readBuffer = ""
                If comOpen Then SerialPort1.WriteLine("2")
                count = 0
            End If


        Catch ex As Exception

        End Try

        If ack = True Then

            Try
                val(count) = Integer.Parse(tbRx.Text)
                count = count + 1
                If count = 7 Then
                    count = 0
                End If


                TextBox1.Clear()
                TextBox2.Clear()
                TextBox3.Clear()
                TextBox4.Clear()
                TextBox5.Clear()
                TextBox6.Clear()
                TextBox7.Clear()


                TextBox1.Text = CStr(val(0))
                TextBox2.Text = CStr(val(1))
                TextBox3.Text = CStr(val(2))
                TextBox4.Text = CStr(val(3))
                TextBox5.Text = CStr(val(4))
                TextBox6.Text = CStr(val(5))
                TextBox7.Text = CStr(val(6))

                Chart1.Series("Val1").Points.AddY(val(0))
                Chart1.Series("Val2").Points.AddY(val(1))
                Chart1.Series("Val3").Points.AddY(val(2))
                Chart1.Series("Val4").Points.AddY(val(3))
                Chart1.Series("Val5").Points.AddY(val(4))
                Chart1.Series("Val6").Points.AddY(val(5))
                Chart1.Series("Val7").Points.AddY(val(6))




                ' Chart1.ChartAreas("Val1").CursorX.AutoScroll = True

                Dim FILE_NAME As String = "d:\txt.txt"

                If System.IO.File.Exists(FILE_NAME) = True Then

                    Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
                    objWriter.Write(Now.TimeOfDay)
                    For index As Integer = 0 To 6
                        objWriter.Write(val(index) & vbTab)
                    Next

                    objWriter.Write(vbCrLf)


                    objWriter.Close()
                    ' MsgBox("Text written to file")

                Else

                    MsgBox("File Does Not Exist")

                End If



            Catch ex As Exception

            End Try

        End If





    End Sub

#End Region

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        If comOpen Then SerialPort1.WriteLine("E")

    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        System.Diagnostics.Process.Start("C:\Users\AMAL\Downloads\Music\a.mp3")
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick









    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)



    End Sub
End Class

解决方案

If you simply auto-scroll it, it still will be as slow as presently, or actually slower, because the control will be invalidated periodically to update the data actually shown on your screen. Moreover, if you wait for longer period of time, your application will eventually exhaust the system memory.

The solution is: first of all, you have to realize that there is no such thing as miracle. You cannot add data infinitely. So, use no scrolling or regular scrolling, or control the scroll to show the newest data. But you need to remove old data by chunks of certain size (you may want to make this chunk configurable, or have only as many points as a single non-scrolling control can fit), remove older data. You also may want to dump all the data in some file, appending it periodically. Later on, your viewer can show any fragment of the history of data.

—SA


这篇关于启用自动滚动vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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