|
@@ -0,0 +1,419 @@
|
|
1
|
+using System;
|
|
2
|
+using System.Collections.Generic;
|
|
3
|
+using System.ComponentModel;
|
|
4
|
+using System.Data;
|
|
5
|
+using System.Drawing;
|
|
6
|
+using System.Linq;
|
|
7
|
+using System.Runtime.InteropServices;
|
|
8
|
+using System.Text;
|
|
9
|
+using System.Threading.Tasks;
|
|
10
|
+using System.Windows.Forms;
|
|
11
|
+using Excel = Microsoft.Office.Interop.Excel;
|
|
12
|
+using System.Data.OleDb;
|
|
13
|
+
|
|
14
|
+//Add
|
|
15
|
+
|
|
16
|
+using System.IO;
|
|
17
|
+using System.IO.Ports;
|
|
18
|
+using System.Text.RegularExpressions;
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+namespace APL_Macro
|
|
24
|
+{
|
|
25
|
+ public partial class Form1 : Form
|
|
26
|
+ {
|
|
27
|
+ private System.IO.Ports.SerialPort serialPort1;
|
|
28
|
+
|
|
29
|
+ // 핸들을 잡기위한 Dll Import
|
|
30
|
+ [DllImport("user32.dll")]
|
|
31
|
+ public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
|
|
32
|
+ [DllImport("user32.dll")]
|
|
33
|
+ public static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);
|
|
34
|
+ [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
|
|
35
|
+ public static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
|
|
36
|
+ [DllImport("user32.dll")]
|
|
37
|
+ public static extern int SendMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
|
|
38
|
+ [DllImport("user32")]
|
|
39
|
+ public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
|
|
40
|
+
|
|
41
|
+ // PostMessage 를 위한 Message Value
|
|
42
|
+ public enum WMessages : int
|
|
43
|
+ {
|
|
44
|
+ WM_LBUTTONDOWN = 0x201, //Left mousebutton down
|
|
45
|
+ WM_LBUTTONUP = 0x202, //Left mousebutton up
|
|
46
|
+ WM_LBUTTONDBLCLK = 0x203, //Left mousebutton doubleclick
|
|
47
|
+ WM_RBUTTONDOWN = 0x204, //Right mousebutton down
|
|
48
|
+ WM_RBUTTONUP = 0x205, //Right mousebutton up
|
|
49
|
+ WM_RBUTTONDBLCLK = 0x206, //Right mousebutton doubleclick
|
|
50
|
+ WM_KEYDOWN = 0x100, //Key down
|
|
51
|
+ WM_KEYUP = 0x101, //Key up
|
|
52
|
+ WM_SYSKEYDOWN = 0x104,
|
|
53
|
+ WM_SYSKEYUP = 0x105,
|
|
54
|
+ WM_CHAR = 0x102,
|
|
55
|
+ WM_COMMAND = 0x111
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+ public Form1()
|
|
60
|
+ {
|
|
61
|
+ InitializeComponent();
|
|
62
|
+ }
|
|
63
|
+ private void Form1_Load(object sender, EventArgs e)
|
|
64
|
+ {
|
|
65
|
+ /* PrivateFontCollection privateFonts = new PrivateFontCollection();
|
|
66
|
+ privateFonts.AddFontFile("뫼비우스 Bold.ttf");
|
|
67
|
+ Font font = new Font(privateFonts.Families[0], 70f);*/
|
|
68
|
+
|
|
69
|
+ /*label38.Font = font;
|
|
70
|
+ label38.Text = "누액 감지 시스템";*/
|
|
71
|
+
|
|
72
|
+ this.serialPort1 = new System.IO.Ports.SerialPort();
|
|
73
|
+ this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.SP1_DataReceived);
|
|
74
|
+ comboBox1.BeginUpdate();
|
|
75
|
+ foreach (string comport in SerialPort.GetPortNames())//foreach (string comport in SerialPort_TestProgram.GetPortNames())
|
|
76
|
+ {
|
|
77
|
+ comboBox1.Items.Add(comport);
|
|
78
|
+ }
|
|
79
|
+ comboBox1.EndUpdate();
|
|
80
|
+ //SerialPort 초기 설정
|
|
81
|
+
|
|
82
|
+ comboBox1.DataSource = SerialPort.GetPortNames();
|
|
83
|
+ serialPort1.BaudRate = (int)115200;
|
|
84
|
+ serialPort1.DataBits = (int)8;
|
|
85
|
+ serialPort1.Parity = System.IO.Ports.Parity.None;
|
|
86
|
+ serialPort1.StopBits = StopBits.One;
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+ //Serial Form _Off button 설정
|
|
90
|
+ Button_Stop.Enabled = false; //off 기능 사라짐
|
|
91
|
+ Button_Stop.Visible = false; //off 버튼 숨겨짐
|
|
92
|
+ SerialFormEnagleChoice(true); //serial 설정버튼 on
|
|
93
|
+ SetupDataGridView();
|
|
94
|
+ }
|
|
95
|
+ private void SetupDataGridView()
|
|
96
|
+ {
|
|
97
|
+ this.Controls.Add(dgvList);
|
|
98
|
+
|
|
99
|
+ // DataGridView의 컬럼 갯수를 5개로 설정합니다.
|
|
100
|
+ dgvList.ColumnCount = 2;
|
|
101
|
+
|
|
102
|
+ // DataGridView에 컬럼을 추가합니다.
|
|
103
|
+ dgvList.Columns[0].Name = "IMEI";
|
|
104
|
+ dgvList.Columns[1].Name = "USIM";
|
|
105
|
+
|
|
106
|
+ }
|
|
107
|
+ private void SerialFormEnagleChoice(bool of)
|
|
108
|
+ {
|
|
109
|
+ comboBox1.Enabled = of;
|
|
110
|
+ comboBox2.Enabled = of;
|
|
111
|
+ }
|
|
112
|
+ //글자 수 제한///
|
|
113
|
+ string g_data; // 전역 변수(혹은 멤버 변수)
|
|
114
|
+ private void SP1_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
|
115
|
+ {
|
|
116
|
+ //string receiveData = "";
|
|
117
|
+ //포트로 데이터가 들어오면
|
|
118
|
+ if (serialPort1.IsOpen)
|
|
119
|
+ {
|
|
120
|
+ if (Ascii_checkBox.Checked == true)
|
|
121
|
+ {
|
|
122
|
+ string data = serialPort1.ReadExisting();
|
|
123
|
+ g_data += data;
|
|
124
|
+ if (data != string.Empty)
|
|
125
|
+ {
|
|
126
|
+ Controller_TextLoad(data);
|
|
127
|
+ PacketData(data);
|
|
128
|
+ }
|
|
129
|
+ }
|
|
130
|
+ else
|
|
131
|
+ {
|
|
132
|
+ try
|
|
133
|
+ {
|
|
134
|
+ // 리스트 두개 사용
|
|
135
|
+ int nLnegth = 0;
|
|
136
|
+ nLnegth = serialPort1.BytesToRead;
|
|
137
|
+ byte[] btTemp = new byte[nLnegth];
|
|
138
|
+ serialPort1.Read(btTemp, 0, nLnegth);
|
|
139
|
+
|
|
140
|
+ if (nLnegth > 0)
|
|
141
|
+ {
|
|
142
|
+ PacketData(btTemp);
|
|
143
|
+ // 이부분에서 데이타 처리하는 부분으로 전송하여 사용하면 됨
|
|
144
|
+ }
|
|
145
|
+ }
|
|
146
|
+ catch (Exception ex)
|
|
147
|
+ {
|
|
148
|
+ if (serialPort1.IsOpen)
|
|
149
|
+ {
|
|
150
|
+ System.Windows.Forms.MessageBox.Show(ex.ToString());
|
|
151
|
+ }
|
|
152
|
+ else
|
|
153
|
+ {
|
|
154
|
+ return;
|
|
155
|
+ }
|
|
156
|
+ }
|
|
157
|
+ }
|
|
158
|
+ }
|
|
159
|
+ }
|
|
160
|
+ /*
|
|
161
|
+ * 데이터 수신 부분 Class to Class 전달 부분*
|
|
162
|
+ * */
|
|
163
|
+ private delegate void StringSend(string Text);
|
|
164
|
+ private delegate void ValueSend(byte[] data);
|
|
165
|
+ private delegate void BoolSend(ref CheckBox temp_Checkbox, bool data);
|
|
166
|
+
|
|
167
|
+ private delegate void voidSend();
|
|
168
|
+ private int datacnt = 0;
|
|
169
|
+ string[] SubStr = new string[2];
|
|
170
|
+ private void PacketData(string Text)
|
|
171
|
+ {
|
|
172
|
+
|
|
173
|
+ bool stringExists = Text.Contains("READY");
|
|
174
|
+ if (stringExists)
|
|
175
|
+ {
|
|
176
|
+ MessageBox.Show(Text);
|
|
177
|
+ datacnt = 1;
|
|
178
|
+ }
|
|
179
|
+ if(datacnt >= 1)
|
|
180
|
+ {
|
|
181
|
+
|
|
182
|
+ string strTmp = Regex.Replace(Text, @"\D", "");
|
|
183
|
+ try
|
|
184
|
+ {
|
|
185
|
+ if (strTmp != "")
|
|
186
|
+ {
|
|
187
|
+ if (strTmp.Length < 25 && datacnt == 1)
|
|
188
|
+ {
|
|
189
|
+ datacnt = 2;
|
|
190
|
+ }
|
|
191
|
+ if (datacnt == 1)
|
|
192
|
+ {
|
|
193
|
+ SubStr[0] = "\r\n IMEI : " + strTmp.Substring(0, 15);
|
|
194
|
+ SubStr[1] = "\r\n USIM : " + strTmp.Substring(15, 15);
|
|
195
|
+ MessageBox.Show(SubStr[0] + SubStr[1]);
|
|
196
|
+ datacnt = 0;
|
|
197
|
+ }
|
|
198
|
+ else if (datacnt == 2)
|
|
199
|
+ {
|
|
200
|
+ SubStr[0] = "\r\n IMEI : " + strTmp.Substring(0, 15);
|
|
201
|
+ datacnt = 3;
|
|
202
|
+ }
|
|
203
|
+ else if (datacnt == 3)
|
|
204
|
+ {
|
|
205
|
+ SubStr[1] = "\r\n USIM : " + strTmp.Substring(0, 15);
|
|
206
|
+ MessageBox.Show(SubStr[0] + SubStr[1]);
|
|
207
|
+ datacnt = 0;
|
|
208
|
+ string[] row0 = { Regex.Replace(SubStr[0], @"\D", ""), Regex.Replace(SubStr[1], @"\D", "") };
|
|
209
|
+
|
|
210
|
+ // DataGridView에 한 줄씩 삽입합니다.
|
|
211
|
+ CheckForIllegalCrossThreadCalls = false;
|
|
212
|
+ dgvList.Rows.Add(row0);
|
|
213
|
+ }
|
|
214
|
+ }
|
|
215
|
+ }
|
|
216
|
+ catch (Exception ex)
|
|
217
|
+ {
|
|
218
|
+ MessageBox.Show(ex.Message);
|
|
219
|
+ datacnt = 1;
|
|
220
|
+ }
|
|
221
|
+
|
|
222
|
+ }
|
|
223
|
+ System.GC.Collect(0, GCCollectionMode.Forced);
|
|
224
|
+ System.GC.WaitForFullGCComplete();
|
|
225
|
+
|
|
226
|
+ }
|
|
227
|
+ private void PacketData(byte[] Text)
|
|
228
|
+ {
|
|
229
|
+
|
|
230
|
+ System.GC.Collect(0, GCCollectionMode.Forced);
|
|
231
|
+ System.GC.WaitForFullGCComplete();
|
|
232
|
+
|
|
233
|
+ }
|
|
234
|
+ int LineLimit = 500;
|
|
235
|
+
|
|
236
|
+ [DllImport("user32.dll")]
|
|
237
|
+ public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
|
|
238
|
+ private const int WM_SETREDRAW = 255;
|
|
239
|
+ public void Controller_TextLoad(string text)
|
|
240
|
+ {
|
|
241
|
+ int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
|
|
242
|
+ try
|
|
243
|
+ {
|
|
244
|
+ try
|
|
245
|
+ {
|
|
246
|
+ SendMessage(this.Handle, WM_SETREDRAW, false, 0);
|
|
247
|
+ }
|
|
248
|
+ catch { return; }
|
|
249
|
+ if (richTextBox1.Lines.Length > nLimitLines)
|
|
250
|
+ {
|
|
251
|
+ LinkedList<string> tempLines = new LinkedList<string>(richTextBox1.Lines);
|
|
252
|
+ while ((tempLines.Count - nLimitLines) > 0)
|
|
253
|
+ {
|
|
254
|
+ tempLines.RemoveFirst();
|
|
255
|
+ }
|
|
256
|
+
|
|
257
|
+ richTextBox1.Lines = tempLines.ToArray();
|
|
258
|
+
|
|
259
|
+ }
|
|
260
|
+ try
|
|
261
|
+ {
|
|
262
|
+ SendMessage(this.Handle, WM_SETREDRAW, true, 0);
|
|
263
|
+ }
|
|
264
|
+ catch { return; }
|
|
265
|
+ richTextBox1.AppendText(text);
|
|
266
|
+ richTextBox1.SelectionStart = richTextBox1.Text.Length;//맨 마지막 선택...
|
|
267
|
+ richTextBox1.ScrollToCaret();
|
|
268
|
+ }
|
|
269
|
+ catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
|
|
270
|
+ }
|
|
271
|
+ // 이 코드에는 엑셀파일에서 불러온 데이터를 데이터그리드뷰(DataGridView)에 바인딩하는 것도 포함되어 있다.
|
|
272
|
+ private void btnOpenExcel_Click(object sender, EventArgs e)
|
|
273
|
+ {
|
|
274
|
+ // 엑셀 변수 선언
|
|
275
|
+ Excel.Application xlApp = null;
|
|
276
|
+ Excel.Workbook xlWorkbook = null;
|
|
277
|
+ Excel.Worksheet xlWorksheet = null;
|
|
278
|
+
|
|
279
|
+ // 파일 선택
|
|
280
|
+ OpenFileDialog ofd = new OpenFileDialog();
|
|
281
|
+ ofd.Filter = "Excel File (*.xlsx)|*.xlsx|Excel File 97~2003 (*.xls)|*.xls|All Files (*.*)|*.*";
|
|
282
|
+
|
|
283
|
+ if (ofd.ShowDialog() == DialogResult.OK)
|
|
284
|
+ {
|
|
285
|
+ try
|
|
286
|
+ {
|
|
287
|
+ // 데이터그리드뷰 클리어
|
|
288
|
+ dgvList.Columns.Clear();
|
|
289
|
+
|
|
290
|
+ // 엑셀데이터를 담을 데이터테이블 선언
|
|
291
|
+ DataTable dt = new DataTable();
|
|
292
|
+
|
|
293
|
+ // 엑셀 변수들 초기화
|
|
294
|
+ xlApp = new Excel.Application();
|
|
295
|
+ xlWorkbook = xlApp.Workbooks.Open(ofd.FileName);
|
|
296
|
+ xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1); // 첫 번째 시트
|
|
297
|
+
|
|
298
|
+ // 시트에서 범위 설정
|
|
299
|
+ // UsedRange는 사용된 셀 모두이므로
|
|
300
|
+ // 범위를 따로 지정하려면
|
|
301
|
+ // xlWorksheet.Range[xlWorksheet.Cells[시작 행, 시작 열], xlWorksheet.Cells[끝 행, 끝 열]]
|
|
302
|
+ Excel.Range range = xlWorksheet.UsedRange;
|
|
303
|
+
|
|
304
|
+ // 2차원 배열에 담기
|
|
305
|
+ object[,] data = range.Value;
|
|
306
|
+
|
|
307
|
+ // 데이터테이블에 엑셀 칼럼만큼 칼럼 추가
|
|
308
|
+ for (int i = 1; i <= range.Columns.Count; i++)
|
|
309
|
+ {
|
|
310
|
+ dt.Columns.Add(i.ToString(), typeof(string));
|
|
311
|
+ }
|
|
312
|
+
|
|
313
|
+ // 데이터테이블에 2차원 배열에 담은 엑셀데이터 추가
|
|
314
|
+ for (int r = 1; r <= range.Rows.Count; r++)
|
|
315
|
+ {
|
|
316
|
+ DataRow dr = dt.Rows.Add();
|
|
317
|
+
|
|
318
|
+ for (int c = 1; c <= range.Columns.Count; c++)
|
|
319
|
+ {
|
|
320
|
+ dr[c - 1] = data[r, c];
|
|
321
|
+ }
|
|
322
|
+ }
|
|
323
|
+
|
|
324
|
+ xlWorkbook.Close(true);
|
|
325
|
+ xlApp.Quit();
|
|
326
|
+
|
|
327
|
+ // 데이터그리드뷰에 데이터테이블 바인딩
|
|
328
|
+ dgvList.DataSource = dt;
|
|
329
|
+
|
|
330
|
+ }
|
|
331
|
+ catch (Exception ex)
|
|
332
|
+ {
|
|
333
|
+ MessageBox.Show(ex.Message);
|
|
334
|
+ }
|
|
335
|
+ finally
|
|
336
|
+ {
|
|
337
|
+
|
|
338
|
+ // 사용이 끝난 엑셀파일 Release
|
|
339
|
+ ReleaseExcelObject(xlWorksheet);
|
|
340
|
+ ReleaseExcelObject(xlWorkbook);
|
|
341
|
+ ReleaseExcelObject(xlApp);
|
|
342
|
+ }
|
|
343
|
+ }
|
|
344
|
+ }
|
|
345
|
+ private void ReleaseExcelObject(object obj)
|
|
346
|
+ {
|
|
347
|
+ try
|
|
348
|
+ {
|
|
349
|
+ if (obj != null)
|
|
350
|
+ {
|
|
351
|
+ Marshal.ReleaseComObject(obj);
|
|
352
|
+ obj = null;
|
|
353
|
+ }
|
|
354
|
+ }
|
|
355
|
+ catch (Exception ex)
|
|
356
|
+ {
|
|
357
|
+ obj = null;
|
|
358
|
+ throw ex;
|
|
359
|
+ }
|
|
360
|
+ finally
|
|
361
|
+ {
|
|
362
|
+ GC.Collect();
|
|
363
|
+ }
|
|
364
|
+ }
|
|
365
|
+ private void Button_Start_Click(object sender, EventArgs e)
|
|
366
|
+ {
|
|
367
|
+
|
|
368
|
+ serialPort1.PortName = comboBox1.SelectedItem.ToString();
|
|
369
|
+ serialPort1.PortName = comboBox1.Text.ToString();//
|
|
370
|
+ try
|
|
371
|
+ {
|
|
372
|
+ serialPort1.Open();
|
|
373
|
+ }
|
|
374
|
+ catch (Exception ea)
|
|
375
|
+ {
|
|
376
|
+
|
|
377
|
+ MessageBox.Show(ea.Message);
|
|
378
|
+
|
|
379
|
+ }
|
|
380
|
+ if (serialPort1.IsOpen)
|
|
381
|
+ {
|
|
382
|
+ try
|
|
383
|
+ {
|
|
384
|
+ PacketData("Port Open Connect!!!\r\n");
|
|
385
|
+ }
|
|
386
|
+ catch { }
|
|
387
|
+ //rbText.Text = "[" + SP1.PortName.ToString() + "] Port Open Connect!!";
|
|
388
|
+
|
|
389
|
+ //Serial on/off Button의 보이고 안보이게 하기
|
|
390
|
+ Button_Start.Enabled = false; //on 기능 소멸
|
|
391
|
+ Button_Start.Visible = false; //on 버튼 사라짐
|
|
392
|
+ Button_Stop.Enabled = true; //off 기능 나타남
|
|
393
|
+ Button_Stop.Visible = true; //off 버튼 나타남.
|
|
394
|
+ SerialFormEnagleChoice(false); //serial 설정버튼 off
|
|
395
|
+ //timer1.Start();
|
|
396
|
+ Button_Start.Visible = false;
|
|
397
|
+ }
|
|
398
|
+ else
|
|
399
|
+ {
|
|
400
|
+ try
|
|
401
|
+ {
|
|
402
|
+ // Controller_Debug.tbReceived2.Text = "Port Open Failed!!!";
|
|
403
|
+ MessageBox.Show("Port Open Failed!!!");
|
|
404
|
+ }
|
|
405
|
+ catch { }
|
|
406
|
+ }
|
|
407
|
+
|
|
408
|
+ //////////////////////////다른 프로그래머의 데이터
|
|
409
|
+ /*
|
|
410
|
+ * if (serialPort1.IsOpen) serialPort1.Close();
|
|
411
|
+
|
|
412
|
+ serialPort1.PortName = comboBox1.Text;
|
|
413
|
+ tbReceived.Clear();
|
|
414
|
+ tbReceived.Text += serialPort1.PortName + "Selected\n";
|
|
415
|
+ */
|
|
416
|
+ ////////////////////
|
|
417
|
+ }
|
|
418
|
+ }
|
|
419
|
+}
|