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