Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
2
Reply
How let a class send a signal to the main class
Bas Knippels
Aug 17 2017 5:00 AM
193
Reply
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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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)
{
}
}
}
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
2
)
Next Recommended Forum
how to get values from asp gridview to code behind to updat
Login form using data base