Passing value from a label to the Arduino via serial

1

I developed a WindowsForm that contains a label that performs the CPU usage check and displays its variations, and a button that connects the software with the arduino to the available serial port. I also created a textBoxReceber, to check if the arduino is getting any value, but that's the problem, I'm not able to send the updated values to the arduino. Follow the snippet of code.

private void startMonitor()
    {
        MachineMonitor machineMonitor = new MachineMonitor("B-NTIDDT001");
        while (true)
        {

            var ram = "Mem RAM: " + machineMonitor.GetUsageMemoryPercentage() + " %";
            var cpu = "CPU: " + machineMonitor.GetUsageCPUPercentage() + " %";

            if (this.lblCpu.InvokeRequired)
            {
                lblCpu.Invoke((MethodInvoker)delegate () { this.lblCpu.Text = cpu; });
                this.ValorR = cpu;
            }
            else
            {
                this.lblCpu.Text = cpu;
            }

            Thread.Sleep(1000);
        }

Here is the method that takes the values of CPU and RAM usage and passes it to lblCpu.

private void btConectar_Click(object sender, EventArgs e)
    {
        if (serialPort.IsOpen == false)
        {
            try
            {
                serialPort.PortName = comboBox1.Items[comboBox1.SelectedIndex].ToString();
                serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
                serialPort.Open();
            }
            catch
            {
                return;
            }
            if (serialPort.IsOpen == true)
            {
                btConectar.Text = "Desconectar";
                comboBox1.Enabled = false;
            }
        }
        else
        {
            try
            {
                serialPort.Close();
                comboBox1.Enabled = true;
                btConectar.Text = "Conectar";
            }
            catch
            {
                return;
            }
        }
    }

My connection to serial ports is available.

private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        lblCpu.Text = RxString;
        RxString = serialPort.ReadExisting();
        this.Invoke(new EventHandler(trataDadoRecebido));
    }

    private void trataDadoRecebido(object sender, EventArgs e)
    {
        textBoxReceber.AppendText(RxString);
    }

As you can see, I can not get the data from lblCpu and pass it on to my Arduino. I made a lblCpu_Click method, then it worked but I do not want to have to click on lblCpu all the time, I would like it to update constantly without my interference.

    
asked by anonymous 10.03.2017 / 14:26

1 answer

0

For this you should use a timer and a time loop, ie you will choose a specific time, for example every 1 minute for the "lblCpu_Click" method to be triggered.

    
24.08.2017 / 00:40