Ver Mensaje Individual
  #7 (permalink)  
Antiguo 04/12/2015, 09:04
REHome
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 17 años
Puntos: 8
Respuesta: [C#] Cortar por lo sano

Hola:

Probando el código.
Código C++:
Ver original
  1. var si = Properties.Resources.Led_rojo_encendido;
  2.             var no = Properties.Resources.Led_rojo_apagado;
  3.  
  4.             var valores = Recibidos.Split(' ')
  5.                                    .Select(x => x.Split('='))
  6.                                    .Select(x => new
  7.                                    {
  8.                                        Index = int.Parse(x[0]),
  9.                                        Value = x[1] == "ON"
  10.                                    });
  11.  
  12.             foreach (var v in valores)
  13.             {
  14.                 this.leds[v.Index].Image = v.Value ? si : no;
  15.             }

Me sale este error.

Cita:
Error CS1061 'Form1' no contiene una definición para 'leds' ni se encuentra ningún método de extensión 'leds' que acepte un primer argumento del tipo 'Form1' (¿falta alguna directiva using o una referencia de ensamblado?) Entrada_Arduino_AWF_3_CS C:\Users\Usuario\Documents\Visual Studio 2015\Projects\Entrada_Arduino_AWF_3_CS\Entrada_Ard uino_AWF_3_CS\Form1.cs 125
Código completo.
Código C++:
Ver original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. using System.IO.Ports; // No olvidar.
  12.  
  13. namespace Entrada_Arduino_AWF_3_CS
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         // Utilizaremos un string como buffer de recepción.
  18.         string Recibidos;
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.  
  24.             if (!serialPort1.IsOpen)
  25.             {
  26.                 try
  27.                 {
  28.                     serialPort1.Open();
  29.                 }
  30.                 catch (System.Exception ex)
  31.                 {
  32.                     MessageBox.Show(ex.ToString());
  33.                 }
  34.  
  35.                 serialPort1.DataReceived += new SerialDataReceivedEventHandler(Recepcion);
  36.             }
  37.  
  38.             //Inicializar la lista, esto debe ocurrir DESPUES de InitializeComponent()
  39.             this.leds = new[] { this.pictureBox1, this.pictureBox2, this.pictureBox3, this.pictureBox4 }.ToList();
  40.         }
  41.         // Al recibir datos.
  42.         private void Recepcion(object sender, SerialDataReceivedEventArgs e)
  43.         {
  44.             // Acumula los caracteres recibidos a nuestro 'buffer' (string).
  45.             Recibidos += serialPort1.ReadExisting();
  46.  
  47.             // Invocar o llamar al proceso de tramas.
  48.             Invoke(new EventHandler(Actualizar));
  49.         }
  50.  
  51.         // Procesar los datos recibidos en el bufer y extraer tramas completas.
  52.         private void Actualizar(object sender, EventArgs e)
  53.         {
  54.  
  55.             // Asignar el valor de la trama al richTextBox.
  56.             richTextBox1.Text += Recibidos;
  57.  
  58.             // Selecciona la posición final para leer los mensajes entrantes.
  59.             richTextBox1.SelectionStart = richTextBox1.Text.Length;
  60.  
  61.             // Mantiene el scroll en la entrada de cada mensaje.
  62.             richTextBox1.ScrollToCaret();
  63.  
  64.             //  char[] Delimitador = { ' ', '\r', '\n' };
  65.  
  66.             //  string[] Palabras = Recibidos.Split(Delimitador);
  67.  
  68.  
  69.             //  foreach (string Comandos in Palabras)
  70.             //  {
  71.             //      switch (Comandos)
  72.             //      {
  73.             //          case "1=ON":
  74.             //              pictureBox1.Image = Properties.Resources.Led_rojo_encendido;
  75.             //              Recibidos = "";
  76.             //              break;
  77.  
  78.             //          case "1=OFF":
  79.             //              pictureBox1.Image = Properties.Resources.Led_rojo_apagado;
  80.             //              Recibidos = "";
  81.             //              break;
  82.  
  83.             //          case "2=ON":
  84.             //              pictureBox2.Image = Properties.Resources.Led_rojo_encendido;
  85.             //              Recibidos = "";
  86.             //              break;
  87.  
  88.             //          case "2=OFF":
  89.             //              pictureBox2.Image = Properties.Resources.Led_rojo_apagado;
  90.             //              Recibidos = "";
  91.             //              break;
  92.  
  93.             //          case "3=ON":
  94.             //              pictureBox3.Image = Properties.Resources.Led_rojo_encendido;
  95.             //              Recibidos = "";
  96.             //              break;
  97.  
  98.             //          case "3=OFF":
  99.             //              pictureBox3.Image = Properties.Resources.Led_rojo_apagado;
  100.             //              Recibidos = "";
  101.             //              break;
  102.  
  103.             //          case "4=ON":
  104.             //              pictureBox4.Image = Properties.Resources.Led_rojo_encendido;
  105.             //              Recibidos = "";
  106.             //              break;
  107.  
  108.             //          case "4=OFF":
  109.             //              pictureBox4.Image = Properties.Resources.Led_rojo_apagado;
  110.             //              Recibidos = "";
  111.             //              break;
  112.             //  }
  113.             //}
  114.  
  115.             var si = Properties.Resources.Led_rojo_encendido;
  116.             var no = Properties.Resources.Led_rojo_apagado;
  117.  
  118.             var valores = Recibidos.Split(' ')
  119.                                    .Select(x => x.Split('='))
  120.                                    .Select(x => new
  121.                                    {
  122.                                        Index = int.Parse(x[0]),
  123.                                        Value = x[1] == "ON"
  124.                                    });
  125.  
  126.             foreach (var v in valores)
  127.             {
  128.                 this.leds[v.Index].Image = v.Value ? si : no;
  129.             }
  130.  
  131.             //richTextBox1.Text += "        " + DateTime.Now.ToString() + "\r";
  132.         }
  133.  
  134.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  135.         {
  136.             if (serialPort1.IsOpen) // ¿El puerto está abierto?
  137.             {
  138.                 serialPort1.Close(); // Puerto cerrado.
  139.             }
  140.         }
  141.  
  142.         void Actualizar()
  143.         {
  144.             byte[] mBuffer = Encoding.ASCII.GetBytes("ACTUALIZAR"); // Envía comando ACTUALIZAR por el puerto.
  145.             serialPort1.Write(mBuffer, 0, mBuffer.Length);
  146.         }
  147.  
  148.         private void button_Actualizar_Click(object sender, EventArgs e)
  149.         {
  150.             Actualizar();
  151.         }
  152.  
  153.         private void Form1_Load(object sender, EventArgs e)
  154.         {
  155.             Actualizar();
  156.         }
  157.  
  158.         private void button2_Click(object sender, EventArgs e)
  159.         {
  160.             richTextBox1.Clear(); // Limpiar contenido del richTextBox.
  161.             Recibidos = "";
  162.         }
  163.     }
  164. }

Saludos.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar