-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMainWindowSerial.cs
92 lines (85 loc) · 3.67 KB
/
MainWindowSerial.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.IO.Ports;
using System.Windows;
using System.Windows.Threading;
namespace ESPEDfGK
{
public enum ShortParity
{
N = 0,
O = 1,
E = 2,
M = 3,
S = 4,
}
//*****************************************************************************************
public partial class MainWindow : Window
{
// lots of this is cut/paste from https://github.com/minhncedutw/wpf-serial-communication
SerialPort serialPort = new SerialPort();
string recievedData;
//*****************************************************************************************
private void BtnOpenSermonitor(object sender, RoutedEventArgs e)
{
try
{
serialPort.PortName = cBoxComPort.Text;
serialPort.BaudRate = Convert.ToInt32(cBoxBaudRate.Text);
serialPort.DataBits = Convert.ToInt32(cBoxDataBits.Text);
serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cBoxStopBits.Text);
serialPort.Parity = (Parity)Enum.Parse(typeof(ShortParity), cBoxParityBits.Text);
serialPort.Open(); // Open port.
serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataRecieved);
btnOpen.Visibility = Visibility.Collapsed;
btnClose.Visibility = Visibility.Visible;
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
//*****************************************************************************************
private void BtnCloseSermonitor(object sender, RoutedEventArgs e)
{
btnOpen.Visibility = Visibility.Visible;
btnClose.Visibility = Visibility.Collapsed;
serialPort.Close();
}
//*****************************************************************************************
private delegate void UpdateUiTextDelegate(string text);
//*****************************************************************************************
private void serialPort_DataRecieved(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
// Collecting the characters received to our 'buffer' (string).
recievedData = serialPort.ReadExisting();
// Delegate a function to display the received data.
Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(DataWrited), recievedData);
}
//*****************************************************************************************
private void DataWrited(string text)
{
// Limit text
while (tBoxInData.Text.Length > 50 * 1024)
{
tBoxInData.Text = tBoxInData.Text.Remove(0, 1024);
}
tBoxInData.Text += text;
}
//*****************************************************************************************
private void BtnClearSermonitor(object sender, RoutedEventArgs e)
{
tBoxInData.Text = "";
}
//*****************************************************************************************
private void BtnCopyToAnalyzer(object sender, RoutedEventArgs e)
{
TBStackdump.Text = tBoxInData.Text;
}
//*****************************************************************************************
private void BtnCopyToClipboard(object sender, RoutedEventArgs e)
{
Clipboard.SetText(tBoxInData.Text);
}
}
}