Ver Mensaje Individual
  #6 (permalink)  
Antiguo 27/04/2016, 08:34
REHome
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 17 años
Puntos: 8
Respuesta: Se me cuelga Windows Form

Holaaaaaaaa:

El program.cs es el típico que viene.
Código:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Arduino_In_Analogico_prueba_01
{
    static class Program
    {
        /// <summary>
        /// Punto de entrada principal para la aplicación.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
El código completo es este.
Código:
using System;
using System.Collections.Generic;
using System.Windows.Forms;

using System.IO.Ports; // No olvidar.
using System.IO;

namespace Arduino_In_Analogico_prueba_01
{
    public partial class Form1 : Form
    {
        // Utilizaremos un string como buffer de recepción.
        string Recibidos;

        public Form1()
        {
            InitializeComponent();

            if (!serialPort1.IsOpen)
            {
                try
                {
                    serialPort1.Open();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
            }

            
        }

        // Al recibir datos.
        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            // Acumula los caracteres recibidos a nuestro 'buffer' (string).
            try
            {
                Recibidos = serialPort1.ReadLine();
            }

            catch (IOException)
            {
                // Información adicional: La operación de E/S se anuló por una salida de subproceso o por una solicitud de aplicación.
            }

            // Invocar o llamar al proceso de tramas.
            Invoke(new EventHandler(Actualizar));
        }


        // Como variables de clase
        private string[] Separador = new string[] { ",", "\r", "\n", "/n" };
        private List<string> Leo_Dato = new List<string>();

        // Procesar los datos recibidos en el bufer y extraer tramas completas.
        private void Actualizar(object sender, EventArgs e)
        {
            double Voltaje = 0;
            double Porcentaje = 0;

            // En el evento
            Leo_Dato.Clear();
            Leo_Dato.AddRange(Recibidos.Split(Separador, StringSplitOptions.RemoveEmptyEntries));

            //            Se produjo una excepción de tipo 'System.ArgumentOutOfRangeException' en mscorlib.dll pero no se controló en el código del usuario


            try
            {
                label_Lectura_Potenciometro.Text = Leo_Dato[0].ToString();
            }

            catch (ArgumentOutOfRangeException)
            {
                //Información adicional: El índice estaba fuera del intervalo. Debe ser un valor no negativo e inferior al tamaño de la colección.
            }


            progressBar1.Value = Convert.ToInt32(Leo_Dato[0].ToString());
            double Dato_Voltaje = Convert.ToDouble(Leo_Dato[0]);
            double Dato_Porcentaje = Convert.ToDouble(Leo_Dato[0]);

            Voltaje = Dato_Voltaje * (5.00 / 1023.00);
            Porcentaje = Dato_Porcentaje * (100.00 / 1023.00);

            label_Voltio.Text = Voltaje.ToString("N2") + " V."; // N2 tiene dos decimales.
            label_Portentaje.Text = Porcentaje.ToString("N2") + " %";
        }
    }
}
Saludos.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar