|
@@ -4,10 +4,16 @@ using System.ComponentModel;
|
4
|
4
|
using System.Data;
|
5
|
5
|
using System.Drawing;
|
6
|
6
|
using System.Linq;
|
|
7
|
+using System.Runtime.InteropServices;
|
7
|
8
|
using System.Text;
|
8
|
9
|
using System.Threading.Tasks;
|
9
|
10
|
using System.Windows.Forms;
|
10
|
11
|
|
|
12
|
+//Add code
|
|
13
|
+using System.IO;
|
|
14
|
+using System.IO.Ports;
|
|
15
|
+using Jdas_Mbic.Function;
|
|
16
|
+
|
11
|
17
|
namespace Jdas_Mbic
|
12
|
18
|
{
|
13
|
19
|
public partial class Terminal : Form
|
|
@@ -21,5 +27,447 @@ namespace Jdas_Mbic
|
21
|
27
|
{
|
22
|
28
|
this.Dispose();
|
23
|
29
|
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+ JdasMbic main_form;
|
|
33
|
+ int LineLimit = 500;
|
|
34
|
+ [DllImport("user32.dll")]
|
|
35
|
+ public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
|
|
36
|
+ private const int WM_SETREDRAW = 11;
|
|
37
|
+
|
|
38
|
+ public RadioButton RadioButton_ascii { get => radioButton_ascii; set => radioButton_ascii = value; }
|
|
39
|
+ public RadioButton RadioButton_hex { get => radioButton_hex; set => radioButton_hex = value; }
|
|
40
|
+#if true
|
|
41
|
+ public void Data_Recv_Str(string text)
|
|
42
|
+ {
|
|
43
|
+
|
|
44
|
+ int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
|
|
45
|
+ try
|
|
46
|
+ {
|
|
47
|
+ try
|
|
48
|
+ {
|
|
49
|
+ SendMessage(this.Handle, WM_SETREDRAW, false, 0);
|
|
50
|
+ }
|
|
51
|
+ catch (Exception e) { MessageBox.Show(e.StackTrace); }
|
|
52
|
+ if (tbReceived.Lines.Length > nLimitLines)
|
|
53
|
+ {
|
|
54
|
+ LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
|
|
55
|
+ while ((tempLines.Count - nLimitLines) > 0)
|
|
56
|
+ {
|
|
57
|
+ tempLines.RemoveFirst();
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ tbReceived.Lines = tempLines.ToArray();
|
|
61
|
+
|
|
62
|
+ }
|
|
63
|
+ try
|
|
64
|
+ {
|
|
65
|
+ SendMessage(this.Handle, WM_SETREDRAW, true, 0);
|
|
66
|
+ }
|
|
67
|
+ catch { return; }
|
|
68
|
+ tbReceived.AppendText(text);
|
|
69
|
+ tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택...
|
|
70
|
+ tbReceived.ScrollToCaret();
|
|
71
|
+ }
|
|
72
|
+ catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
|
|
73
|
+ }
|
|
74
|
+#endif
|
|
75
|
+#if false
|
|
76
|
+ public void Data_Recv_Hex(byte[] text)
|
|
77
|
+ {
|
|
78
|
+ int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
|
|
79
|
+ try
|
|
80
|
+ {
|
|
81
|
+ try
|
|
82
|
+ {
|
|
83
|
+ SendMessage(this.Handle, WM_SETREDRAW, false, 0);
|
|
84
|
+ }
|
|
85
|
+ catch (Exception e) { MessageBox.Show(e.StackTrace); }
|
|
86
|
+ if (tbReceived.Lines.Length > nLimitLines)
|
|
87
|
+ {
|
|
88
|
+ LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
|
|
89
|
+ while ((tempLines.Count - nLimitLines) > 0)
|
|
90
|
+ {
|
|
91
|
+ tempLines.RemoveFirst();
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ tbReceived.Lines = tempLines.ToArray();
|
|
95
|
+
|
|
96
|
+ }
|
|
97
|
+ try
|
|
98
|
+ {
|
|
99
|
+ SendMessage(this.Handle, WM_SETREDRAW, true, 0);
|
|
100
|
+ }
|
|
101
|
+ catch { return; }
|
|
102
|
+ tbReceived.AppendText(text.ToString());
|
|
103
|
+ tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택...
|
|
104
|
+ tbReceived.ScrollToCaret();
|
|
105
|
+ }
|
|
106
|
+ catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
|
|
107
|
+
|
|
108
|
+ }
|
|
109
|
+#endif
|
|
110
|
+ public void Data_Recv_Hex(byte[] text)
|
|
111
|
+ {
|
|
112
|
+ string AppendMessage = "";
|
|
113
|
+
|
|
114
|
+ int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
|
|
115
|
+ try
|
|
116
|
+ {/*
|
|
117
|
+ try
|
|
118
|
+ {
|
|
119
|
+ SendMessage(this.Handle, WM_SETREDRAW, false, 0);
|
|
120
|
+ }
|
|
121
|
+ catch (Exception e) { MessageBox.Show(e.StackTrace); }*/
|
|
122
|
+ for (int i = 0; i < text.Length; i++)
|
|
123
|
+ {
|
|
124
|
+ AppendMessage += Convert.ToString(text[i], 16);
|
|
125
|
+ AppendMessage += " ";
|
|
126
|
+ }
|
|
127
|
+ AppendMessage += "\r\n";
|
|
128
|
+ tbReceived.AppendText(AppendMessage);
|
|
129
|
+
|
|
130
|
+ if (tbReceived.Lines.Length > nLimitLines)
|
|
131
|
+ {
|
|
132
|
+ LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
|
|
133
|
+
|
|
134
|
+ while ((tempLines.Count - nLimitLines) > 0)
|
|
135
|
+ {
|
|
136
|
+ tempLines.RemoveFirst();
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ tbReceived.Lines = tempLines.ToArray();
|
|
140
|
+ }
|
|
141
|
+ try
|
|
142
|
+ {
|
|
143
|
+ SendMessage(this.Handle, WM_SETREDRAW, true, 0);
|
|
144
|
+ }
|
|
145
|
+ catch { return; }
|
|
146
|
+ tbReceived.Select(tbReceived.Text.Length, 0);
|
|
147
|
+ tbReceived.ScrollToCaret();
|
|
148
|
+ }
|
|
149
|
+ catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
|
|
150
|
+
|
|
151
|
+ }
|
|
152
|
+ public void Data_Send(byte[] text)
|
|
153
|
+ {
|
|
154
|
+ string AppendMessage = "\n[TX]";
|
|
155
|
+
|
|
156
|
+ int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
|
|
157
|
+ try
|
|
158
|
+ {
|
|
159
|
+ for (int i = 0; i < text.Length; i++)
|
|
160
|
+ {
|
|
161
|
+ AppendMessage += Convert.ToString(text[i], 16);
|
|
162
|
+ }
|
|
163
|
+ tbReceived.AppendText(AppendMessage);
|
|
164
|
+ try
|
|
165
|
+ {
|
|
166
|
+ SendMessage(this.Handle, WM_SETREDRAW, false, 0);
|
|
167
|
+ }
|
|
168
|
+ catch { return; }
|
|
169
|
+ if (tbReceived.Lines.Length > nLimitLines)
|
|
170
|
+ {
|
|
171
|
+ LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
|
|
172
|
+
|
|
173
|
+ while ((tempLines.Count - nLimitLines) > 0)
|
|
174
|
+ {
|
|
175
|
+ tempLines.RemoveFirst();
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ tbReceived.Lines = tempLines.ToArray();
|
|
179
|
+ }
|
|
180
|
+ try
|
|
181
|
+ {
|
|
182
|
+ SendMessage(this.Handle, WM_SETREDRAW, true, 0);
|
|
183
|
+ }
|
|
184
|
+ catch { return; }
|
|
185
|
+ tbReceived.Select(tbReceived.Text.Length, 0);
|
|
186
|
+ tbReceived.ScrollToCaret();
|
|
187
|
+ }
|
|
188
|
+ finally { }
|
|
189
|
+
|
|
190
|
+ }
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+ private void Button_Send_Click(object sender, EventArgs e)
|
|
194
|
+ {
|
|
195
|
+
|
|
196
|
+ }
|
|
197
|
+
|
|
198
|
+ private void Button_Clear_Click(object sender, EventArgs e)
|
|
199
|
+ {
|
|
200
|
+
|
|
201
|
+ }
|
|
202
|
+ public void hex_to_ascii_radiobuttonConvert()
|
|
203
|
+ {
|
|
204
|
+ radioButton_hex.Checked = true;
|
|
205
|
+ radioButton_ascii.Checked = false;
|
|
206
|
+ }
|
|
207
|
+ public void ascii_to_hex_radiobuttonConvert()
|
|
208
|
+ {
|
|
209
|
+ radioButton_ascii.Checked = true;
|
|
210
|
+ radioButton_hex.Checked = false;
|
|
211
|
+ }
|
|
212
|
+ public void Debug_Main_Form_Get(object frm)
|
|
213
|
+ {
|
|
214
|
+ this.main_form = (JdasMbic)frm;
|
|
215
|
+ }
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+ //public string Serial_Name { get => serialPort.PortName; set => serialPort.PortName = value;}
|
|
224
|
+ Data_Handler data_Handler = new Data_Handler();
|
|
225
|
+ //FileDownload fileDownload;
|
|
226
|
+ Update_Serial fileDownload = null;
|
|
227
|
+ public void SetPortNameValues(object obj)
|
|
228
|
+ {
|
|
229
|
+ string[] ports = SerialPort.GetPortNames(); // load all name of com ports to string
|
|
230
|
+ try
|
|
231
|
+ {
|
|
232
|
+ ((ComboBox)obj).Items.Clear(); //delete previous names in combobox items
|
|
233
|
+ }
|
|
234
|
+ catch { }
|
|
235
|
+ ((ComboBox)obj).DataSource = null;
|
|
236
|
+ foreach (string port in ports) //add this names to comboboxPort items
|
|
237
|
+ {
|
|
238
|
+ ((ComboBox)obj).Items.Add(port); //if there are some com ports ,select first
|
|
239
|
+ }
|
|
240
|
+ if (((ComboBox)obj).Items.Count > 0)
|
|
241
|
+ {
|
|
242
|
+ ((ComboBox)obj).SelectedIndex = 0;
|
|
243
|
+ }
|
|
244
|
+ else
|
|
245
|
+ {
|
|
246
|
+ ((ComboBox)obj).Text = " "; //if there are no com ports ,write Empty
|
|
247
|
+ }
|
|
248
|
+ }
|
|
249
|
+ public void Serial_Initialize(ref ComboBox cb_port)
|
|
250
|
+ {
|
|
251
|
+
|
|
252
|
+ this.serialPort = new System.IO.Ports.SerialPort();
|
|
253
|
+ this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.Serial_DataRecvFunction);
|
|
254
|
+
|
|
255
|
+ cb_port.BeginUpdate();
|
|
256
|
+ foreach (string comport in SerialPort.GetPortNames())//foreach (string comport in SerialPort_TestProgram.GetPortNames())
|
|
257
|
+ {
|
|
258
|
+ cb_port.Items.Add(comport);
|
|
259
|
+ }
|
|
260
|
+ cb_port.EndUpdate();
|
|
261
|
+ //SerialPort 초기 설정.
|
|
262
|
+ // serialPort.Encoding = Encoding.GetEncoding("Windows-1252");
|
|
263
|
+ cb_port.DataSource = SerialPort.GetPortNames();
|
|
264
|
+ try
|
|
265
|
+ {
|
|
266
|
+ serialPort_JdasMbic.PortName = cb_port.SelectedItem.ToString();
|
|
267
|
+ serialPort_JdasMbic.BaudRate = (int)921600;
|
|
268
|
+ serialPort_JdasMbic.DataBits = (int)8;
|
|
269
|
+ serialPort_JdasMbic.Parity = System.IO.Ports.Parity.None;
|
|
270
|
+ serialPort_JdasMbic.StopBits = StopBits.One;
|
|
271
|
+ }
|
|
272
|
+ catch { }
|
|
273
|
+
|
|
274
|
+ }
|
|
275
|
+ private delegate void StringSend(string Text);
|
|
276
|
+ private delegate void ByteSend(byte[] Text);
|
|
277
|
+
|
|
278
|
+ public string Str2hex(string strData,Boolean Compotable)
|
|
279
|
+ {
|
|
280
|
+ string resultHex = string.Empty;
|
|
281
|
+ byte[] arr_byteStr = Encoding.Default.GetBytes(strData);
|
|
282
|
+
|
|
283
|
+ foreach (byte byteStr in arr_byteStr)
|
|
284
|
+ {
|
|
285
|
+ if(Compotable == true)
|
|
286
|
+ resultHex += string.Format("{0:X2}", byteStr) + " ";
|
|
287
|
+ else
|
|
288
|
+ resultHex += string.Format("{0:X2}", byteStr);
|
|
289
|
+ }
|
|
290
|
+
|
|
291
|
+ return resultHex;
|
|
292
|
+ }
|
|
293
|
+
|
|
294
|
+ static public byte[] Str2bytes(string byteData)
|
|
295
|
+ {
|
|
296
|
+
|
|
297
|
+#if false
|
|
298
|
+ System.Text.ASCIIEncoding asencoding = new System.Text.ASCIIEncoding();
|
|
299
|
+ return Encoding.Default.GetBytes(byteData);
|
|
300
|
+#else
|
|
301
|
+ byte[] arr_byteStr = Encoding.Default.GetBytes(byteData);
|
|
302
|
+ return arr_byteStr;
|
|
303
|
+#endif
|
|
304
|
+ }
|
|
305
|
+ private delegate void BoolSet();
|
|
306
|
+ public void Serial_DataRecvFunction(object sender, SerialDataReceivedEventArgs e)
|
|
307
|
+ {
|
|
308
|
+ string tmpSTR = "";
|
|
309
|
+ Data_Handler data_Handler = new Data_Handler();
|
|
310
|
+ int nLnegth = serialPort.BytesToRead;
|
|
311
|
+ byte[] btdata = new byte[nLnegth];
|
|
312
|
+
|
|
313
|
+ serialPort.Read(btdata, 0, nLnegth);
|
|
314
|
+
|
|
315
|
+ main_form.Invoke(new BoolSet(main_form.RX_Light_ON));
|
|
316
|
+ main_form.TX_RX_Light = true;
|
|
317
|
+ if (this.Debug.Created && Debug.RadioButton_ascii.Checked == true)
|
|
318
|
+ {
|
|
319
|
+ string data = Encoding.Default.GetString(btdata);
|
|
320
|
+ // string data = serialPort.ReadExisting();
|
|
321
|
+
|
|
322
|
+ if (Debug.RadioButton_ascii.Checked == true)
|
|
323
|
+ Debug.Invoke(new StringSend(Debug.Data_Recv_Str), data);
|
|
324
|
+ else
|
|
325
|
+ Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(data, true));
|
|
326
|
+
|
|
327
|
+ data_Handler.Recv_dataCheck(this,this.main_form,this.fileDownload, btdata);
|
|
328
|
+ }
|
|
329
|
+ else
|
|
330
|
+ {
|
|
331
|
+ // 리스트 두개 사용
|
|
332
|
+ if (nLnegth > 0)
|
|
333
|
+ {
|
|
334
|
+ if (this.Debug.Created)
|
|
335
|
+ {
|
|
336
|
+ if (this.Debug.radioButton_ascii.Checked == true)
|
|
337
|
+ {
|
|
338
|
+ tmpSTR = Encoding.Default.GetString(btdata).TrimEnd('\0');
|
|
339
|
+ Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(tmpSTR, true));
|
|
340
|
+ }
|
|
341
|
+ else
|
|
342
|
+ {
|
|
343
|
+ Debug.Invoke(new ByteSend(Debug.Data_Recv_Hex), btdata);
|
|
344
|
+ }
|
|
345
|
+ }
|
|
346
|
+ else
|
|
347
|
+ {
|
|
348
|
+ if (this.Debug.Created)
|
|
349
|
+ Debug.Invoke(new ByteSend(Debug.Data_Recv_Hex), btdata);
|
|
350
|
+ }
|
|
351
|
+ // 이부분에서 데이타 처리하는 부분으로 전송하여 사용하면 됨
|
|
352
|
+ }
|
|
353
|
+
|
|
354
|
+ data_Handler.Recv_dataCheck(this, this.main_form,this.fileDownload, btdata);
|
|
355
|
+ }
|
|
356
|
+ /****
|
|
357
|
+ *메모리 누수 방지용 코드
|
|
358
|
+ */
|
|
359
|
+ System.GC.Collect(0, GCCollectionMode.Forced);
|
|
360
|
+ System.GC.WaitForFullGCComplete();
|
|
361
|
+ }
|
|
362
|
+ public Boolean Serial_PortOpen(ref Button Btn_Portonoff,ref ComboBox cb)
|
|
363
|
+ {
|
|
364
|
+ Boolean ret = false;
|
|
365
|
+ try
|
|
366
|
+ {
|
|
367
|
+ if (serialPort.IsOpen) { //When the port is open
|
|
368
|
+ serialPort.Close();
|
|
369
|
+ Btn_Portonoff.Text = "Port Open";
|
|
370
|
+ ret = true;
|
|
371
|
+ }
|
|
372
|
+ else//When the port is close
|
|
373
|
+ {
|
|
374
|
+ if (cb.Text != "")
|
|
375
|
+ {
|
|
376
|
+ serialPort.Open();
|
|
377
|
+ Btn_Portonoff.Text = "Port Close";
|
|
378
|
+ Debug.Debug_Main_Form_Get(this.main_form);
|
|
379
|
+ }
|
|
380
|
+ else
|
|
381
|
+ {
|
|
382
|
+ MessageBox.Show("Port is not set");
|
|
383
|
+ ret = true;
|
|
384
|
+ }
|
|
385
|
+ }
|
|
386
|
+ }
|
|
387
|
+ catch
|
|
388
|
+ {
|
|
389
|
+ MessageBox.Show("already port open " + Serial_Name);
|
|
390
|
+
|
|
391
|
+ }
|
|
392
|
+ return ret;
|
|
393
|
+ }
|
|
394
|
+ public void Serial_TerminalOpen(object serial)
|
|
395
|
+ {
|
|
396
|
+ this.Debug.Serial_ClassSet(serial);
|
|
397
|
+ try
|
|
398
|
+ {
|
|
399
|
+ this.Debug.Show();
|
|
400
|
+ }
|
|
401
|
+ catch
|
|
402
|
+ {
|
|
403
|
+ Debug = new Debug();
|
|
404
|
+ this.Debug.Show();
|
|
405
|
+ }
|
|
406
|
+ }
|
|
407
|
+ public void Serial_DataSend(byte[] data)
|
|
408
|
+ {
|
|
409
|
+ try
|
|
410
|
+ {
|
|
411
|
+ serialPort.Write(data,0,data.Length);
|
|
412
|
+ main_form.pictureBox_R_TX.Visible = false;
|
|
413
|
+ main_form.pictureBox_G_TX.Visible = true;
|
|
414
|
+ }
|
|
415
|
+ catch (System.Exception ex)
|
|
416
|
+ {
|
|
417
|
+ MessageBox.Show(ex.Message);
|
|
418
|
+ }
|
|
419
|
+ }
|
|
420
|
+ public void Serial_DataSend(byte[] buffer, int count)
|
|
421
|
+ {
|
|
422
|
+ try {
|
|
423
|
+ serialPort.Write(buffer, 0, count);
|
|
424
|
+ main_form.Invoke(new BoolSet(main_form.TX_Light_ON));
|
|
425
|
+ main_form.TX_RX_Light = true;
|
|
426
|
+ }
|
|
427
|
+ catch
|
|
428
|
+ {
|
|
429
|
+ System.Diagnostics.Process.GetCurrentProcess().Kill();
|
|
430
|
+ MessageBox.Show("Port Open Failed!!!");
|
|
431
|
+ }
|
|
432
|
+ }
|
|
433
|
+
|
|
434
|
+ public void FileDownloadClass_Set(object filedownload)
|
|
435
|
+ {
|
|
436
|
+ this.fileDownload = (Update_Serial)filedownload;
|
|
437
|
+ }
|
|
438
|
+ public object FileDownloadClass_Get()
|
|
439
|
+ {
|
|
440
|
+ return this.fileDownload;
|
|
441
|
+ }
|
|
442
|
+ public void Test_Serial()
|
|
443
|
+ {
|
|
444
|
+ byte[] tempdata = new byte[1024];
|
|
445
|
+ for (int i = 0; i < 255; i++)
|
|
446
|
+ {
|
|
447
|
+
|
|
448
|
+ tempdata[i] = Convert.ToByte(i);
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+ }
|
|
452
|
+ this.serialPort.Write(tempdata, 0, 255);
|
|
453
|
+ //this.serialPort.Write(tempdata.ToString());
|
|
454
|
+
|
|
455
|
+ }
|
|
456
|
+ public void debug_hextoasciiConvert()
|
|
457
|
+ {
|
|
458
|
+ if(this.Debug.Created)
|
|
459
|
+ this.Debug.hex_to_ascii_radiobuttonConvert();
|
|
460
|
+ }
|
|
461
|
+ public void debug_asciitohexConvert()
|
|
462
|
+ {
|
|
463
|
+ if (this.Debug.Created)
|
|
464
|
+ this.Debug.ascii_to_hex_radiobuttonConvert();
|
|
465
|
+ }
|
|
466
|
+
|
|
467
|
+ public void Serial_Main_Form_Get(object frm)
|
|
468
|
+ {
|
|
469
|
+ this.main_form = (Main_Form)frm;
|
|
470
|
+ }
|
|
471
|
+
|
24
|
472
|
}
|
25
|
473
|
}
|