my code stock.com

Snippet options

Download: Download snippet as medidor-cpu-software.vbs.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

Imports System.Diagnostics
Imports System.Math
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        SerialPort1.Write(TextBox1.Text) 'Acá el programa envía al puerto serial el texto que se muestra en pantalla
        System.Threading.Thread.Sleep(200)
    End Sub
    Private m_PerformanceCounter As New _
        System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", "_Total")

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each sp As String In My.Computer.Ports.SerialPortNames 'Obtenemos los puertos serial disponibles
            ComboBox1.Items.Add(sp) 'Se añaden los puertos a el combobox
        Next
        If ComboBox1.SelectedIndex = -1 Then 'Hacemos el el combobox muestre el primer item y no esté en blanco
            ComboBox1.SelectedIndex = 0
        End If
        Label3.Text = ComboBox1.Text 'Acá indicamos el puerto, por defecto será siempre el primer puerto del combobox
        Dim cpu As Double
        cpu = m_PerformanceCounter.NextValue 'Obtenemos el uso de la CPU
        Try
            If cpu <= 9 Then
                Label2.Text = "Uso CPU: 0" & Round(cpu, 0) & "%" 'Usamos la funcion "Round" para aproximar el numero a un entero
            End If
            If cpu >= 10 Then
                Label2.Text = "Uso CPU: " & Round(cpu, 0) & "%"
            End If
        Catch ex As Exception
        End Try
        Try
            'Todo esto es para la conexión vía serial
            SerialPort1.BaudRate = 9600
            SerialPort1.DataBits = 8
            SerialPort1.Parity = IO.Ports.Parity.None
            SerialPort1.StopBits = IO.Ports.StopBits.One
            SerialPort1.PortName = Label3.Text
            SerialPort1.Open()
            Label2.Text = "Uso CPU: " & Round(cpu, 1) & "%"
            SerialPort1.Write(Label2.Text)
            System.Threading.Thread.Sleep(200)
        Catch ex As Exception
            MsgBox("Seleccione un puerto")
        End Try



    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        'Este es un pequeño ciclo que va actualizando el uso de la CPU cada 0.5s
        Try
            Dim cpu As Double
            cpu = m_PerformanceCounter.NextValue
            If cpu <= 9 Then
                Label2.Text = "Uso CPU: 0" & Round(cpu, 0) & "%"
            End If
            If cpu >= 10 Then
                Label2.Text = "Uso CPU: " & Round(cpu, 0) & "%"
            End If

            SerialPort1.Write(Label2.Text)
            System.Threading.Thread.Sleep(200)
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        'Esta función es la que nos permite cambiar el puerto serial
        Label3.Text = ComboBox1.Text

        Dim cpu As Double
        cpu = m_PerformanceCounter.NextValue
        Try
            If cpu <= 9 Then
                Label2.Text = "Uso CPU: 0" & Round(cpu, 0) & "%"
            End If
            If cpu >= 10 Then
                Label2.Text = "Uso CPU: " & Round(cpu, 0) & "%"
            End If
        Catch ex As Exception
        End Try
        Try
            SerialPort1.BaudRate = 9600
            SerialPort1.DataBits = 8
            SerialPort1.Parity = IO.Ports.Parity.None
            SerialPort1.StopBits = IO.Ports.StopBits.One
            SerialPort1.PortName = Label3.Text
            SerialPort1.Open()
            Label2.Text = "Uso CPU: " & Round(cpu, 1) & "%"
            SerialPort1.Write(Label2.Text)
            System.Threading.Thread.Sleep(200)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim x As Double
        x = 1
        'Limpiamos la pantalla
        While x < 64
            SerialPort1.Write(" ")
            x = x + 1
        End While
    End Sub
End Class

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.