Ver Mensaje Individual
  #3 (permalink)  
Antiguo 27/02/2015, 21:10
REHome
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 16 años, 11 meses
Puntos: 8
Respuesta: Crear puerto serie.

Buenas campeón:

He comprobado que no es exactamente lo mismo.
Código:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

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

namespace WpfApplication1
{
    /// <summary>
    /// Lógica de interacción para MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // Utilizaremos un string como buffer de recepción.
        string Recibidos;

        SerialPort serialPort1 = new SerialPort();

        public MainWindow()
        {
            InitializeComponent();

            serialPort1.BaudRate = 115200;
            serialPort1.PortName = "COM4";
            serialPort1.Parity = Parity.None;
            serialPort1.DataBits = 8;
            serialPort1.StopBits = StopBits.Two;

            // Abrir puerto mientras se ejecute la aplicación.
            if (!serialPort1.IsOpen)
            {
                try
                {
                    serialPort1.Open();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }

            // Ejecutar la función REcepción por disparo del Evento ¡DataReived'.
            //serialPort1.DataReceived += new SerialDataReceivedEventHandler(Recepcion);
        }

        private void Recepcion(object sender, SerialDataReceivedEventHandler e)
        {
            // Acumular los caracteres recibidos a nuestro 'buffer' (string).
            Recibidos += serialPort1.ReadExisting();

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


        // Procesar los datos recibidos en el buffer y estraer tramas completas.
        private void Actualizar(object s, EventArgs e)
        {
            // Asignar el valor de la trama al RichTextBox.
            RichTextBox_Mensajes.DataContext = Recibidos;
        }

        private void Button_Led_8_ON_Click(object sender, RoutedEventArgs e)
        {
            byte[] mBuffer = Encoding.ASCII.GetBytes("Led_8_ON");
            serialPort1.Write(mBuffer, 0, mBuffer.Length);
        }

        private void Button_Led_8_OFF_Click(object sender, RoutedEventArgs e)
        {
            byte[] mBuffer = Encoding.ASCII.GetBytes("Led_8_OFF");
            serialPort1.Write(mBuffer, 0, mBuffer.Length);
            RichTextBox_Mensajes.DataContext = "Hola";
        }

    }
}
Error:
Error 1 Ninguna sobrecarga correspondiente a 'Recepcion' coincide con el 'System.IO.Ports.SerialDataReceivedEventHandler' delegado C:\Users\Meta\Documents\Visual Studio 2013\Projects\WpfApplication1\WpfApplication1\Main Window.xaml.cs 58 41 WpfApplication1

Error 2:
Error 2 'WpfApplication1.MainWindow' no contiene una definición de 'Invoke' ni se encontró ningún método de extensión 'Invoke' que acepte un primer argumento de tipo 'WpfApplication1.MainWindow' (¿falta una directiva using o una referencia de ensamblado?) C:\Users\Meta\Documents\Visual Studio 2013\Projects\WpfApplication1\WpfApplication1\Main Window.xaml.cs 67 18 WpfApplication1

Ojalá fuera exactamente lo mismo.

si pongo como dos comentarios a estos errores, se puede encener y apagar el Led, eso si, no puedo leer mensaje sy publicarla en el richTextBox.

Saludos.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar