lundi 11 mai 2015

vb.net using chart with serial port

I finished my project and everything work great, VB.net program read values right I want "chart" read the values from serial port and i want it to read the values 1/day Can someone help me in this! This is my code

Public Class frmDisplay2
    Dim comOpen As Boolean      'Keeps track of the port status. True = Open; False = Closed
    Dim readbuffer As String    'Buffer of whats read from the serial port

    Private Sub frmDisplay_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Get all connected serial ports
        Dim comPorts As String() = System.IO.Ports.SerialPort.GetPortNames

        If comPorts.Count < 1 Then
            'If there are not ports connected, show an error and close the program.
            MsgBox("There are no com ports available! Closing program.")
            Me.Close()
        Else
            cmbPorts.Items.AddRange(comPorts)
            cmbPorts.Text = comPorts(0)
        End If
    End Sub

    Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
        'Conenct to serial port
        DoConnect()
    End Sub

    Private Sub frmDisplay_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        'Gracefully disconnect before form closes
        DoDisconnect()
    End Sub

    Private Sub btnDisconnect_Click(sender As Object, e As EventArgs) Handles btnDisconnect.Click
        'Disconnect the serial port
        DoDisconnect()
    End Sub

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

            Try
                'Send data to a new thread to update the temperature display
                readbuffer = SerialPort1.ReadLine()
                Me.Invoke(New EventHandler(AddressOf updateTemp))
            Catch ex As Exception
                'Otherwise show error. Will display when disconnecting.
                'MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Public Sub updateTemp(ByVal sender As Object, ByVal e As System.EventArgs)
        'Update temperature display as it comes in
        Dim read As Decimal
        read = readbuffer.Replace(vbCr, "").Replace(vbLf, "")
        txtTemp.Text = read
        lstHistory.Items.Insert(0, read)

        'Check Highest Temp
        If txtHigh.Text < read Then
            txtHigh.Text = read
        End If

        'Check Lowest Temp
        If txtLow.Text > read Then
            txtLow.Text = read
        End If

        'Calculate Average
        Dim total As Decimal
        Dim count As Integer
        For Each temperature In lstHistory.Items
            total += temperature
            count = count + 1
        Next

        txtAverage.Text = total / count

        GroupBox2.Text = "History [" & count & "]" 'Running count of temperature reads. 

        total = 0 'Reset total
        count = 0 'Reset count
    End Sub

    Public Sub DoDisconnect()
        'Graceful disconnect if port is open
        If comOpen Then
            SerialPort1.DiscardInBuffer()
            SerialPort1.Close()

            'Reset our flag and controls
            comOpen = False
            btnDisconnect.Enabled = False
            btnConnect.Enabled = True
            txtBaudRate.Enabled = True
            cmbPorts.Enabled = True
        End If
    End Sub

    Public Sub DoConnect()
        'Setup the serial port connection
        With SerialPort1
            .PortName = cmbPorts.Text               'Selected Port
            .BaudRate = CInt(txtBaudRate.Text)      'Baud Rate. 9600 is default.
            .Parity = IO.Ports.Parity.None
            .DataBits = 8
            .StopBits = IO.Ports.StopBits.One
            .Handshake = IO.Ports.Handshake.None
            .RtsEnable = False
            .ReceivedBytesThreshold = 1
            .NewLine = vbCr
            .ReadTimeout = 10000
        End With

        'Try to open the selected port...
        Try
            SerialPort1.Open()
            comOpen = SerialPort1.IsOpen
        Catch ex As Exception
            comOpen = False
            MsgBox("Error Open: " & ex.Message)
        End Try

        btnDisconnect.Enabled = True
        btnConnect.Enabled = False
        txtBaudRate.Enabled = False
        cmbPorts.Enabled = False
    End Sub

    Private Sub cmbPorts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbPorts.SelectedIndexChanged

    End Sub

    Private Sub txtTemp_TextChanged(sender As Object, e As EventArgs) Handles txtTemp.TextChanged

    End Sub

    Private Sub Chart1_Click(sender As Object, e As EventArgs) Handles Chart1.Click

    End Sub

    Private Sub txtHigh_TextChanged(sender As Object, e As EventArgs) Handles txtHigh.TextChanged

    End Sub

    Private Sub lstHistory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstHistory.SelectedIndexChanged

    End Sub

    Private Sub txtAverage_TextChanged(sender As Object, e As EventArgs) Handles txtAverage.TextChanged

    End Sub
End Class

Aucun commentaire:

Enregistrer un commentaire