-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
122 lines (112 loc) · 4.12 KB
/
MainWindow.xaml.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.IO.Ports;
using System.Linq;
using System.Windows;
using System.Windows.Media;
namespace LedWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
SerialPort serialPort1;
string[] serialPorts2;
public MainWindow()
{
InitializeComponent();
InitializedSerialPorts();//custom
}
private void InitializedSerialPorts() //custom
{
serialPorts2 = SerialPort.GetPortNames();//Using the System.IO.Ports.SerialPort.GetPortNames() it will retrieve the all available serial port names.
if ( serialPorts2.Count() != 0 )
{
foreach ( string serial in serialPorts2 )
{
var serialItem = SerialPortComboBox.Items;
if ( !serialItem.Contains(serial) )//contain used for comparing string with substring
{
SerialPortComboBox.Items.Add(serial);
}
}
SerialPortComboBox.SelectedItem = serialPorts2[0];
}
}
// WPF to arduino Connection
bool isConnectedToArduino = false;
private void ConnectToArduino()
{
try
{
string selectedSerialPort = SerialPortComboBox.SelectedItem.ToString(); //get selected port
serialPort1 = new SerialPort(selectedSerialPort , 9600);
serialPort1.Open();
SerialPortConnection.Content = "Disconnect";
LedControlPanel.IsEnabled = true;
isConnectedToArduino = true;
}
catch ( UnauthorizedAccessException )
{
MessageBox.Show("The selected serial port is busy" , "Busy" , MessageBoxButton.OK , MessageBoxImage.Stop);
}
catch ( NullReferenceException )
{
MessageBox.Show("There is no serial port!" , "Empty Serial port" , MessageBoxButton.OK , MessageBoxImage.Exclamation);
}
catch ( Exception ex )
{
MessageBox.Show(ex.ToString());
}
}
private void DisconnectFromArduino()
{
SerialPortConnection.Content = "Connected";
LedControlPanel.IsEnabled = false;
isConnectedToArduino = false;
ResetControl();
serialPort1.Close();
}
private void ResetControl()
{
LedOff.Background = Brushes.SaddleBrown;
LedON.Background = Brushes.Green;
SliderBrightness.Value = 0;
lblSliderBrightness.Content = "0";
}
private void RefreshBtn_Click(object sender , RoutedEventArgs e)
{
InitializedSerialPorts();
}
private void SerialPortConnection_Click(object sender , RoutedEventArgs e)
{
if ( !isConnectedToArduino )
{
ConnectToArduino();
}
else
{
DisconnectFromArduino();
}
}
private void LedOff_Click(object sender , RoutedEventArgs e)
{
serialPort1.Write("256");
LedOff.Background = Brushes.DarkSeaGreen;
LedON.Background = Brushes.Green;
}
private void LedON_Click(object sender , RoutedEventArgs e)
{
serialPort1.Write("257");
LedOff.Background = Brushes.SaddleBrown;
LedON.Background = Brushes.YellowGreen;
}
private void SliderBrightness_ValueChanged(object sender , RoutedPropertyChangedEventArgs<double> e)
{
int sliderValue = (int) SliderBrightness.Value;
serialPort1.Write(sliderValue.ToString());//send the value of slider from arduino
serialPort1.Write("-");//as no data to send
lblSliderBrightness.Content = sliderValue.ToString();
}
}
}