Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2287 | - | 1 | ///============================================================================ |
2 | /// This file is part of MIKROKOPTER SERIAL CONTROL TUTORIAL. |
||
3 | /// by JOHN C. MACDONALD at Ira A. Fulton College of Engineering and Technology |
||
4 | /// (http://hdl.lib.byu.edu/1877/2747) |
||
5 | /// (http://hdl.lib.byu.edu/1877/2748) |
||
6 | ///============================================================================ |
||
2431 | - | 7 | /// made lot of changes here... |
8 | ///============================================================================ |
||
2287 | - | 9 | |
2431 | - | 10 | |
11 | |||
2287 | - | 12 | using System; |
13 | using System.Collections.Generic; |
||
14 | using System.Linq; |
||
15 | using System.Text; |
||
16 | using System.Threading.Tasks; |
||
17 | using System.Windows; |
||
18 | using System.Windows.Controls; |
||
19 | using System.Windows.Data; |
||
20 | using System.Windows.Documents; |
||
21 | using System.Windows.Input; |
||
22 | using System.Windows.Media; |
||
23 | using System.Windows.Media.Imaging; |
||
24 | using System.Windows.Navigation; |
||
25 | using System.Windows.Shapes; |
||
26 | using System.IO.Ports; |
||
27 | |||
28 | namespace MKLiveView |
||
29 | { |
||
30 | public partial class SerialPortCtrl : UserControl |
||
31 | { |
||
32 | private SerialPort port; |
||
33 | |||
34 | public event PortOpenedHandler PortOpened; |
||
35 | public delegate void PortOpenedHandler(); |
||
36 | public event PortClosedHandler PortClosed; |
||
37 | public delegate void PortClosedHandler(); |
||
38 | public event DataReceivedHandler DataReceived; |
||
39 | public delegate void DataReceivedHandler(byte[] buffer); |
||
40 | |||
41 | public SerialPortCtrl() |
||
42 | { |
||
43 | InitializeComponent(); |
||
44 | port = new SerialPort(); |
||
45 | port.ReadTimeout = 10000; |
||
46 | port.WriteTimeout = 1000; |
||
47 | textBoxStatus.Text = "closed"; |
||
48 | textBoxBaudRate.Text = "57600"; |
||
49 | textBoxDataBits.Text = port.DataBits.ToString(); |
||
50 | populateComboBoxes(); |
||
51 | setComboBoxDefaultValues(); |
||
52 | } |
||
53 | |||
54 | public SerialPort Port |
||
55 | { |
||
56 | get |
||
57 | { |
||
58 | return port; |
||
59 | } |
||
60 | } |
||
61 | |||
62 | public void Connect(bool bConn) |
||
63 | { |
||
64 | if (bConn) |
||
65 | Dispatcher.Invoke((Action)(() => buttonOpen_Click(null, null))); |
||
66 | else |
||
67 | Dispatcher.Invoke((Action)(() => buttonClose_Click(null, null))); |
||
68 | } |
||
69 | private void buttonOpen_Click(object sender, RoutedEventArgs e) |
||
70 | { |
||
71 | try |
||
72 | { |
||
73 | if (port.IsOpen == true) |
||
74 | { |
||
75 | port.DataReceived -= SerialPortDataReceived; |
||
76 | port.Close(); |
||
77 | } |
||
78 | |||
79 | lastoffset = 0; |
||
80 | append = false; |
||
81 | |||
82 | port.PortName = comboBoxPortName.Text; |
||
83 | port.BaudRate = Convert.ToInt32(textBoxBaudRate.Text); |
||
84 | port.DataBits = Convert.ToInt32(textBoxDataBits.Text); |
||
85 | port.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBoxStopBits.Text); |
||
86 | port.Parity = (Parity)Enum.Parse(typeof(Parity), comboBoxParity.Text); |
||
87 | port.DataReceived += SerialPortDataReceived; |
||
88 | |||
89 | port.Open(); |
||
90 | textBoxStatus.Text = "open"; |
||
91 | textBoxStatus.Background = Brushes.LightGreen; |
||
92 | if (PortOpened != null) |
||
93 | { |
||
94 | // PortOpened(port.BaseStream); |
||
95 | PortOpened(); |
||
96 | } |
||
97 | } |
||
98 | catch (Exception ex) |
||
99 | { |
||
100 | textBoxStatus.Text = "error: " + ex; |
||
101 | textBoxStatus.Background = Brushes.Coral; |
||
102 | } |
||
103 | } |
||
104 | private void buttonClose_Click(object sender, RoutedEventArgs e) |
||
105 | { |
||
106 | port.DataReceived -= SerialPortDataReceived; |
||
107 | port.Close(); |
||
108 | if (PortClosed != null) |
||
109 | PortClosed(); |
||
110 | textBoxStatus.Text = "closed"; |
||
111 | textBoxStatus.Background = Brushes.LightGray; |
||
112 | lastoffset = 0; |
||
113 | append = false; |
||
114 | } |
||
115 | |||
116 | public string[] getPortNameValues() |
||
117 | { |
||
118 | try |
||
119 | { |
||
120 | return SerialPort.GetPortNames().Reverse().ToArray(); |
||
121 | } |
||
122 | catch |
||
123 | { |
||
124 | return new string[0]; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | public string[] getStopBitValues() |
||
129 | { |
||
130 | return Enum.GetNames(typeof(StopBits)); |
||
131 | } |
||
132 | |||
133 | public string[] getParityValues() |
||
134 | { |
||
135 | return Enum.GetNames(typeof(Parity)); |
||
136 | } |
||
137 | |||
138 | private void populateComboBoxes() |
||
139 | { |
||
140 | comboBoxPortName.ItemsSource = getPortNameValues(); |
||
141 | comboBoxStopBits.ItemsSource = getStopBitValues(); |
||
142 | comboBoxParity.ItemsSource = getParityValues(); |
||
143 | } |
||
144 | |||
145 | private void setComboBoxDefaultValues() |
||
146 | { |
||
147 | try { comboBoxPortName.SelectedIndex = 0; } |
||
148 | catch { } |
||
149 | try { comboBoxStopBits.SelectedIndex = 1; } |
||
150 | catch { } |
||
151 | try { comboBoxParity.SelectedIndex = 0; } |
||
152 | catch { } |
||
153 | } |
||
154 | |||
155 | byte[] messageBuffer = new byte[4096]; |
||
156 | int lastoffset = 0; |
||
157 | bool append = false; |
||
158 | byte sentinel = Convert.ToByte('\r'); |
||
159 | |||
160 | object oLock = 0; |
||
161 | private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e) |
||
162 | { |
||
163 | System.Threading.Monitor.Enter(oLock); |
||
164 | int offset = 0; |
||
165 | try |
||
166 | { |
||
167 | byte[] buffer = new byte[port.BytesToRead]; |
||
168 | port.Read(buffer, 0, port.BytesToRead); |
||
169 | |||
170 | bool bFound = false; |
||
171 | for (int i = 0; (i < buffer.Length) && port.IsOpen; i++) |
||
172 | { |
||
173 | if (buffer[i] == sentinel) |
||
174 | { |
||
175 | if (!append) |
||
176 | { |
||
177 | byte[] message = new byte[i + 1 - offset]; |
||
178 | if (buffer[offset] != '#') |
||
179 | System.Diagnostics.Debug.Print(buffer[offset].ToString()); |
||
180 | Buffer.BlockCopy(buffer, offset, message, 0, i + 1 - offset); |
||
181 | if (DataReceived != null) |
||
182 | DataReceived(message); |
||
183 | } |
||
184 | else |
||
185 | { |
||
186 | |||
187 | Buffer.BlockCopy(buffer, 0, messageBuffer, lastoffset, i + 1); |
||
188 | byte[] message = new byte[lastoffset + i + 1]; |
||
189 | Buffer.BlockCopy(messageBuffer, 0, message, 0, message.Length); |
||
190 | messageBuffer.Initialize(); |
||
191 | append = false; |
||
192 | lastoffset = 0; |
||
193 | // System.Diagnostics.Debug.Print(message.Length.ToString()); |
||
194 | // System.Diagnostics.Debug.Print(BitConverter.ToString(message)); |
||
195 | if (DataReceived != null) |
||
196 | DataReceived(message); |
||
197 | } |
||
198 | offset = i + 1; |
||
199 | if (buffer.Length > i + 1) |
||
200 | { |
||
201 | //offset = i + 1; |
||
202 | bFound = false; |
||
203 | } |
||
204 | else |
||
205 | { |
||
206 | // offset = 0; |
||
207 | bFound = true; |
||
208 | } |
||
209 | |||
210 | |||
211 | } |
||
212 | |||
213 | } |
||
214 | if (!bFound && port.IsOpen) |
||
215 | { |
||
216 | if (!append) |
||
217 | { |
||
218 | if (buffer[offset] != '#') |
||
219 | System.Diagnostics.Debug.Print(buffer[offset].ToString("X2")); |
||
220 | Buffer.BlockCopy(buffer, offset, messageBuffer, 0, buffer.Length - offset); |
||
221 | lastoffset = (buffer.Length - offset); |
||
222 | } |
||
223 | else |
||
224 | { |
||
225 | if (messageBuffer[0] != '#') |
||
226 | System.Diagnostics.Debug.Print(buffer[0].ToString("X2")); |
||
227 | Buffer.BlockCopy(buffer, offset, messageBuffer, lastoffset, buffer.Length - offset); |
||
228 | |||
229 | lastoffset += (buffer.Length - offset); |
||
230 | } |
||
231 | append = true; |
||
232 | |||
233 | } |
||
234 | } |
||
235 | catch (Exception ex) |
||
236 | { |
||
237 | System.Diagnostics.Debug.Print(ex.Message); |
||
238 | } |
||
239 | finally |
||
240 | { |
||
241 | System.Threading.Monitor.Exit(oLock); |
||
242 | } |
||
243 | |||
244 | |||
245 | } |
||
246 | |||
247 | private void btnRefresh_Click(object sender, RoutedEventArgs e) |
||
248 | { |
||
249 | if (!port.IsOpen) |
||
250 | { |
||
251 | // comboBoxPortName.Items.Clear(); |
||
252 | comboBoxPortName.ItemsSource = getPortNameValues(); |
||
253 | } |
||
254 | else |
||
255 | MessageBox.Show("Port has to be closed!"); |
||
256 | } |
||
257 | |||
258 | } |
||
259 | |||
260 | } |