-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
257 lines (220 loc) · 7.5 KB
/
MainForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace SerialMonitor
{
public partial class MainForm : Form
{
#region Constant
private readonly int[] baudrate = { 9600, 19200, 38400, 115200, 230400, 460800, 921600, 3860000 };
private readonly string[] lineTypes = { "No Line Ending", "NewLine", "Carriage return", "Both NL & CR"};
private readonly string defaultBaudrate = "115200 baud";
private readonly string defaultLinetype = "NewLine";
#endregion
private SerialPort Serial = new SerialPort();
#region Local Helpers
private void UpdateCOMPortList()
{
// Get all existing Com Port names
string[] Ports = System.IO.Ports.SerialPort.GetPortNames();
Ports = Ports.Distinct().ToArray();
Array.Sort(Ports);
string selectDefaultPort = "";
cboxComport.Items.Clear();
cboxBaudrate.Items.Clear();
cboxLinetype.Items.Clear();
// Append existing COM to the cboxComport list
foreach (var item in Ports)
{
cboxComport.Items.Add(item);
selectDefaultPort = item;
}
//select lastest Port found by default
if(Ports.Length > 1)
{
cboxComport.Text = selectDefaultPort;
}
// Append possible Baudrate to the cboxBaudrate list
foreach (var baud in baudrate)
{
cboxBaudrate.Items.Add(baud.ToString()+" baud");
}
//set default value
cboxBaudrate.Text = defaultBaudrate;
// Append possible Baudrate to the cboxBaudrate list
foreach (var line in lineTypes)
{
cboxLinetype.Items.Add(line.ToString());
}
//set default value
cboxLinetype.Text = defaultLinetype;
}
#endregion
#region Delegates
public delegate void UPDATE_OUTPUT_TEXT(String Str);
public void UpdateOutputText(String Str)
{
richTextBoxReceive.AppendText(Str);
if (cboxAutoscroll.Checked)
{
//tboxReceive.SelectionStart = tboxReceive.Text.Length;
if (Str.Contains("\n"))
richTextBoxReceive.ScrollToCaret();
}
}
#endregion
#region Handlers
void SerialOnReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
String str = Serial.ReadExisting();
Invoke(new UPDATE_OUTPUT_TEXT(UpdateOutputText), str);
}
#endregion
public MainForm()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
// If user click disconnect
if ("Disconnect" == btnConnect.Text.ToString())
{
if (true == Serial.IsOpen)
{
Serial.Close();
}
btnConnect.Text = "Connect";
cboxComport.Enabled = true;
cboxBaudrate.Enabled = true;
btnRefresh.Enabled = true;
return;
}
// else we gonna open the desired COM port
// Get user comport from cbox
try
{
Serial.PortName = cboxComport.Text;
}
catch
{
MessageBox.Show("Error! No COM Port selected");
return;
}
// Get user baudrate from cbox
try
{
Serial.BaudRate = int.Parse(cboxBaudrate.Text.ToString().Replace(" baud",""));
}
catch
{
MessageBox.Show("Error! No Baudrate selected");
return;
}
// Serial Port Configuration
Serial.Parity = Parity.None;
Serial.DataBits = 8;
Serial.ReceivedBytesThreshold = 1;
Serial.StopBits = StopBits.One;
Serial.Handshake = Handshake.None;
Serial.WriteTimeout = 3000;
Serial.Encoding = Encoding.GetEncoding("utf-8");
//Serial.RtsEnable = true;
Serial.DtrEnable = true;
// Check if com port is opened by other application
if (false == Serial.IsOpen)
{
try
{
// Com port available
Serial.Open();
}
catch
{
MessageBox.Show("The COM port is not accessible", "Error");
return;
};
// double comform it is opened
if (true == Serial.IsOpen)
{
btnConnect.Text = "Disconnect";
cboxComport.Enabled = false;
cboxBaudrate.Enabled = false;
btnRefresh.Enabled = false;
// Add callback handler for receiving
Serial.DataReceived += new SerialDataReceivedEventHandler(SerialOnReceivedHandler);
}
}
}
private void MainForm_Load(object sender, EventArgs e)
{
// We need to populate the lists during mainform is loading
UpdateCOMPortList();
}
private void btnRefresh_Click(object sender, EventArgs e)
{
// We need to update all lists again if user requested
UpdateCOMPortList();
}
private void btnSend_Click(object sender, EventArgs e)
{
SendSerialCommand();
}
private void SendSerialCommand()
{
if (null != Serial)
{
if (true == Serial.IsOpen)
{
Serial.Write(tboxData.Text + NewLineCode());
tboxData.Text = "";
}
else
{
MessageBox.Show("COM Port is not Opened");
}
}
}
private string NewLineCode()
{
string newlinechar = "";
switch(cboxLinetype.Text)
{
case "No Line Ending":
newlinechar = "";
break;
case "NewLine":
newlinechar = "\n";
break;
case "Carriage return":
newlinechar = "\r";
break;
case "Both NL & CR":
newlinechar = "\n\r";
break;
}
return newlinechar;
}
private void btnClearOutput_Click(object sender, EventArgs e)
{
richTextBoxReceive.Clear();
}
private void tboxData_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(Keys.Return))
{
SendSerialCommand();
e.Handled = true; //remove enter key pressed windows default sound
}
}
}
}