-
Notifications
You must be signed in to change notification settings - Fork 1
/
OptionsWindowForm.cs
382 lines (354 loc) · 13.9 KB
/
OptionsWindowForm.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;
namespace SerialSuite
{
public partial class OptionsWindowForm : Form
{
readonly MainWindowForm mainWindow; // Initialises MainWindowForm
readonly SerialPort sPort = new SerialPort(); // Used to copy create a template of the types
readonly UserSettings userSettings = new UserSettings(); // Creates an object to persist serial port settings
public OptionsWindowForm()
{
InitializeComponent();
}
/// <summary>
/// Allows for access to Form1 to save user changes to the serial port configuration
/// </summary>
/// <param name="callingForm"></param> Is the source of which this form was invoked
public OptionsWindowForm(Form callingForm)
{
mainWindow = callingForm as MainWindowForm;
InitializeComponent();
try
{
userSettings = ReadConfig(); // Attempt to read current serial port config
CopyStoredSettings(); // Write settings to application
UpdateTextBoxes(); // Update text boxes in advanced options window
}
catch(Exception ex)
{
Debug.Write(ex);
DefaultSettings(); // Config is non-existent, set to defaults
StoreSettings(); // Copy default settings to peristence class
WriteConfig(userSettings); // Write persistance class to disk
}
}
/// <summary>
/// Class for persisting serial port settings
/// </summary>
[Serializable]
public class UserSettings
{
public Handshake handshake;
public Parity parity;
public StopBits stopBits;
public int dataBits;
public bool rtsEnabled;
public Encoding encoding;
}
/// <summary>
/// Updates the parity selection from predefined valid options
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxParity_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxParity.SelectedIndex)
{
case 0:
sPort.Parity = Parity.None;
Debug.WriteLine("Parity changed to None");
break;
case 1:
sPort.Parity = Parity.Odd;
Debug.WriteLine("Parity changed to Odd");
break;
case 2:
sPort.Parity = Parity.Even;
Debug.WriteLine("Parity changed to Even");
break;
case 3:
sPort.Parity = Parity.Mark;
Debug.WriteLine("Parity changed to Mark");
break;
case 4:
sPort.Parity = Parity.Space;
Debug.WriteLine("Parity changed to Space");
break;
}
}
/// <summary>
/// Updates the stop bits for the serial port from defined valid options
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxStopBits_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxStopBits.SelectedIndex)
{
case 0:
sPort.StopBits = StopBits.One;
Debug.WriteLine("Parity changed to 1");
break;
case 1:
sPort.StopBits = StopBits.OnePointFive;
Debug.WriteLine("Parity changed to 1.5");
break;
case 2:
sPort.StopBits = StopBits.Two;
Debug.WriteLine("Parity changed to 2");
break;
}
}
/// <summary>
/// Updates the data bits from predefined valid options
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxDataBits_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxDataBits.SelectedIndex)
{
case 0:
sPort.DataBits = 8;
Debug.WriteLine("DataBits changed to 8");
break;
case 1:
sPort.DataBits = 7;
Debug.WriteLine("DataBits changed to 7");
break;
case 2:
sPort.DataBits = 6;
Debug.WriteLine("DataBits changed to 6");
break;
case 3:
sPort.DataBits = 5;
Debug.WriteLine("DataBits changed to 5");
break;
}
}
/// <summary>
/// Updates the handshake serial setting from valid options
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxHandshake_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxHandshake.SelectedIndex)
{
case 0:
sPort.Handshake = Handshake.None;
Debug.WriteLine("Handshake changed to None");
comboBoxHandshake.Text = "None";
break;
case 1:
sPort.Handshake = Handshake.RequestToSend;
Debug.WriteLine("Handshake changed to RequestToSend");
comboBoxHandshake.Text = "RequestToSend";
break;
case 2:
sPort.Handshake = Handshake.RequestToSendXOnXOff;
Debug.WriteLine("Handshake changed to RequestToSendXOnXOff");
comboBoxHandshake.Text = "RequestToSendXOnXOff";
break;
case 3:
sPort.Handshake = Handshake.XOnXOff;
Debug.WriteLine("Handshake changed to XOnXOff");
comboBoxHandshake.Text = "XOnXOff";
break;
}
}
/// <summary>
/// For changing the encoding type of the serial port
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxEncoding_SelectedIndexChanged(object sender, EventArgs e)
{
string currentEncoding = comboBoxEncoding.Text;
sPort.Encoding = Encoding.GetEncoding(currentEncoding);
}
/// <summary>
/// Sets RTS enabled to true via radio button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RadioButtonRTStrue_CheckedChanged(object sender, EventArgs e)
{
sPort.RtsEnable = true;
Debug.WriteLine("RTS changed to " + sPort.RtsEnable.ToString());
}
/// <summary>
/// Sets RTS enabled to false via radio button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RadioButtonRTSfalse_CheckedChanged(object sender, EventArgs e)
{
sPort.RtsEnable = false;
Debug.WriteLine("RTS changed to " + sPort.RtsEnable.ToString());
}
/// <summary>
/// If confirmed, copies the settings to the main serial port and closes the window
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonConfirm_Click(object sender, EventArgs e)
{
try
{
mainWindow.SerialPortSet(sPort); // Call set function and pass set version of serial port
StoreSettings(); // Store port settings for persisting
WriteConfig(userSettings); // Store port settings peristence
Debug.WriteLine("Settings Confirmed");
this.Hide(); // Close window
}
catch(Exception ex)
{
Debug.WriteLine(ex);
}
}
/// <summary>
/// If canceled, disregards any changes and closes the window
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCancel_Click(object sender, EventArgs e)
{
Debug.WriteLine("Changes Cancelled"); //Don't do any changes inc. store/writes/reads
this.Hide(); //close window
}
/// <summary>
/// Sets serial port settings to default
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDefault_Click(object sender, EventArgs e)
{
DefaultSettings(); // Restore port settings to default
mainWindow.SerialPortSet(sPort); // Set port settings to default
StoreSettings(); // Store default port settings ready for persistence
WriteConfig(userSettings); // Save port settings with peristence
Debug.WriteLine("Settings set to default");
this.Hide();
}
/// <summary>
/// Writes the persistance class object UserSettings to disk for next instance of menu or startup
/// </summary>
/// <param name="port"></param>
public static void WriteConfig(UserSettings port)
{
string dir = @".\";
string serializationFile = Path.Combine(dir, "sps.bin");
//serialize
using (Stream stream = File.Open(serializationFile, FileMode.Create))
{
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bformatter.Serialize(stream, port);
}
}
/// <summary>
/// Reads stored peristance on disk
/// </summary>
/// <returns>
/// Returns a UserSettings class object from the serialised data
/// </returns>
public UserSettings ReadConfig()
{
string dir = @".\";
string serializationFile = Path.Combine(dir, "sps.bin");
UserSettings savedPort;
using (Stream stream = File.Open(serializationFile, FileMode.Open))
{
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
savedPort = (UserSettings)bformatter.Deserialize(stream);
}
return savedPort;
}
/// <summary>
/// Copies settings from the read persistance class on disk to the serial port object
/// </summary>
public void StoreSettings()
{
userSettings.parity = sPort.Parity;
userSettings.stopBits = sPort.StopBits;
userSettings.dataBits = sPort.DataBits;
userSettings.handshake = sPort.Handshake;
userSettings.rtsEnabled = sPort.RtsEnable;
userSettings.encoding = sPort.Encoding;
}
/// <summary>
/// Sets the serial port object back to "default"
/// </summary>
public void DefaultSettings()
{
sPort.Parity = Parity.None;
sPort.StopBits = StopBits.One;
sPort.DataBits = 8;
sPort.Handshake = Handshake.None;
sPort.RtsEnable = true;
sPort.Encoding = Encoding.GetEncoding("iso-8859-1");
}
/// <summary>
/// Writes settings from the serial port to the peristance class object
/// </summary>
public void CopyStoredSettings()
{
sPort.DataBits = userSettings.dataBits;
sPort.StopBits = userSettings.stopBits;
sPort.RtsEnable = userSettings.rtsEnabled;
sPort.Handshake = userSettings.handshake;
sPort.Parity = userSettings.parity;
sPort.Encoding = userSettings.encoding;
}
/// <summary>
/// Updates text boxes text with user settings to show peristing of data
/// </summary>
public void UpdateTextBoxes()
{
comboBoxDataBits.Text = userSettings.dataBits.ToString();
comboBoxHandshake.Text = userSettings.handshake.ToString();
comboBoxParity.Text = userSettings.parity.ToString();
// Check for stopbits value and set text accordingly
switch (userSettings.stopBits)
{
case StopBits.One:
comboBoxStopBits.Text = "1";
break;
case StopBits.OnePointFive:
comboBoxStopBits.Text = "1.5";
break;
case StopBits.Two:
comboBoxStopBits.Text = "2";
break;
}
//If RTS is enabled mark correct radio buttons as checked/unchecked
if (userSettings.rtsEnabled)
{
radioButtonRTSfalse.Checked = false;
radioButtonRTStrue.Checked = true;
}
else
{
radioButtonRTSfalse.Checked = true;
radioButtonRTStrue.Checked = false;
}
switch(userSettings.encoding.ToString())
{
case "System.Text.Latin1Encoding":
comboBoxEncoding.Text = "iso-8859-1";
break;
case "System.Text.UnicodeEncoding":
comboBoxEncoding.Text = "unicode";
break;
case "System.Text.ASCIIEncoding":
comboBoxEncoding.Text = "ascii";
break;
}
}
}
}