Encontrar la solución de recibir datos.

(1/1)

Meta:
Hola:

Tengo este código con WPF C# 2013. Puedo enviar datos pero no recibirlos y se muestre en RichTextBox.
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";
       }
 
   }
}
 

Alguna solución donde está el problema.

Saludos.

Navegación

[0] Índice de Mensajes