/
MainForm.cs
562 lines (478 loc) · 17.3 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
using BCom;
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static BlueSerial.Utils.Utils;
namespace BlueSerial
{
public partial class MainForm : Form
{
private ArrayList comList = new ArrayList();//串口列表
private ArrayList scanComList = new ArrayList();//串口列表
private static SerialPort mSerialPort;
private Thread receiveThread=null,scanComThread=null,periodSendThread;
private delegate void SafeCallDelegate(string text);
private int sendByteCount = 0;
public MainForm()
{
InitializeComponent();
initView();
serialPortListInit();
scanComThread = new Thread(new ThreadStart(scanComTask));
scanComThread.Start();
}
private void initView()
{
cb_baud_list.DataSource = baudRateList;
cb_data_list.DataSource = dataBitList;
cb_parity_list.DataSource = parityList;
cb_stop_list.DataSource = stopBitList;
cb_baud_list.SelectedIndex = 3;
this.AllowDrop = true;
this.DragEnter += new DragEventHandler(tvRecvDragEnterEventHandler);
}
private void tvRecvDragEnterEventHandler(object obj, DragEventArgs args)
{
try
{
string dataString = (string)args.Data.GetData(DataFormats.StringFormat);
tb_send.Text = dataString;
}
catch
{
}
}
private void scanComTask()
{
while (true)
{
scanComList.Clear();
foreach (string portName in SerialPort.GetPortNames())
{
scanComList.Add(portName);
}
if (comList.Count != scanComList.Count)
{
cb_com_list.DataSource = scanComList;
}
Thread.Sleep(2000);
}
}
private void serialPortListInit()
{
if (comList.Count > 0)
{
comList.Clear();
}
foreach (string portName in SerialPort.GetPortNames())
{
comList.Add(portName);
}
if (comList.Count == 0)
{
MessageBox.Show("没有搜索到串口");
cb_com_list.DataSource = comList;
return;
}
cb_com_list.DataSource = comList;
if (cb_com_list.SelectedIndex > comList.Count || cb_com_list.SelectedIndex < 0)
{
//MessageBox.Show("串口丢失");
return;
}
mSerialPort = new SerialPort(comList[cb_com_list.SelectedIndex].ToString());
if (!mSerialPort.IsOpen)
{
try
{
mSerialPort.Open();
btn_open_com.Text = "关闭串口";
btn_open_com.ForeColor = Color.Red;
}
catch (Exception)
{
MessageBox.Show("串口"+mSerialPort.PortName+"被占用,无法打开,请关闭占用串口的程序");
}
}
mSerialPort.BaudRate = baudRateList[cb_baud_list.SelectedIndex];
mSerialPort.DataBits = dataBitList[cb_data_list.SelectedIndex];
mSerialPort.Parity = parityList[cb_parity_list.SelectedIndex];
mSerialPort.StopBits = stopBitList[cb_stop_list.SelectedIndex];
mSerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
receiveThread = new Thread(new ParameterizedThreadStart(displayReceiveData));
receiveThread.Start(indata);
}
private void displayReceiveData(object obj)
{
if (tb_recv.InvokeRequired)
{
var d = new SafeCallDelegate(displayReceiveData);
Invoke(d, new object[] { obj });
}
else {
if (cbox_hex_display.Checked)
{
tb_recv.AppendText(" " + convertToHexString((string)obj));
label_recv_count.Text = "R:" + (tb_recv.Text.Length + 1) / 3;
}
else {
tb_recv.AppendText((string)obj);
label_recv_count.Text = "R:" + tb_recv.Text.Length;
}
}
}
private void Btn_open_com_Click(object sender, EventArgs e)
{
if (mSerialPort != null)
{
if (mSerialPort.IsOpen)
{
try
{
mSerialPort.Close();
btn_open_com.Text = "打开串口";
btn_open_com.ForeColor = Color.Black;
}
catch (Exception)
{
MessageBox.Show("串口关闭失败");
}
}
else
{
try
{
mSerialPort.Open();
btn_open_com.Text = "关闭串口";
btn_open_com.ForeColor = Color.Red;
}
catch (Exception)
{
MessageBox.Show("串口" + mSerialPort.PortName + "被占用,无法打开,请关闭占用串口的程序");
btn_open_com.Text = "打开串口";
btn_open_com.ForeColor = Color.Black;
}
}
}
else
{
serialPortListInit();
}
}
private void Btn_clean_send_Click(object sender, EventArgs e)
{
tb_send.Text = "";
label_send_count.Text = "S:0";
}
private void Btn_send_Click(object sender, EventArgs e)
{
sendData();
}
private void sendData()
{
if (mSerialPort != null && mSerialPort.IsOpen)
{
if ((cbox_send_line.Checked == true) && (cbox_send_hex.Checked == true))
{
//发送16进制新行
byte[] bytes = convertHexStringToBytes(tb_send.Text);
if (bytes != null)
{
mSerialPort.Write(bytes, 0, bytes.Length);
byte[] newLine = { 0xd, 0xa };
mSerialPort.Write(newLine, 0, newLine.Length);
sendByteCount += (bytes.Length + newLine.Length);
}
}
else if ((cbox_send_line.Checked == true) && (cbox_send_hex.Checked == false))
{
//发送普通字符串新行
mSerialPort.Write(tb_send.Text + "\r\n");
sendByteCount += (tb_send.Text.Length + 2);
}
else if ((cbox_send_line.Checked == false) && (cbox_send_hex.Checked == false))
{
//发送普通字符串
mSerialPort.Write(tb_send.Text);
sendByteCount += tb_send.Text.Length;
}
else if ((cbox_send_line.Checked == false) && (cbox_send_hex.Checked == true))
{
//发送16进制
byte[] bytes = convertHexStringToBytes(tb_send.Text);
if (bytes != null)
{
mSerialPort.Write(bytes, 0, bytes.Length);
sendByteCount += bytes.Length;
}
}
else
{
MessageBox.Show("不支持的操作!");
}
label_send_count.Text = "S:" + sendByteCount;
}
else
{
MessageBox.Show("尚未打开串口,请打开后再发送");
}
}
private void Cb_com_list_SelectedIndexChanged(object sender, EventArgs e)
{
if (mSerialPort != null)
{
if (mSerialPort.IsOpen)
{
mSerialPort.Close();
}
btn_open_com.Text = "打开串口";
btn_open_com.ForeColor = Color.Black;
mSerialPort = null;
serialPortListInit();
}
}
private void Cb_baud_list_SelectedIndexChanged(object sender, EventArgs e)
{
if (mSerialPort != null)
{
mSerialPort.BaudRate = baudRateList[cb_baud_list.SelectedIndex];
}
}
private void Cb_stop_list_SelectedIndexChanged(object sender, EventArgs e)
{
if (mSerialPort != null)
{
try
{
mSerialPort.StopBits = stopBitList[cb_stop_list.SelectedIndex];
//mSerialPort.StopBits = stopBitList[cb_stop_list.SelectedIndex];
}
catch (Exception)
{
}
}
}
private void Cb_data_list_SelectedIndexChanged(object sender, EventArgs e)
{
if (mSerialPort != null)
{
mSerialPort.DataBits = dataBitList[cb_data_list.SelectedIndex];
}
}
private void Cb_parity_list_SelectedIndexChanged(object sender, EventArgs e)
{
if (mSerialPort != null)
{
mSerialPort.Parity = parityList[cb_parity_list.SelectedIndex];
}
}
private void Cbox_hex_display_CheckedChanged(object sender, EventArgs e)
{
if (cbox_hex_display.Checked)
{
tb_recv.Text = convertToHexString(tb_recv.Text);
}
else {
tb_recv.Text = convertHexStringToCommonString(tb_recv.Text);
}
}
private void Cbox_timer_send_CheckedChanged(object sender, EventArgs e)
{
if (cbox_timer_send.Checked)
{
try
{
Convert.ToInt32(tb_period_send_time_ms.Text);
periodSendThread = new Thread(new ThreadStart(periodSendTask));
periodSendThread.Start();
}
catch (Exception) {
MessageBox.Show("周期格式不正确");
cbox_timer_send.Checked = false;
return;
}
}
else
{
if (periodSendThread != null)
{
try
{
periodSendThread.Abort();
} catch (Exception)
{
MessageBox.Show("周期发送结束失败,请重试!");
}
}
}
}
private void Ts_menu_about_Click(object sender, EventArgs e)
{
MessageBox.Show("BCom是一款免费开源的串口调试助手","关于 BCom2.0");
}
private void Ts_menu_weixin_open_Click(object sender, EventArgs e)
{
WxForm wxForm = new WxForm();
wxForm.Show();
}
private void Ts_menu_git_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://github.com/bestyize/BCom");
}
private void Ts_menu_calc_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("calc");
}
private void Ts_menu_nodepad_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("notepad");
}
private void Ts_menu_cmd_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("cmd");
}
private void Ts_menu_regedit_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("regedit");
}
private void Ts_menu_screenshot_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start("snippingtool");
}
catch (Exception)
{
MessageBox.Show("您的系统不支持");
}
}
private void Ts_menu_mspaint_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("mspaint");
}
private void Ts_menu_screenshot_tool_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("snippingtool");
}
private void Ts_menu_mind_paint_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.freedraw.xyz/");
}
private void Ts_menu_mode_Click(object sender, EventArgs e)
{
if (this.Width == 400)
{
this.Width = 730;
this.Height = 530;
ts_menu_mode.Text = "精简模式";
}
else
{
this.Width = 400;
this.Height = 60;
ts_menu_mode.Text = "高级模式";
}
}
private void Ts_menu_notepad_tool_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("notepad");
}
private void Ts_menu_save_as_Click(object sender, EventArgs e)
{
Stream mStream;
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = "d:\\";
saveFileDialog.Filter = "ext files (*.txt)|*.txt|All files(*.*)|*>**";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog()==DialogResult.OK&& saveFileDialog.FileName.Length > 0)
{
if ((mStream = saveFileDialog.OpenFile())!= null)
{
StreamWriter writer = new StreamWriter(mStream);
writer.Write(tb_recv.Text);
writer.Flush();
writer.Close();
mStream.Close();
MessageBox.Show("存储文件成功!", "温馨提示");
}
}
}
private void Ts_menu_open_file_Click(object sender, EventArgs e)
{
var fileContent = string.Empty;
var filePath = string.Empty;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filePath = openFileDialog.FileName;
var fileStream = openFileDialog.OpenFile();
StreamReader reader = new StreamReader(fileStream);
fileContent = reader.ReadToEnd();
reader.Close();
fileStream.Close();
tb_send.Text = fileContent;
}
}
private void Cbox_rts_CheckedChanged(object sender, EventArgs e)
{
if (mSerialPort != null)
{
if (cbox_rts.Checked)
{
mSerialPort.RtsEnable = true;
}
else
{
mSerialPort.RtsEnable = false;
}
}
}
private void Cbox_dtr_CheckedChanged(object sender, EventArgs e)
{
if (mSerialPort != null)
{
if (cbox_dtr.Checked)
{
mSerialPort.DtrEnable = true;
}
else
{
mSerialPort.DtrEnable = false;
}
}
}
private void MainForm_Load(object sender, EventArgs e)
{
this.AllowDrop = true;
}
private void periodSendTask()
{
Control.CheckForIllegalCrossThreadCalls = false;
while (true)
{
sendData();
Thread.Sleep(Convert.ToInt32(tb_period_send_time_ms.Text));
}
}
private void Btn_clean_recv_Click(object sender, EventArgs e)
{
tb_recv.Text = "";
label_recv_count.Text = "R:0";
}
}
}