Click here to Skip to main content
14,603,943 members
Rate this:
Please Sign up or sign in to vote.
See more:
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
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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
   
Comments
Amal anjula 16-Jun-15 20:56pm
   
Ok sir. How clear all data in every 5mins? Data buffer, chart also
Sergey Alexandrovich Kryukov 16-Jun-15 23:21pm
   
Not "every 5 miniute". When number of points reaches some value. Deliver data in cycle, each cycle remove old points.
—SA
Amal anjula 16-Jun-15 23:59pm
   
can u edit my code pls
   
I'm afraid it would not make any sense.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100