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

Hola:

Tu código es corto, el mio que hice es largo y por fin funciona aunque a veces se le va la oya.

Código:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

namespace Entrada_Arduino_AWF_3_CS
{
    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(Recepcion);
            }
        }
        // Al recibir datos.
        private void Recepcion(object sender, SerialDataReceivedEventArgs e)
        {
            // Acumula los caracteres recibidos a nuestro 'buffer' (string).
            Recibidos += serialPort1.ReadExisting();

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

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

            // Asignar el valor de la trama al richTextBox.
            richTextBox1.Text += Recibidos;

            // Selecciona la posición final para leer los mensajes entrantes.
            richTextBox1.SelectionStart = richTextBox1.Text.Length;

            // Mantiene el scroll en la entrada de cada mensaje.
            richTextBox1.ScrollToCaret();

            char[] Delimitador = { ' ', '\r', '\n' };

            string[] Palabras = Recibidos.Split(Delimitador);


            foreach (string Comandos in Palabras)
            {
                switch (Comandos)
                {
                    case "1=ON":
                        pictureBox1.Image = Properties.Resources.Led_rojo_encendido;
                        Recibidos = "";
                        break;

                    case "1=OFF":
                        pictureBox1.Image = Properties.Resources.Led_rojo_apagado;
                        Recibidos = "";
                        break;

                    case "2=ON":
                        pictureBox2.Image = Properties.Resources.Led_rojo_encendido;
                        Recibidos = "";
                        break;

                    case "2=OFF":
                        pictureBox2.Image = Properties.Resources.Led_rojo_apagado;
                        Recibidos = "";
                        break;

                    case "3=ON":
                        pictureBox3.Image = Properties.Resources.Led_rojo_encendido;
                        Recibidos = "";
                        break;

                    case "3=OFF":
                        pictureBox3.Image = Properties.Resources.Led_rojo_apagado;
                        Recibidos = "";
                        break;

                    case "4=ON":
                        pictureBox4.Image = Properties.Resources.Led_rojo_encendido;
                        richTextBox1.Text += " " + DateTime.Now.ToString();
                        Recibidos = "";
                        break;

                    case "4=OFF":
                        pictureBox4.Image = Properties.Resources.Led_rojo_apagado;
                        richTextBox1.Text += " " + DateTime.Now.ToString();
                        Recibidos = "";
                        break;
            }
          }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen) // ¿El puerto está abierto?
            {
                serialPort1.Close(); // Puerto cerrado.
            }
        }

        void Actualizar()
        {
            byte[] mBuffer = Encoding.ASCII.GetBytes("ACTUALIZAR"); // Envía comando ACTUALIZAR por el puerto.
            serialPort1.Write(mBuffer, 0, mBuffer.Length);
        }

        private void button_Actualizar_Click(object sender, EventArgs e)
        {
            Actualizar();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Actualizar();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear(); // Limpiar contenido del richTextBox.
            Recibidos = "";
        }
    }
}
Saludos.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar