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

Hola:

Gracias, aún así me da un error en el this.Invoke.

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 += Recepcion;
        }

        private void Recepcion(object sender, SerialDataReceivedEventArgs 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;

            var doc = new System.Windows.Documents.FlowDocument();
            doc.Blocks.Add(new Paragraph(new Run(Recibidos)));
            RichTextBox_Mensajes.Document = doc;
        }

        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);
        }

    }
}
Error 1 '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


Saludos.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar