Ver Mensaje Individual
  #11 (permalink)  
Antiguo 01/05/2016, 01:43
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

Hola:

Ahora añadí un timer que lee a 1000 ms frente a los 100 ms que envía Arduino.

Se me cuelga a veces, antes era siempre.

Código C:
Ver original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4.  
  5. using System.IO.Ports; // No olvidar.
  6. using System.IO;
  7.  
  8. namespace Arduino_In_Analogico_prueba_01
  9. {
  10.     public partial class Form1 : Form
  11.     {
  12.         // Utilizaremos un string como buffer de recepción.
  13.         string Recibidos;
  14.  
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.  
  19.             if (!serialPort1.IsOpen)
  20.             {
  21.                 try
  22.                 {
  23.                     serialPort1.Open();
  24.                 }
  25.                 catch (System.Exception ex)
  26.                 {
  27.                     MessageBox.Show(ex.ToString());
  28.                 }
  29.  
  30.                 serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
  31.             }
  32.         }
  33.  
  34.  
  35.         // Al recibir datos.
  36.         private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  37.         {
  38.  
  39.             // Acumula los caracteres recibidos a nuestro 'buffer' (string).
  40.             try
  41.             {
  42.                 // Recibidos = serialPort1.ReadLine();
  43.             }
  44.  
  45.             catch (IOException)
  46.             {
  47.                 // Información adicional: La operación de E/S se anuló por una salida de subproceso o por una solicitud de aplicación.
  48.                 return;
  49.             }
  50.  
  51.             catch(Exception)
  52.             {
  53.                 return;
  54.             }
  55.  
  56.             // Invocar o llamar al proceso de tramas.
  57.             Invoke(new EventHandler(Actualizar));
  58.         }
  59.  
  60.  
  61.         // Como variables de clase
  62.         private string[] Separador = new string[] { ",", "\r", "\n", "/n" };
  63.         private List<string> Leo_Dato = new List<string>();
  64.  
  65.         // Procesar los datos recibidos en el bufer y extraer tramas completas.
  66.         private void Actualizar(object sender, EventArgs e)
  67.         {
  68.             Recibidos = serialPort1.ReadLine();
  69.  
  70.             double Voltaje = 0;
  71.             double Porcentaje = 0;
  72.  
  73.             // En el evento
  74.             Leo_Dato.Clear();
  75.             Leo_Dato.AddRange(Recibidos.Split(Separador, StringSplitOptions.RemoveEmptyEntries));
  76.  
  77.             //            Se produjo una excepción de tipo 'System.ArgumentOutOfRangeException' en mscorlib.dll pero no se controló en el código del usuario
  78.  
  79.  
  80.             try
  81.             {
  82.                 label_Lectura_Potenciometro.Text = Leo_Dato[0].ToString();
  83.             }
  84.  
  85.             catch (ArgumentOutOfRangeException)
  86.             {
  87.                 //Información adicional: El índice estaba fuera del intervalo. Debe ser un valor no negativo e inferior al tamaño de la colección.
  88.             }
  89.  
  90.  
  91.             progressBar1.Value = Convert.ToInt32(Leo_Dato[0].ToString());
  92.             double Dato_Voltaje = Convert.ToDouble(Leo_Dato[0]);
  93.             double Dato_Porcentaje = Convert.ToDouble(Leo_Dato[0]);
  94.  
  95.             Voltaje = Dato_Voltaje * (5.00 / 1023.00);
  96.             Porcentaje = Dato_Porcentaje * (100.00 / 1023.00);
  97.  
  98.             label_Voltio.Text = Voltaje.ToString("N2") + " V."; // N2 tiene dos decimales.
  99.             label_Portentaje.Text = Porcentaje.ToString("N2") + " %";
  100.         }
  101.  
  102.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  103.         {
  104.  
  105.             timer1.Stop();
  106.  
  107.             if (serialPort1.IsOpen) // ¿El puerto está abierto?
  108.             {
  109.                 serialPort1.Close(); // Puerto cerrado.
  110.             }
  111.         }
  112.     }
  113. }

Seguiré con el tema de los hilos, pero cuesta entender como programarlo.

Saludos.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar