Main.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.IO.Ports;
  8. using System.Linq;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using Excel = Microsoft.Office.Interop.Excel;
  15. using System.Reflection;
  16. using Microsoft.WindowsAPICodePack.Dialogs;
  17. //using Microsoft.Office.Interop.Excel;
  18. namespace APL_TestCheck
  19. {
  20. public partial class Main : Form
  21. {
  22. public string Serial_Name { get => serialPort.PortName; set => serialPort.PortName = value; }
  23. public Main()
  24. {
  25. InitializeComponent();
  26. }
  27. private void panel_PanelMode_MouseLeave(object sender, EventArgs e)
  28. {
  29. System.Windows.Forms.Panel Panel = (System.Windows.Forms.Panel)sender;
  30. Panel.BackColor = Color.FromArgb(0, 159, 200);
  31. label_GPSTest_MouseLeave(null, null);
  32. }
  33. private void panel_PanelMode_MouseMove(object sender, MouseEventArgs e)
  34. {
  35. System.Windows.Forms.Panel Panel = (System.Windows.Forms.Panel)sender;
  36. Panel.BackColor = Color.FromArgb(0, 159, 255);
  37. }
  38. private void label_GPSTest_MouseLeave(object sender, EventArgs e)
  39. {
  40. if (panel_GpsTest.Visible == true)
  41. {
  42. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  43. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  44. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  45. }
  46. else if (panel_Catm1Test.Visible == true)
  47. {
  48. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  49. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  50. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  51. }
  52. else if (panel_SensorTest.Visible == true)
  53. {
  54. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  55. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  56. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  57. }
  58. }
  59. private void label_Catm1Test_MouseLeave(object sender, EventArgs e)
  60. {
  61. if (panel_GpsTest.Visible == true)
  62. {
  63. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  64. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  65. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  66. }
  67. else if (panel_Catm1Test.Visible == true)
  68. {
  69. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  70. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  71. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  72. }
  73. else if (panel_SensorTest.Visible == true)
  74. {
  75. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  76. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  77. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  78. }
  79. }
  80. private void label_SensorTest_MouseLeave(object sender, EventArgs e)
  81. {
  82. if (panel_GpsTest.Visible == true)
  83. {
  84. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  85. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  86. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  87. }
  88. else if (panel_Catm1Test.Visible == true)
  89. {
  90. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  91. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  92. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  93. }
  94. else if (panel_SensorTest.Visible == true)
  95. {
  96. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  97. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  98. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  99. }
  100. }
  101. private void label_Catm1Test_MouseMove(object sender, MouseEventArgs e)
  102. {
  103. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  104. }
  105. private void label_SensorTest_MouseMove(object sender, MouseEventArgs e)
  106. {
  107. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  108. }
  109. private void label_GPSTest_MouseMove(object sender, MouseEventArgs e)
  110. {
  111. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  112. }
  113. public struct POINT { public int X, Y; }
  114. [DllImport("user32.dll")] // 현재 마우스 위치를 얻기위한 API함수.
  115. public extern static void GetCursorPos(out POINT point);
  116. System.Drawing.Point FormLocation; // 현재 폼 위치
  117. POINT LastLocation = new POINT(); // 방금 전의 마우스 위치
  118. POINT CurrentLocation = new POINT(); // 현재 마우스 위치
  119. // 폼이 움직일 양 = CurrentLocation - LastLocation.
  120. bool IsMouseMoveStart = false; // 현재 마우스 움직이기 기능이 켜져있는가.
  121. // 만약 이게 없으면 그냥 폼위에서
  122. private void panel_Main_MouseDown(object sender, MouseEventArgs e)
  123. {
  124. GetCursorPos(out CurrentLocation);
  125. FormLocation = this.Location;
  126. IsMouseMoveStart = true;
  127. }
  128. private void panel_Main_MouseMove(object sender, MouseEventArgs e)
  129. {
  130. if (!IsMouseMoveStart) return;
  131. GetCursorPos(out LastLocation);
  132. FormLocation.X -= (CurrentLocation.X - LastLocation.X);
  133. FormLocation.Y -= (CurrentLocation.Y - LastLocation.Y);
  134. this.Location = FormLocation;
  135. CurrentLocation = LastLocation;
  136. }
  137. private void panel_Main_MouseUp(object sender, MouseEventArgs e)
  138. {
  139. IsMouseMoveStart = false;
  140. }
  141. private void pictureBox_min_red_Click(object sender, EventArgs e)
  142. {
  143. this.WindowState = FormWindowState.Minimized;
  144. }
  145. private void pictureBox_X_red_Click(object sender, EventArgs e)
  146. {
  147. //Application.Exit();
  148. }
  149. private void label_GPSTest_Click(object sender, EventArgs e)
  150. {
  151. panel_GpsTest.Visible = true;
  152. panel_SensorTest.Visible = false;
  153. panel_Catm1Test.Visible = false;
  154. }
  155. private void label_Catm1Test_Click(object sender, EventArgs e)
  156. {
  157. panel_GpsTest.Visible = false;
  158. panel_SensorTest.Visible = false;
  159. panel_Catm1Test.Visible = true;
  160. }
  161. private void label_SensorTest_Click(object sender, EventArgs e)
  162. {
  163. panel_GpsTest.Visible = false;
  164. panel_SensorTest.Visible = true;
  165. panel_Catm1Test.Visible = false;
  166. }
  167. private void Main_Load(object sender, EventArgs e)
  168. {
  169. Serial_Initialize(ref comboBox_Port);
  170. dataGridView_Gps.RowCount = 1;
  171. dataGridView_Catm1.RowCount = 1;
  172. dataGridView_Sensor.RowCount = 1;
  173. panel_GpsTest.Visible = true;
  174. panel_SensorTest.Visible = false;
  175. panel_Catm1Test.Visible = false;
  176. pictureBox_Check1.Visible = false;
  177. pictureBox_Check2.Visible = false;
  178. pictureBox_Check3.Visible = false;
  179. pictureBox_Check4.Visible = false;
  180. }
  181. public void Serial_Initialize(ref ComboBox cb_port)
  182. {
  183. this.serialPort = new System.IO.Ports.SerialPort();
  184. this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.Serial_DataRecvFunction);
  185. cb_port.BeginUpdate();
  186. foreach (string comport in SerialPort.GetPortNames())//foreach (string comport in SerialPort_TestProgram.GetPortNames())
  187. {
  188. cb_port.Items.Add(comport);
  189. }
  190. cb_port.EndUpdate();
  191. //SerialPort 초기 설정.
  192. // serialPort.Encoding = Encoding.GetEncoding("Windows-1252");
  193. cb_port.DataSource = SerialPort.GetPortNames();
  194. try
  195. {
  196. serialPort.PortName = Serial_Name = cb_port.SelectedItem.ToString();
  197. serialPort.BaudRate = (int)115200;
  198. serialPort.DataBits = (int)8;
  199. serialPort.Parity = System.IO.Ports.Parity.None;
  200. serialPort.StopBits = StopBits.One;
  201. }
  202. catch { }
  203. }
  204. private delegate void StringSend(string Text);
  205. public void Serial_DataRecvFunction(object sender, SerialDataReceivedEventArgs e)
  206. {
  207. int nLnegth = serialPort.BytesToRead;
  208. byte[] btdata = new byte[nLnegth];
  209. serialPort.Read(btdata, 0, nLnegth);
  210. string data = Encoding.Default.GetString(btdata);
  211. if (checkBox_TerminalStop.Checked == true)
  212. return;
  213. // string data = serialPort.ReadExisting();
  214. this.Invoke(new StringSend(Data_Recv_Str), data);
  215. /****
  216. *메모리 누수 방지용 코드
  217. */
  218. System.GC.Collect(0, GCCollectionMode.Forced);
  219. System.GC.WaitForFullGCComplete();
  220. }
  221. private void label_Port_MouseClick(object sender, MouseEventArgs e)
  222. {
  223. }
  224. int LineLimit = 500;
  225. [DllImport("user32.dll")]
  226. public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
  227. private const int WM_SETREDRAW = 11;
  228. public void Data_Recv_Str(string text)
  229. {
  230. System.Windows.Forms.RichTextBox tbReceived = null;
  231. if (panel_GpsTest.Visible == true) {
  232. tbReceived = richTextBox_Gps;
  233. }
  234. else if (panel_Catm1Test.Visible == true) {
  235. tbReceived = richTextBox_catm1;
  236. }
  237. else if (panel_SensorTest.Visible == true) {
  238. tbReceived = richTextBox_Sensor;
  239. }
  240. int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
  241. try
  242. {
  243. try
  244. {
  245. SendMessage(this.Handle, WM_SETREDRAW, false, 0);
  246. }
  247. catch (Exception e) { MessageBox.Show(e.StackTrace); }
  248. if (tbReceived.Lines.Length > nLimitLines)
  249. {
  250. LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
  251. while ((tempLines.Count - nLimitLines) > 0)
  252. {
  253. tempLines.RemoveFirst();
  254. }
  255. tbReceived.Lines = tempLines.ToArray();
  256. }
  257. try
  258. {
  259. SendMessage(this.Handle, WM_SETREDRAW, true, 0);
  260. }
  261. catch { return; }
  262. if (checkBox_GPGSV.Checked == true && panel_GpsTest.Visible == true)
  263. {
  264. tbReceived.AppendText(GPS_TestFunc(text));
  265. }
  266. else
  267. {
  268. tbReceived.AppendText(text);
  269. }
  270. tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택...
  271. tbReceived.ScrollToCaret();
  272. }
  273. catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
  274. }
  275. public int WordNum(String data, String fixdata)
  276. {
  277. MatchCollection matches = Regex.Matches(data, fixdata);
  278. return matches.Count;
  279. }
  280. double GPS_TestSnRValue = 0;
  281. int Gps_CurrentCount = 0;
  282. int TestVal = 0;
  283. private string GPS_TestFunc(string data)
  284. {
  285. int Gps_Count, Sentence, SnrIndex = 3;
  286. // string tmpstr = "$GPGSV,2,1,08,10,69,246,47,12,50,082,43,15,12,108,29,20,49,190,45*77\r\n";
  287. // string tmpstr1 = "$GPGSV,2,2,08,24,36,052,37,25,48,159,42,31,17,245,,32,46,312,45*74\r\n";
  288. string[] tmpstr = new String[3];
  289. string splitstr = null, str = data;
  290. string[] sp = str.Split(',');
  291. int Losscount = 0;
  292. int location1, location2, LastLocation, SentenceCount;
  293. try
  294. {
  295. /*
  296. if(TestVal == 0)
  297. {
  298. str = tmpstr;
  299. TestVal++;
  300. }
  301. else
  302. {
  303. str = tmpstr1;
  304. TestVal = 0;
  305. }
  306. */
  307. location1 = str.IndexOf("$GPGSV");
  308. SentenceCount = WordNum(str, "GPGSV");
  309. GPS_TestSnRValue = 0;
  310. Gps_CurrentCount = 0;
  311. if (location1 != -1)
  312. {
  313. for (int ii = 0; ii < SentenceCount; ii++)
  314. {
  315. SnrIndex = 3;
  316. if (ii != 0)
  317. str = splitstr;
  318. location1 = str.IndexOf("$GPGSV");
  319. str = str.Substring(location1);
  320. location2 = str.IndexOf("\r\n");
  321. splitstr = str.Substring(location2 + 2);
  322. tmpstr[ii] = str = str.Substring(0, location2 + 2);
  323. sp = str.Split(',');
  324. if (sp[3].Contains("*"))
  325. {
  326. sp[3] = sp[3].Substring(0, 2);
  327. dataGridView_Gps.Rows[0].Cells[1].Value = sp[3];
  328. return str;
  329. }
  330. else
  331. {
  332. Sentence = Convert.ToInt32(sp[2]);
  333. Gps_Count = Convert.ToInt32(sp[3]);
  334. for (int i = 0; i < 4; i++)
  335. {
  336. SnrIndex += 4;
  337. if (sp[SnrIndex].Contains("*"))
  338. {
  339. LastLocation = sp[SnrIndex].IndexOf("*") - 2;
  340. sp[SnrIndex] = sp[SnrIndex].Substring(0, 2);
  341. GPS_TestSnRValue += Convert.ToInt32(sp[SnrIndex]);
  342. }
  343. else
  344. {
  345. if (sp[SnrIndex] == "")
  346. {
  347. sp[SnrIndex] = "0";
  348. Losscount++;
  349. }
  350. GPS_TestSnRValue += Convert.ToInt32(sp[SnrIndex]);
  351. }
  352. Gps_CurrentCount++;
  353. if (Gps_CurrentCount == Gps_Count)
  354. {
  355. GPS_TestSnRValue /= (Gps_Count - Losscount);
  356. GPS_TestSnRValue = Math.Round(GPS_TestSnRValue, 1);
  357. dataGridView_Gps.Rows[0].Cells[1].Value = GPS_TestSnRValue.ToString();
  358. break;
  359. }
  360. }
  361. }
  362. }
  363. str = null;
  364. for (int a = 0; a < SentenceCount; a++)
  365. str += tmpstr[a];
  366. return str;
  367. }
  368. return "";
  369. }
  370. catch (Exception e) { /*MessageBox.Show(e.StackTrace); */}
  371. return "";
  372. }
  373. private void label_Port_MouseLeave(object sender, EventArgs e)
  374. {
  375. panel_Port.BackColor = Color.FromArgb(0, 159, 200);
  376. }
  377. private void label_Port_MouseMove(object sender, MouseEventArgs e)
  378. {
  379. panel_Port.BackColor = Color.FromArgb(0, 159, 255);
  380. }
  381. private void label_Port_Click(object sender, EventArgs e)
  382. {
  383. // Boolean ret = false;
  384. try
  385. {
  386. if (serialPort.IsOpen) // 이미 포트가 열려 있을 때
  387. { //When the port is open
  388. serialPort.Close();
  389. label_Port.Text = "Port Open";
  390. //ret = true;
  391. }
  392. else//When the port is close //포트가 열려 있지 않을 때
  393. {
  394. if (comboBox_Port.Text != "")
  395. {
  396. serialPort.PortName = Serial_Name = comboBox_Port.SelectedItem.ToString();
  397. serialPort.BaudRate = Convert.ToInt32(comboBox_bps.Text);
  398. serialPort.Open();
  399. label_Port.Text = "Port Close";
  400. // Debug.Debug_Main_Form_Get(this.main_form);
  401. }
  402. else
  403. {
  404. MessageBox.Show("Port is not set");
  405. // ret = true;
  406. }
  407. }
  408. }
  409. catch
  410. {
  411. MessageBox.Show("Port Open Faile" + Serial_Name);
  412. }
  413. // return ret;
  414. }
  415. public static DialogResult InputBox(string title, string promptText, ref string value)
  416. {
  417. Form form = new Form();
  418. System.Windows.Forms.Label label = new System.Windows.Forms.Label();
  419. System.Windows.Forms.TextBox textBox = new System.Windows.Forms.TextBox();
  420. System.Windows.Forms.Button buttonOk = new System.Windows.Forms.Button();
  421. System.Windows.Forms.Button buttonCancel = new System.Windows.Forms.Button();
  422. form.Text = title;
  423. label.Text = promptText;
  424. textBox.Text = value;
  425. buttonOk.Text = "OK";
  426. buttonCancel.Text = "Cancel";
  427. buttonOk.DialogResult = DialogResult.OK;
  428. buttonCancel.DialogResult = DialogResult.Cancel;
  429. label.SetBounds(9, 20, 372, 13);
  430. textBox.SetBounds(12, 36, 372, 20);
  431. buttonOk.SetBounds(228, 72, 75, 23);
  432. buttonCancel.SetBounds(309, 72, 75, 23);
  433. label.AutoSize = true;
  434. textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
  435. buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  436. buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  437. form.ClientSize = new Size(396, 107);
  438. form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
  439. form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
  440. form.FormBorderStyle = FormBorderStyle.FixedDialog;
  441. form.StartPosition = FormStartPosition.CenterScreen;
  442. form.MinimizeBox = false;
  443. form.MaximizeBox = false;
  444. form.AcceptButton = buttonOk;
  445. form.CancelButton = buttonCancel;
  446. DialogResult dialogResult = form.ShowDialog();
  447. value = textBox.Text;
  448. return dialogResult;
  449. }
  450. private void dataGridView_Sensor_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  451. {
  452. /* string value = "";
  453. if (checkBox_Edit.Checked == false)
  454. {
  455. if (InputBox("번호 입력", "일련 번호:", ref value) == DialogResult.OK)
  456. {
  457. Name = value;
  458. }
  459. }*/
  460. }
  461. private void dataGridView_Catm1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  462. {
  463. /* string value = "";
  464. if (checkBox_Edit.Checked == false)
  465. {
  466. if (InputBox("번호 입력", "일련 번호:", ref value) == DialogResult.OK)
  467. {
  468. Name = value;
  469. }
  470. }*/
  471. }
  472. private void dataGridView_Gps_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  473. {
  474. /* string value = "";
  475. if (checkBox_Edit.Checked == false)
  476. {
  477. if (InputBox("번호 입력", "일련 번호:", ref value) == DialogResult.OK)
  478. {
  479. Name = value;
  480. }
  481. }*/
  482. }
  483. private void button_Clear_Click(object sender, EventArgs e)
  484. {
  485. System.Windows.Forms.RichTextBox tbReceived = null;
  486. if (panel_GpsTest.Visible == true)
  487. {
  488. tbReceived = richTextBox_Gps;
  489. }
  490. else if (panel_Catm1Test.Visible == true)
  491. {
  492. tbReceived = richTextBox_catm1;
  493. }
  494. else if (panel_SensorTest.Visible == true)
  495. {
  496. tbReceived = richTextBox_Sensor;
  497. }
  498. tbReceived.Text = "";
  499. }
  500. string FileSaveSet = null;
  501. private void button_GpsTestSave_Click(object sender, EventArgs e)
  502. {
  503. string CheckProductionNum = null;
  504. try
  505. {
  506. CheckProductionNum = dataGridView_Gps.Rows[0].Cells[0].Value.ToString();
  507. }
  508. catch
  509. {
  510. }
  511. try
  512. {
  513. if (!CheckProductionNum.Contains("BL-VITE00-V"))
  514. {
  515. MessageBox.Show("잘못된 생산 번호 입니다.");
  516. return;
  517. }
  518. }catch
  519. {
  520. MessageBox.Show("생산번호를 적어주세요.");
  521. }
  522. string Ref_Xls_File = new System.IO.DirectoryInfo(System.Windows.Forms.Application.StartupPath).ToString();
  523. string Dst_Xls_File = null;
  524. Ref_Xls_File = Ref_Xls_File + @"\APL_200221.xls";
  525. FileInfo SrcfileInfo = new FileInfo(Ref_Xls_File);
  526. FileInfo DstfileInfo;
  527. if (dataGridView_Gps.Rows[0].Cells[0].Value != null
  528. && dataGridView_Gps.Rows[0].Cells[1].Value != null)
  529. {
  530. if (SrcfileInfo.Exists)
  531. {
  532. if (FileSaveSet == null)
  533. {
  534. button_Path_Click(null, null);
  535. return;
  536. // MessageBox.Show("레퍼런스 엑셀 파일이 존재하지 않습니다. \r\n강차장님에게 문의 하세요.");
  537. }
  538. else
  539. {
  540. // File Copy 후 Data 입력
  541. Dst_Xls_File = FileSaveSet + @"\" + dataGridView_Gps.Rows[0].Cells[0].Value.ToString() + @".xls";
  542. DstfileInfo = new FileInfo(Dst_Xls_File);
  543. if (DstfileInfo.Exists)
  544. {
  545. if (MessageBox.Show("이미 파일이 있습니다.\r\n 덮어씌우겠습니까?", "YesOrNo", MessageBoxButtons.YesNo) == DialogResult.Yes)
  546. {
  547. try
  548. {
  549. File.Copy(Ref_Xls_File, Dst_Xls_File, true);
  550. }
  551. catch (Exception ex)
  552. {
  553. MessageBox.Show(ex.StackTrace);
  554. return;
  555. }
  556. }
  557. else
  558. {
  559. //MessageBox.Show("아니요 클릭");
  560. return; // 아니오시 Return;
  561. }
  562. }
  563. else
  564. {
  565. File.Copy(Ref_Xls_File, Dst_Xls_File);
  566. }
  567. }
  568. }
  569. else
  570. {
  571. MessageBox.Show("레퍼런스 엑셀 파일이 존재하지 않습니다. \r\n강차장님에게 문의 하세요.");
  572. return;
  573. }
  574. }
  575. else
  576. {
  577. MessageBox.Show("생산번호/GPS SNR 값이 제대로 입력 되지 않았습니다.\r\n확인후 다시 저장하세요.");
  578. return;
  579. }
  580. Gps_DataxlsWrite(Dst_Xls_File);
  581. dataGridView_Gps.Rows[0].Cells[0].Value = null;
  582. dataGridView_Gps.Rows[0].Cells[1].Value = null;
  583. // if (!di.Exists) { di.Create(); }
  584. //string tmppath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  585. #if false
  586. if (dataGridView_Gps.Rows[0].Cells[1].Value != null)
  587. {
  588. string path = @"xxxx.txt";
  589. FileInfo file = new FileInfo(path);
  590. if (file.Exists)
  591. { //파일이 있는지
  592. //복사(경로,덮어쓰기 옵션 기본값 false)
  593. //만약 덮어쓰기 false일 때 파일이 존재하면 에러남.
  594. file.CopyTo(@"xxx2.txt", true);
  595. }
  596. }
  597. #endif
  598. }
  599. Excel.Application ExcelApp = null;
  600. Excel.Workbook wb = null;
  601. Excel.Worksheet ws = null;
  602. private void Gps_DataxlsWrite(string dst)
  603. {
  604. //Write
  605. //파일이 존재 한다면..삭제 하고..
  606. /*if (File.Exists("C:\\Test.xls"))
  607. {
  608. File.Delete("C:\\Test.xls");
  609. }*/
  610. try
  611. {
  612. ExcelApp = new Excel.Application();
  613. wb = ExcelApp.Workbooks.Open(dst, 0, false, 5, Missing.Value, Missing.Value, false, Missing.Value,
  614. Missing.Value, true, false, Missing.Value, false, false, false);
  615. ws = wb.Worksheets["Sheet1"] as Excel.Worksheet;
  616. //엑셀 시트 인덱스 번호는 0,0 부터 시작 하는 것이 아니라 1,1 A1 부터 시작 함. 0,0 으로 시작하면 오류...
  617. //시트에 값 쓰기...
  618. ws.Cells[8, 8] = dataGridView_Gps.Rows[0].Cells[0].Value;
  619. ws.Cells[8, 10] = dataGridView_Gps.Rows[0].Cells[1].Value;
  620. /*ws.Cells[1, 3] = "123";
  621. ws.Cells[1, 4] = "1234";
  622. ws.Cells[2, 1] = "Test1"; //A2
  623. ws.Cells[3, 1] = "Test12"; //A3
  624. ws.Cells[4, 1] = "Test123"; //A4*/
  625. //다른 이름으로 저장하기...
  626. wb.Save(); // => 오픈한 파일 그대로 저장...
  627. /* wb.SaveAs("C:\\Test.xls",
  628. Excel.XlFileFormat.xlWorkbookNormal,
  629. Type.Missing,
  630. Type.Missing,
  631. Type.Missing,
  632. Type.Missing,
  633. Excel.XlSaveAsAccessMode.xlNoChange,
  634. Type.Missing,
  635. Type.Missing,
  636. Type.Missing,
  637. Type.Missing,
  638. Type.Missing);
  639. */
  640. //파일 닫기...
  641. wb.Close(false, Type.Missing, Type.Missing);
  642. wb = null;
  643. ExcelApp.Quit();
  644. }
  645. catch (Exception ex)
  646. {
  647. //객체들 메모리 해제
  648. ReleaseExcelObject(ws);
  649. ReleaseExcelObject(wb);
  650. ReleaseExcelObject(ExcelApp);
  651. GC.Collect();
  652. }
  653. finally
  654. {
  655. //객체들 메모리 해제
  656. ReleaseExcelObject(ws);
  657. ReleaseExcelObject(wb);
  658. ReleaseExcelObject(ExcelApp);
  659. GC.Collect();
  660. }
  661. }
  662. private void ReleaseExcelObject(object obj)
  663. {
  664. try
  665. {
  666. if (obj != null)
  667. {
  668. Marshal.ReleaseComObject(obj);
  669. obj = null;
  670. }
  671. }
  672. catch (Exception ex)
  673. {
  674. obj = null;
  675. throw ex;
  676. }
  677. finally
  678. {
  679. GC.Collect();
  680. }
  681. }
  682. // CommonOpenFileDialog 클래스 생성
  683. CommonOpenFileDialog dialog = new CommonOpenFileDialog();
  684. private void button_Path_Click(object sender, EventArgs e)
  685. {
  686. // 처음 보여줄 폴더 설정(안해도 됨)
  687. //dialog.InitialDirectory = "";
  688. dialog.IsFolderPicker = true;
  689. if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
  690. {
  691. FileSaveSet = dialog.FileName;
  692. // label.Text = dialog.FileName;
  693. // 테스트용, 폴더 선택이 완료되면 선택된 폴더를 label에 출력
  694. }
  695. }
  696. private void dataGridView_Gps_KeyDown(object sender, KeyEventArgs e)
  697. {
  698. if (e.KeyCode == Keys.Enter)
  699. {
  700. button_GpsTestSave_Click(null,null);
  701. //to do
  702. }
  703. else
  704. {
  705. return;
  706. }
  707. }
  708. }
  709. }