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?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Diagnostics;
- 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;
- class Serial
- {
- private SerialPort port = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One);
- String received;
- public Serial()
- {
- port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
- port.Open();
- }
- public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- received = port.ReadExisting();
- Debug.Write(received);
- }
- public String getReceived()
- {
- return received;
- }
- }
- namespace WpfApplication1
- {
- ///
- /// Interaction logic for MainWindow.xaml
- ///
- public partial class MainWindow : Window
- {
- Serial serial = new Serial();
- public MainWindow()
- {
- InitializeComponent();
- }
- private void pagedown_Click(object sender, RoutedEventArgs e)
- {
- Textblok.Text = serial.getReceived();
- Debug.Write("hello world");
- }
- private void menu_Click(object sender, RoutedEventArgs e)
- {
- }
- private void pageup_Click(object sender, RoutedEventArgs e)
- {
- }
- private void stop_Click(object sender, RoutedEventArgs e)
- {
- }
- private void step_Click(object sender, RoutedEventArgs e)
- {
- }
- private void start_Click(object sender, RoutedEventArgs e)
- {
- }
- }
- }

Nilesh ShahRank #42 · 34.2K reputationPosted Aug 17, 2017, 5:16 AM EDT
what is the code in SerialDataReceivedEventHandler?
this one should execute when a byte is received, try to put a message box here to see if it gets fired, if it gets, then your code to assign it to textbox can be written here
Bas Knippels9 reputationPosted Aug 17, 2017, 10:10 PM EDT