Bas Knippels

Bas Knippels

  • NA
  • 9
  • 620

How let a class send a signal to the main class

Aug 17 2017 5:00 AM
 The question is about the serial class. One of the class's methods is void port_dataReceived(). This function is a serial event and get triggered when a byte is received over the serial port.
 
This byte must be processed in the rest of the code. Right now I can acces it with a pushbutton and that works but I need to this to work automatically.
 
The bytes can contain instructions which places text on the userinterface. For instance "Textblok.Text += received_byte;"
 
How can I let the serial class add the byte in the the textblock? 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows;  
  7. using System.Diagnostics;  
  8. using System.Windows.Controls;  
  9. using System.Windows.Data;  
  10. using System.Windows.Documents;  
  11. using System.Windows.Input;  
  12. using System.Windows.Media;  
  13. using System.Windows.Media.Imaging;  
  14. using System.Windows.Navigation;  
  15. using System.Windows.Shapes;  
  16. using System.IO.Ports;  
  17.   
  18.   
  19. class Serial  
  20. {  
  21.     private SerialPort port = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One);  
  22.     String received;  
  23.   
  24.     public Serial()  
  25.     {  
  26.         port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);  
  27.         port.Open();  
  28.     }  
  29.   
  30.     public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)  
  31.     {  
  32.         received = port.ReadExisting();  
  33.         Debug.Write(received);  
  34.     }  
  35.   
  36.     public String getReceived()  
  37.     {  
  38.         return received;  
  39.     }  
  40. }     
  41.   
  42. namespace WpfApplication1  
  43. {  
  44.   
  45.     /// <summary>  
  46.     /// Interaction logic for MainWindow.xaml  
  47.     /// </summary>  
  48.     public partial class MainWindow : Window  
  49.     {  
  50.         Serial serial = new Serial();  
  51.         public MainWindow()  
  52.         {  
  53.             InitializeComponent();  
  54.         }  
  55.   
  56.         private void pagedown_Click(object sender, RoutedEventArgs e)  
  57.         {  
  58.             Textblok.Text = serial.getReceived();  
  59.             Debug.Write("hello world");  
  60.         }  
  61.   
  62.         private void menu_Click(object sender, RoutedEventArgs e)  
  63.         {  
  64.   
  65.         }  
  66.   
  67.         private void pageup_Click(object sender, RoutedEventArgs e)  
  68.         {  
  69.   
  70.         }  
  71.   
  72.         private void stop_Click(object sender, RoutedEventArgs e)  
  73.         {  
  74.   
  75.         }  
  76.   
  77.         private void step_Click(object sender, RoutedEventArgs e)  
  78.         {  
  79.   
  80.         }  
  81.   
  82.         private void start_Click(object sender, RoutedEventArgs e)  
  83.         {  
  84.   
  85.         }  
  86.     }      
  87. }  
 

Answers (2)