Main.cs 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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 System.Threading;
  18. //using Microsoft.Office.Interop.Excel;
  19. namespace APL_TestCheck
  20. {
  21. public partial class Main : Form
  22. {
  23. public string Serial_Name { get => serialPort.PortName; set => serialPort.PortName = value; }
  24. public Main()
  25. {
  26. InitializeComponent();
  27. }
  28. private void panel_PanelMode_MouseLeave(object sender, EventArgs e)
  29. {
  30. System.Windows.Forms.Panel Panel = (System.Windows.Forms.Panel)sender;
  31. Panel.BackColor = Color.FromArgb(0, 159, 200);
  32. label_GPSTest_MouseLeave(null, null);
  33. }
  34. private void panel_PanelMode_MouseMove(object sender, MouseEventArgs e)
  35. {
  36. System.Windows.Forms.Panel Panel = (System.Windows.Forms.Panel)sender;
  37. Panel.BackColor = Color.FromArgb(0, 159, 255);
  38. }
  39. private void label_GPSTest_MouseLeave(object sender, EventArgs e)
  40. {
  41. if (panel_GpsTest.Visible == true)
  42. {
  43. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  44. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  45. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  46. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  47. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  48. }
  49. else if (panel_Catm1Test.Visible == true)
  50. {
  51. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  52. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  53. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  54. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  55. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  56. }
  57. else if (panel_SensorTest.Visible == true)
  58. {
  59. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  60. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  61. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  62. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  63. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  64. }
  65. else if (panel_MergeMain.Visible == true)
  66. {
  67. panel_Merge.BackColor = Color.FromArgb(0, 159, 255);
  68. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  69. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  70. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  71. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  72. }
  73. else if (panel_SnCheckMain.Visible == true)
  74. {
  75. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  76. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  77. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  78. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  79. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 255);
  80. }
  81. }
  82. private void label_Catm1Test_MouseLeave(object sender, EventArgs e)
  83. {
  84. if (panel_GpsTest.Visible == true)
  85. {
  86. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  87. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  88. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  89. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  90. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  91. }
  92. else if (panel_Catm1Test.Visible == true)
  93. {
  94. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  95. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  96. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  97. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  98. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  99. }
  100. else if (panel_SensorTest.Visible == true)
  101. {
  102. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  103. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  104. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  105. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  106. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  107. }
  108. else if (panel_MergeMain.Visible == true)
  109. {
  110. panel_Merge.BackColor = Color.FromArgb(0, 159, 255);
  111. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  112. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  113. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  114. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  115. }
  116. else if (panel_SnCheckMain.Visible == true)
  117. {
  118. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  119. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  120. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  121. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  122. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 255);
  123. }
  124. }
  125. private void label_SensorTest_MouseLeave(object sender, EventArgs e)
  126. {
  127. if (panel_GpsTest.Visible == true)
  128. {
  129. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  130. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  131. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  132. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  133. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  134. }
  135. else if (panel_Catm1Test.Visible == true)
  136. {
  137. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  138. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  139. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  140. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  141. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  142. }
  143. else if (panel_SensorTest.Visible == true)
  144. {
  145. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  146. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  147. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  148. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  149. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  150. }
  151. else if (panel_MergeMain.Visible == true)
  152. {
  153. panel_Merge.BackColor = Color.FromArgb(0, 159, 255);
  154. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  155. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  156. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  157. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  158. }
  159. else if (panel_SnCheckMain.Visible == true)
  160. {
  161. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  162. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  163. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  164. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  165. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 255);
  166. }
  167. }
  168. private void label_Merge_MouseLeave(object sender, EventArgs e)
  169. {
  170. if (panel_GpsTest.Visible == true)
  171. {
  172. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  173. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  174. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  175. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  176. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  177. }
  178. else if (panel_Catm1Test.Visible == true)
  179. {
  180. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  181. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  182. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  183. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  184. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  185. }
  186. else if (panel_SensorTest.Visible == true)
  187. {
  188. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  189. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  190. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  191. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  192. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  193. }
  194. else if (panel_MergeMain.Visible == true)
  195. {
  196. panel_Merge.BackColor = Color.FromArgb(0, 159, 255);
  197. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  198. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  199. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  200. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  201. }
  202. else if(panel_SnCheckMain.Visible == true)
  203. {
  204. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  205. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  206. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  207. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  208. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 255);
  209. }
  210. }
  211. private void label_SnCheck_MouseLeave(object sender, EventArgs e)
  212. {
  213. if (panel_GpsTest.Visible == true)
  214. {
  215. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  216. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  217. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  218. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  219. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  220. }
  221. else if (panel_Catm1Test.Visible == true)
  222. {
  223. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  224. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  225. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  226. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  227. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  228. }
  229. else if (panel_SensorTest.Visible == true)
  230. {
  231. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  232. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  233. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  234. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  235. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  236. }
  237. else if (panel_MergeMain.Visible == true)
  238. {
  239. panel_Merge.BackColor = Color.FromArgb(0, 159, 255);
  240. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  241. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  242. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  243. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 200);
  244. }
  245. else if (panel_SnCheckMain.Visible == true)
  246. {
  247. panel_Merge.BackColor = Color.FromArgb(0, 159, 200);
  248. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  249. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  250. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  251. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 255);
  252. }
  253. }
  254. private void label_SnCheck_MouseMove(object sender, MouseEventArgs e)
  255. {
  256. panel_SnCheck.BackColor = Color.FromArgb(0, 159, 255);
  257. }
  258. private void label_Merge_MouseMove(object sender, MouseEventArgs e)
  259. {
  260. panel_Merge.BackColor = Color.FromArgb(0, 159, 255);
  261. }
  262. private void label_Catm1Test_MouseMove(object sender, MouseEventArgs e)
  263. {
  264. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  265. }
  266. private void label_SensorTest_MouseMove(object sender, MouseEventArgs e)
  267. {
  268. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  269. }
  270. private void label_GPSTest_MouseMove(object sender, MouseEventArgs e)
  271. {
  272. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  273. }
  274. public struct POINT { public int X, Y; }
  275. [DllImport("user32.dll")] // 현재 마우스 위치를 얻기위한 API함수.
  276. public extern static void GetCursorPos(out POINT point);
  277. System.Drawing.Point FormLocation; // 현재 폼 위치
  278. POINT LastLocation = new POINT(); // 방금 전의 마우스 위치
  279. POINT CurrentLocation = new POINT(); // 현재 마우스 위치
  280. // 폼이 움직일 양 = CurrentLocation - LastLocation.
  281. bool IsMouseMoveStart = false; // 현재 마우스 움직이기 기능이 켜져있는가.
  282. // 만약 이게 없으면 그냥 폼위에서
  283. private void panel_Main_MouseDown(object sender, MouseEventArgs e)
  284. {
  285. GetCursorPos(out CurrentLocation);
  286. FormLocation = this.Location;
  287. IsMouseMoveStart = true;
  288. }
  289. private void panel_Main_MouseMove(object sender, MouseEventArgs e)
  290. {
  291. if (!IsMouseMoveStart) return;
  292. GetCursorPos(out LastLocation);
  293. FormLocation.X -= (CurrentLocation.X - LastLocation.X);
  294. FormLocation.Y -= (CurrentLocation.Y - LastLocation.Y);
  295. this.Location = FormLocation;
  296. CurrentLocation = LastLocation;
  297. }
  298. private void panel_Main_MouseUp(object sender, MouseEventArgs e)
  299. {
  300. IsMouseMoveStart = false;
  301. }
  302. private void pictureBox_min_red_Click(object sender, EventArgs e)
  303. {
  304. this.WindowState = FormWindowState.Minimized;
  305. }
  306. private void pictureBox_X_red_Click(object sender, EventArgs e)
  307. {
  308. //Application.Exit();
  309. }
  310. private void label_GPSTest_Click(object sender, EventArgs e)
  311. {
  312. panel_GpsTest.Visible = true;
  313. panel_SensorTest.Visible = false;
  314. panel_Catm1Test.Visible = false;
  315. panel_MergeMain.Visible = false;
  316. panel_SnCheckMain.Visible = false;
  317. }
  318. private void label_Catm1Test_Click(object sender, EventArgs e)
  319. {
  320. panel_GpsTest.Visible = false;
  321. panel_SensorTest.Visible = false;
  322. panel_Catm1Test.Visible = true;
  323. panel_MergeMain.Visible = false;
  324. panel_SnCheckMain.Visible = false;
  325. }
  326. private void label_SensorTest_Click(object sender, EventArgs e)
  327. {
  328. panel_GpsTest.Visible = false;
  329. panel_SensorTest.Visible = true;
  330. panel_Catm1Test.Visible = false;
  331. panel_MergeMain.Visible = false;
  332. panel_SnCheckMain.Visible = false;
  333. }
  334. private void Main_Load(object sender, EventArgs e)
  335. {
  336. Serial_Initialize(ref comboBox_Port);
  337. dataGridView_Gps.RowCount = 1;
  338. dataGridView_Catm1.RowCount = 1;
  339. dataGridView_Sensor.RowCount = 1;
  340. panel_GpsTest.Visible = true;
  341. panel_SensorTest.Visible = false;
  342. panel_Catm1Test.Visible = false;
  343. panel_MergeMain.Visible = false;
  344. pictureBox_Check1.Visible = false;
  345. pictureBox_Check2.Visible = false;
  346. pictureBox_Check3.Visible = false;
  347. pictureBox_Check4.Visible = false;
  348. }
  349. public void Serial_Initialize(ref ComboBox cb_port)
  350. {
  351. this.serialPort = new System.IO.Ports.SerialPort();
  352. this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.sPort_DataReceived);
  353. cb_port.BeginUpdate();
  354. foreach (string comport in SerialPort.GetPortNames())//foreach (string comport in SerialPort_TestProgram.GetPortNames())
  355. {
  356. cb_port.Items.Add(comport);
  357. }
  358. cb_port.EndUpdate();
  359. //SerialPort 초기 설정.
  360. // serialPort.Encoding = Encoding.GetEncoding("Windows-1252");
  361. cb_port.DataSource = SerialPort.GetPortNames();
  362. try
  363. {
  364. serialPort.PortName = Serial_Name = cb_port.SelectedItem.ToString();
  365. serialPort.BaudRate = (int)115200;
  366. serialPort.DataBits = (int)8;
  367. serialPort.Parity = System.IO.Ports.Parity.None;
  368. serialPort.StopBits = StopBits.One;
  369. }
  370. catch { }
  371. }
  372. private delegate void StringSend(string Text);
  373. private delegate void ByteSend(byte[] Text);
  374. string startStr ="V";
  375. string endStr = "\n";
  376. public void Serial_DataRecvFunction(object sender, SerialDataReceivedEventArgs e)
  377. {
  378. int TmpLength = 0;
  379. //System.Threading.Thread.Sleep(50);
  380. int nLnegth = serialPort.BytesToRead;
  381. byte[] btdata = new byte[nLnegth];
  382. serialPort.Read(btdata, 0, nLnegth);
  383. string data = Encoding.Default.GetString(btdata);
  384. if (checkBox_TerminalStop.Checked == true)
  385. return;
  386. // string data = serialPort.ReadExisting();
  387. if (panel_SensorTest.Visible == true)
  388. {
  389. if (data.Contains(startStr))
  390. {
  391. data = data.Substring(data.IndexOf(startStr));
  392. if (data.Contains(endStr))
  393. {
  394. this.Invoke(new StringSend(Data_Recv_Str), data);
  395. }
  396. }
  397. }
  398. else
  399. {
  400. this.Invoke(new StringSend(Data_Recv_Str), data);
  401. }
  402. /*try
  403. {
  404. this.Invoke(new ByteSend(Data_Recv_Str), btdata);
  405. }
  406. catch
  407. {
  408. }*/
  409. /****
  410. *메모리 누수 방지용 코드
  411. */
  412. System.GC.Collect(0, GCCollectionMode.Forced);
  413. System.GC.WaitForFullGCComplete();
  414. }
  415. int offset = 0;
  416. byte[] rxBuffer;
  417. private void sPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
  418. {
  419. if (offset == 0)
  420. {
  421. rxBuffer = new byte[4096];
  422. }
  423. string rxString = "";
  424. int intRecSize = serialPort.BytesToRead;
  425. if (intRecSize != 0)
  426. {
  427. serialPort.Read(rxBuffer, offset, intRecSize);
  428. offset += intRecSize;
  429. for (int iTemp = 0; iTemp < offset; iTemp++)
  430. {
  431. rxString += Convert.ToChar(rxBuffer[iTemp]);
  432. }
  433. if (rxString.Contains(startStr))
  434. {
  435. rxString = rxString.Substring(rxString.IndexOf(startStr));
  436. if (rxString.Contains(endStr))
  437. {
  438. this.Invoke(new StringSend(Data_Recv_Str), rxString);
  439. //program.dataReceived(rxString, "Receive");
  440. offset = 0;
  441. }
  442. }
  443. }
  444. }
  445. private void label_Port_MouseClick(object sender, MouseEventArgs e)
  446. {
  447. }
  448. int LineLimit = 500;
  449. [DllImport("user32.dll")]
  450. public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
  451. private const int WM_SETREDRAW = 11;
  452. public void Data_Recv_Str(string text)
  453. {
  454. System.Windows.Forms.RichTextBox tbReceived = null;
  455. if (panel_GpsTest.Visible == true) {
  456. tbReceived = richTextBox_Gps;
  457. }
  458. else if (panel_Catm1Test.Visible == true) {
  459. tbReceived = richTextBox_catm1;
  460. }
  461. else if (panel_SensorTest.Visible == true) {
  462. tbReceived = richTextBox_Sensor;
  463. }
  464. int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
  465. try
  466. {
  467. try
  468. {
  469. SendMessage(this.Handle, WM_SETREDRAW, false, 0);
  470. }
  471. catch (Exception e) { MessageBox.Show(e.StackTrace); }
  472. if (tbReceived.Lines.Length > nLimitLines)
  473. {
  474. LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
  475. while ((tempLines.Count - nLimitLines) > 0)
  476. {
  477. tempLines.RemoveFirst();
  478. }
  479. tbReceived.Lines = tempLines.ToArray();
  480. }
  481. try
  482. {
  483. SendMessage(this.Handle, WM_SETREDRAW, true, 0);
  484. }
  485. catch { return; }
  486. if (checkBox_GPGSV.Checked == true && panel_GpsTest.Visible == true)
  487. {
  488. tbReceived.AppendText(GPS_TestFunc(text));
  489. }
  490. else if(panel_SensorTest.Visible == true)
  491. {
  492. Sensor_TestFunc(text);
  493. tbReceived.AppendText(text);
  494. }
  495. else
  496. {
  497. tbReceived.AppendText(text);
  498. }
  499. tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택...
  500. tbReceived.ScrollToCaret();
  501. }
  502. catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
  503. }
  504. public void Data_Recv_Str(byte[] text)
  505. {
  506. string strtext = Encoding.Default.GetString(text);
  507. System.Windows.Forms.RichTextBox tbReceived = null;
  508. if (panel_GpsTest.Visible == true)
  509. {
  510. tbReceived = richTextBox_Gps;
  511. }
  512. else if (panel_Catm1Test.Visible == true)
  513. {
  514. tbReceived = richTextBox_catm1;
  515. }
  516. else if (panel_SensorTest.Visible == true)
  517. {
  518. tbReceived = richTextBox_Sensor;
  519. }
  520. int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
  521. try
  522. {
  523. try
  524. {
  525. SendMessage(this.Handle, WM_SETREDRAW, false, 0);
  526. }
  527. catch (Exception e) { MessageBox.Show(e.StackTrace); }
  528. if (tbReceived.Lines.Length > nLimitLines)
  529. {
  530. LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
  531. while ((tempLines.Count - nLimitLines) > 0)
  532. {
  533. tempLines.RemoveFirst();
  534. }
  535. tbReceived.Lines = tempLines.ToArray();
  536. }
  537. try
  538. {
  539. SendMessage(this.Handle, WM_SETREDRAW, true, 0);
  540. }
  541. catch { return; }
  542. if (checkBox_GPGSV.Checked == true && panel_GpsTest.Visible == true)
  543. {
  544. tbReceived.AppendText(GPS_TestFunc(Encoding.Default.GetString(text)));
  545. }
  546. else if (panel_SensorTest.Visible == true)
  547. {
  548. Sensor_TestFunc(strtext);
  549. tbReceived.AppendText(strtext);
  550. }
  551. else
  552. {
  553. tbReceived.AppendText(strtext);
  554. }
  555. tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택...
  556. tbReceived.ScrollToCaret();
  557. }
  558. catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
  559. }
  560. public int WordNum(String data, String fixdata)
  561. {
  562. MatchCollection matches = Regex.Matches(data, fixdata);
  563. return matches.Count;
  564. }
  565. double GPS_TestSnRValue = 0;
  566. int Gps_CurrentCount = 0;
  567. int TestVal = 0;
  568. private string GPS_TestFunc(string data)
  569. {
  570. int Gps_Count, Sentence, SnrIndex = 3;
  571. // 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";
  572. // string tmpstr1 = "$GPGSV,2,2,08,24,36,052,37,25,48,159,42,31,17,245,,32,46,312,45*74\r\n";
  573. string[] tmpstr = new String[3];
  574. string splitstr = null, str = data;
  575. string[] sp = str.Split(',');
  576. int Losscount = 0;
  577. int location1, location2, LastLocation, SentenceCount;
  578. try
  579. {
  580. /*
  581. if(TestVal == 0)
  582. {
  583. str = tmpstr;
  584. TestVal++;
  585. }
  586. else
  587. {
  588. str = tmpstr1;
  589. TestVal = 0;
  590. }
  591. */
  592. location1 = str.IndexOf("$GPGSV");
  593. SentenceCount = WordNum(str, "GPGSV");
  594. GPS_TestSnRValue = 0;
  595. Gps_CurrentCount = 0;
  596. if (location1 != -1)
  597. {
  598. for (int ii = 0; ii < SentenceCount; ii++)
  599. {
  600. SnrIndex = 3;
  601. if (ii != 0)
  602. str = splitstr;
  603. location1 = str.IndexOf("$GPGSV");
  604. str = str.Substring(location1);
  605. location2 = str.IndexOf("\r\n");
  606. splitstr = str.Substring(location2 + 2);
  607. tmpstr[ii] = str = str.Substring(0, location2 + 2);
  608. sp = str.Split(',');
  609. if (sp[3].Contains("*"))
  610. {
  611. sp[3] = sp[3].Substring(0, 2);
  612. dataGridView_Gps.Rows[0].Cells[1].Value = sp[3];
  613. return str;
  614. }
  615. else
  616. {
  617. Sentence = Convert.ToInt32(sp[2]);
  618. Gps_Count = Convert.ToInt32(sp[3]);
  619. for (int i = 0; i < 4; i++)
  620. {
  621. SnrIndex += 4;
  622. if (sp[SnrIndex].Contains("*"))
  623. {
  624. LastLocation = sp[SnrIndex].IndexOf("*") - 2;
  625. sp[SnrIndex] = sp[SnrIndex].Substring(0, 2);
  626. GPS_TestSnRValue += Convert.ToInt32(sp[SnrIndex]);
  627. }
  628. else
  629. {
  630. if (sp[SnrIndex] == "")
  631. {
  632. sp[SnrIndex] = "0";
  633. Losscount++;
  634. }
  635. GPS_TestSnRValue += Convert.ToInt32(sp[SnrIndex]);
  636. }
  637. Gps_CurrentCount++;
  638. if (Gps_CurrentCount == Gps_Count)
  639. {
  640. GPS_TestSnRValue /= (Gps_Count - Losscount);
  641. GPS_TestSnRValue = Math.Round(GPS_TestSnRValue, 1);
  642. dataGridView_Gps.Rows[0].Cells[1].Value = GPS_TestSnRValue.ToString();
  643. break;
  644. }
  645. }
  646. }
  647. }
  648. str = null;
  649. for (int a = 0; a < SentenceCount; a++)
  650. str += tmpstr[a];
  651. return str;
  652. }
  653. return "";
  654. }
  655. catch (Exception e) { /*MessageBox.Show(e.StackTrace); */}
  656. return "";
  657. }
  658. string SensorData = null;
  659. private void Sensor_TestFunc(string data)
  660. {
  661. string str = data;
  662. string[] sp = new string[11];
  663. try
  664. {
  665. if (data.Contains("\r\n") == false)
  666. {
  667. return;
  668. }
  669. else
  670. {
  671. SensorData += data;
  672. str = SensorData;
  673. }
  674. if (dataGridView_Sensor.Rows[0].Cells[0].Value != null)
  675. {
  676. //location1 = str.IndexOf("\r\n");
  677. //str = str.Substring(0, location1);
  678. sp = str.Split(',');
  679. if (sp[0].Contains("V") == false/* || sp.Length != 11*/)
  680. {
  681. SensorData = null;
  682. return;
  683. }
  684. dataGridView_Sensor.Rows[0].Cells[5].Value = sp[0]; //version
  685. dataGridView_Sensor.Rows[0].Cells[2].Value = sp[2]; //,IMEI
  686. dataGridView_Sensor.Rows[0].Cells[3].Value = sp[3];//, Phone Number,
  687. dataGridView_Sensor.Rows[0].Cells[4].Value = sp[4];//USIM Number
  688. dataGridView_Sensor.Rows[0].Cells[6].Value = sp[5];//, Battery Voltage,
  689. dataGridView_Sensor.Rows[0].Cells[7].Value = sp[6];// System Voltage,
  690. dataGridView_Sensor.Rows[0].Cells[8].Value = sp[7];//Temp, BMA400 값 출력 x, y, z
  691. dataGridView_Sensor.Rows[0].Cells[9].Value = sp[8];//Temp, BMA400 값 출력 x, y, z
  692. dataGridView_Sensor.Rows[0].Cells[10].Value = sp[9];//Temp, BMA400 값 출력 x, y, z
  693. if (sp[10].Contains("\r"))
  694. {
  695. int loc = sp[10].IndexOf("\r");
  696. sp[10] = sp[10].Substring(0, loc);
  697. }
  698. dataGridView_Sensor.Rows[0].Cells[11].Value = sp[10];//Temp, BMA400 값 출력 x, y, z
  699. SensorData = null;
  700. string value = "";
  701. if (checkBox_Edit.Checked == false)
  702. {
  703. if (InputBox("번호 입력", "일련 번호:", ref value) == DialogResult.OK)
  704. {
  705. // SN
  706. dataGridView_Sensor.Rows[0].Cells[1].Value = Name = value;
  707. serialPort.Write(value);
  708. button_SensorTestSave();
  709. for (int i = 0; i < 12; i++)
  710. dataGridView_Sensor.Rows[0].Cells[i].Value = null;
  711. }
  712. }
  713. dataGridView_Sensor.Refresh();
  714. }
  715. return;
  716. }
  717. catch (Exception ex) { /*MessageBox.Show(ex.StackTrace); */ }
  718. }
  719. private void label_Port_MouseLeave(object sender, EventArgs e)
  720. {
  721. panel_Port.BackColor = Color.FromArgb(0, 159, 200);
  722. }
  723. private void label_Port_MouseMove(object sender, MouseEventArgs e)
  724. {
  725. panel_Port.BackColor = Color.FromArgb(0, 159, 255);
  726. }
  727. private void label_Port_Click(object sender, EventArgs e)
  728. {
  729. // Boolean ret = false;
  730. try
  731. {
  732. if (serialPort.IsOpen) // 이미 포트가 열려 있을 때
  733. { //When the port is open
  734. serialPort.Close();
  735. label_Port.Text = "Port Open";
  736. //ret = true;
  737. }
  738. else//When the port is close //포트가 열려 있지 않을 때
  739. {
  740. if (comboBox_Port.Text != "")
  741. {
  742. serialPort.PortName = Serial_Name = comboBox_Port.SelectedItem.ToString();
  743. serialPort.BaudRate = Convert.ToInt32(comboBox_bps.Text);
  744. serialPort.Open();
  745. label_Port.Text = "Port Close";
  746. // Debug.Debug_Main_Form_Get(this.main_form);
  747. }
  748. else
  749. {
  750. MessageBox.Show("Port is not set");
  751. // ret = true;
  752. }
  753. }
  754. }
  755. catch
  756. {
  757. MessageBox.Show("Port Open Faile" + Serial_Name);
  758. }
  759. // return ret;
  760. }
  761. public static DialogResult InputBox(string title, string promptText, ref string value)
  762. {
  763. Form form = new Form();
  764. System.Windows.Forms.Label label = new System.Windows.Forms.Label();
  765. System.Windows.Forms.TextBox textBox = new System.Windows.Forms.TextBox();
  766. System.Windows.Forms.Button buttonOk = new System.Windows.Forms.Button();
  767. System.Windows.Forms.Button buttonCancel = new System.Windows.Forms.Button();
  768. form.Text = title;
  769. label.Text = promptText;
  770. textBox.Text = value;
  771. buttonOk.Text = "OK";
  772. buttonCancel.Text = "Cancel";
  773. buttonOk.DialogResult = DialogResult.OK;
  774. buttonCancel.DialogResult = DialogResult.Cancel;
  775. label.SetBounds(9, 20, 372, 13);
  776. textBox.SetBounds(12, 36, 372, 20);
  777. buttonOk.SetBounds(228, 72, 75, 23);
  778. buttonCancel.SetBounds(309, 72, 75, 23);
  779. label.AutoSize = true;
  780. textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
  781. buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  782. buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  783. form.ClientSize = new Size(396, 107);
  784. form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
  785. form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
  786. form.FormBorderStyle = FormBorderStyle.FixedDialog;
  787. form.StartPosition = FormStartPosition.CenterScreen;
  788. form.MinimizeBox = false;
  789. form.MaximizeBox = false;
  790. form.AcceptButton = buttonOk;
  791. form.CancelButton = buttonCancel;
  792. DialogResult dialogResult = form.ShowDialog();
  793. value = textBox.Text;
  794. return dialogResult;
  795. }
  796. private void button_Clear_Click(object sender, EventArgs e)
  797. {
  798. System.Windows.Forms.RichTextBox tbReceived = null;
  799. if (panel_GpsTest.Visible == true)
  800. {
  801. tbReceived = richTextBox_Gps;
  802. }
  803. else if (panel_Catm1Test.Visible == true)
  804. {
  805. tbReceived = richTextBox_catm1;
  806. }
  807. else if (panel_SensorTest.Visible == true)
  808. {
  809. tbReceived = richTextBox_Sensor;
  810. }
  811. tbReceived.Text = "";
  812. }
  813. string FileSaveSet = null;
  814. private void button_GpsTestSave_Click(object sender, EventArgs e)
  815. {
  816. string CheckProductionNum = null;
  817. try
  818. {
  819. CheckProductionNum = dataGridView_Gps.Rows[0].Cells[0].Value.ToString();
  820. }
  821. catch
  822. {
  823. }
  824. try
  825. {
  826. if (!CheckProductionNum.Contains("BL-VITE00-V"))
  827. {
  828. MessageBox.Show("잘못된 생산 번호 입니다.");
  829. return;
  830. }
  831. }catch
  832. {
  833. MessageBox.Show("생산번호를 적어주세요.");
  834. }
  835. string Ref_Xls_File = new System.IO.DirectoryInfo(System.Windows.Forms.Application.StartupPath).ToString();
  836. string Dst_Xls_File = null;
  837. Ref_Xls_File = Ref_Xls_File + @"\APL_200221.xls";
  838. FileInfo SrcfileInfo = new FileInfo(Ref_Xls_File);
  839. FileInfo DstfileInfo;
  840. if (dataGridView_Gps.Rows[0].Cells[0].Value != null
  841. && dataGridView_Gps.Rows[0].Cells[1].Value != null)
  842. {
  843. if (SrcfileInfo.Exists)
  844. {
  845. if (FileSaveSet == null)
  846. {
  847. button_Path_Click(null, null);
  848. return;
  849. // MessageBox.Show("레퍼런스 엑셀 파일이 존재하지 않습니다. \r\n강차장님에게 문의 하세요.");
  850. }
  851. else
  852. {
  853. // File Copy 후 Data 입력
  854. Dst_Xls_File = FileSaveSet + @"\" + dataGridView_Gps.Rows[0].Cells[0].Value.ToString() + @".xls";
  855. DstfileInfo = new FileInfo(Dst_Xls_File);
  856. if (DstfileInfo.Exists)
  857. {
  858. if (MessageBox.Show("이미 파일이 있습니다.\r\n 덮어씌우겠습니까?", "YesOrNo", MessageBoxButtons.YesNo) == DialogResult.Yes)
  859. {
  860. try
  861. {
  862. // File.Copy(Ref_Xls_File, Dst_Xls_File, true);
  863. }
  864. catch (Exception ex)
  865. {
  866. MessageBox.Show(ex.StackTrace);
  867. return;
  868. }
  869. }
  870. else
  871. {
  872. //MessageBox.Show("아니요 클릭");
  873. return; // 아니오시 Return;
  874. }
  875. }
  876. else
  877. {
  878. File.Copy(Ref_Xls_File, Dst_Xls_File);
  879. }
  880. }
  881. }
  882. else
  883. {
  884. MessageBox.Show("레퍼런스 엑셀 파일이 존재하지 않습니다. \r\n강차장님에게 문의 하세요.");
  885. return;
  886. }
  887. }
  888. else
  889. {
  890. MessageBox.Show("생산번호/GPS SNR 값이 제대로 입력 되지 않았습니다.\r\n확인후 다시 저장하세요.");
  891. return;
  892. }
  893. Gps_DataxlsWrite(Dst_Xls_File);
  894. dataGridView_Gps.Rows[0].Cells[0].Value = null;
  895. dataGridView_Gps.Rows[0].Cells[1].Value = null;
  896. dataGridView_Gps.Refresh();
  897. // if (!di.Exists) { di.Create(); }
  898. //string tmppath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  899. #if false
  900. if (dataGridView_Gps.Rows[0].Cells[1].Value != null)
  901. {
  902. string path = @"xxxx.txt";
  903. FileInfo file = new FileInfo(path);
  904. if (file.Exists)
  905. { //파일이 있는지
  906. //복사(경로,덮어쓰기 옵션 기본값 false)
  907. //만약 덮어쓰기 false일 때 파일이 존재하면 에러남.
  908. file.CopyTo(@"xxx2.txt", true);
  909. }
  910. }
  911. #endif
  912. }
  913. private void button_SensorTestSave()
  914. {
  915. string CheckProductionNum = null;
  916. try
  917. {
  918. CheckProductionNum = dataGridView_Sensor.Rows[0].Cells[0].Value.ToString();
  919. }
  920. catch
  921. {
  922. }
  923. try
  924. {
  925. if (!CheckProductionNum.Contains("BL-VITE00-V"))
  926. {
  927. MessageBox.Show("잘못된 생산 번호 입니다.");
  928. return;
  929. }
  930. }
  931. catch
  932. {
  933. MessageBox.Show("생산번호를 적어주세요.");
  934. }
  935. string Ref_Xls_File = new System.IO.DirectoryInfo(System.Windows.Forms.Application.StartupPath).ToString();
  936. string Dst_Xls_File = null;
  937. Ref_Xls_File = Ref_Xls_File + @"\APL_200221.xls";
  938. FileInfo SrcfileInfo = new FileInfo(Ref_Xls_File);
  939. FileInfo DstfileInfo;
  940. if (dataGridView_Sensor.Rows[0].Cells[0].Value != null)
  941. {
  942. if (SrcfileInfo.Exists)
  943. {
  944. if (FileSaveSet == null)
  945. {
  946. button_Path_Click(null, null);
  947. return;
  948. // MessageBox.Show("레퍼런스 엑셀 파일이 존재하지 않습니다. \r\n강차장님에게 문의 하세요.");
  949. }
  950. else
  951. {
  952. // File Copy 후 Data 입력
  953. Dst_Xls_File = FileSaveSet + @"\" + dataGridView_Sensor.Rows[0].Cells[0].Value.ToString() + @".xls";
  954. DstfileInfo = new FileInfo(Dst_Xls_File);
  955. if (DstfileInfo.Exists)
  956. {
  957. if (MessageBox.Show("이미 파일이 있습니다.\r\n 덮어씌우겠습니까?", "YesOrNo", MessageBoxButtons.YesNo) == DialogResult.Yes)
  958. {
  959. try
  960. {
  961. // File.Copy(Ref_Xls_File, Dst_Xls_File, true);
  962. }
  963. catch (Exception ex)
  964. {
  965. MessageBox.Show(ex.StackTrace);
  966. return;
  967. }
  968. }
  969. else
  970. {
  971. //MessageBox.Show("아니요 클릭");
  972. return; // 아니오시 Return;
  973. }
  974. }
  975. else
  976. {
  977. File.Copy(Ref_Xls_File, Dst_Xls_File);
  978. }
  979. }
  980. }
  981. else
  982. {
  983. MessageBox.Show("레퍼런스 엑셀 파일이 존재하지 않습니다. \r\n강차장님에게 문의 하세요.");
  984. return;
  985. }
  986. }
  987. else
  988. {
  989. MessageBox.Show("생산번호 값이 제대로 입력 되지 않았습니다.\r\n확인후 다시 저장하세요.");
  990. return;
  991. }
  992. Sensor_DataxlsWrite(Dst_Xls_File);
  993. // dataGridView_Sensor.Rows[0].Cells[0].Value = null;
  994. // dataGridView_Sensor.Rows[0].Cells[1].Value = null;
  995. // if (!di.Exists) { di.Create(); }
  996. //string tmppath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  997. #if false
  998. if (dataGridView_Gps.Rows[0].Cells[1].Value != null)
  999. {
  1000. string path = @"xxxx.txt";
  1001. FileInfo file = new FileInfo(path);
  1002. if (file.Exists)
  1003. { //파일이 있는지
  1004. //복사(경로,덮어쓰기 옵션 기본값 false)
  1005. //만약 덮어쓰기 false일 때 파일이 존재하면 에러남.
  1006. file.CopyTo(@"xxx2.txt", true);
  1007. }
  1008. }
  1009. #endif
  1010. }
  1011. Excel.Application ExcelApp = null;
  1012. Excel.Workbook wb = null;
  1013. Excel.Worksheet ws = null;
  1014. private void Gps_DataxlsWrite(string dst)
  1015. {
  1016. //Write
  1017. //파일이 존재 한다면..삭제 하고..
  1018. /*if (File.Exists("C:\\Test.xls"))
  1019. {
  1020. File.Delete("C:\\Test.xls");
  1021. }*/
  1022. try
  1023. {
  1024. ExcelApp = new Excel.Application();
  1025. wb = ExcelApp.Workbooks.Open(dst, 0, false, 5, Missing.Value, Missing.Value, false, Missing.Value,
  1026. Missing.Value, true, false, Missing.Value, false, false, false);
  1027. ws = wb.Worksheets["Sheet1"] as Excel.Worksheet;
  1028. //엑셀 시트 인덱스 번호는 0,0 부터 시작 하는 것이 아니라 1,1 A1 부터 시작 함. 0,0 으로 시작하면 오류...
  1029. //시트에 값 쓰기...
  1030. ws.Cells[8, 9] = dataGridView_Gps.Rows[0].Cells[0].Value;
  1031. ws.Cells[8, 11] = dataGridView_Gps.Rows[0].Cells[1].Value;
  1032. /*ws.Cells[1, 3] = "123";
  1033. ws.Cells[1, 4] = "1234";
  1034. ws.Cells[2, 1] = "Test1"; //A2
  1035. ws.Cells[3, 1] = "Test12"; //A3
  1036. ws.Cells[4, 1] = "Test123"; //A4*/
  1037. //다른 이름으로 저장하기...
  1038. wb.Save(); // => 오픈한 파일 그대로 저장...
  1039. /* wb.SaveAs("C:\\Test.xls",
  1040. Excel.XlFileFormat.xlWorkbookNormal,
  1041. Type.Missing,
  1042. Type.Missing,
  1043. Type.Missing,
  1044. Type.Missing,
  1045. Excel.XlSaveAsAccessMode.xlNoChange,
  1046. Type.Missing,
  1047. Type.Missing,
  1048. Type.Missing,
  1049. Type.Missing,
  1050. Type.Missing);
  1051. */
  1052. //파일 닫기...
  1053. wb.Close(false, Type.Missing, Type.Missing);
  1054. wb = null;
  1055. ExcelApp.Quit();
  1056. }
  1057. catch (Exception ex)
  1058. {
  1059. //객체들 메모리 해제
  1060. ReleaseExcelObject(ws);
  1061. ReleaseExcelObject(wb);
  1062. ReleaseExcelObject(ExcelApp);
  1063. GC.Collect();
  1064. }
  1065. finally
  1066. {
  1067. //객체들 메모리 해제
  1068. ReleaseExcelObject(ws);
  1069. ReleaseExcelObject(wb);
  1070. ReleaseExcelObject(ExcelApp);
  1071. GC.Collect();
  1072. }
  1073. }
  1074. private void Sensor_DataxlsWrite(string dst)
  1075. {
  1076. //Write
  1077. //파일이 존재 한다면..삭제 하고..
  1078. /*if (File.Exists("C:\\Test.xls"))
  1079. {
  1080. File.Delete("C:\\Test.xls");
  1081. }*/
  1082. try
  1083. {
  1084. ExcelApp = new Excel.Application();
  1085. wb = ExcelApp.Workbooks.Open(dst, 0, false, 5, Missing.Value, Missing.Value, false, Missing.Value,
  1086. Missing.Value, true, false, Missing.Value, false, false, false);
  1087. ws = wb.Worksheets["Sheet1"] as Excel.Worksheet;
  1088. //엑셀 시트 인덱스 번호는 0,0 부터 시작 하는 것이 아니라 1,1 A1 부터 시작 함. 0,0 으로 시작하면 오류...
  1089. //시트에 값 쓰기...
  1090. ws.Cells[8, 9] = dataGridView_Sensor.Rows[0].Cells[0].Value; // 제조 번호
  1091. ws.Cells[8, 4] = dataGridView_Sensor.Rows[0].Cells[1].Value; // 일련번호
  1092. ws.Cells[8, 6] = dataGridView_Sensor.Rows[0].Cells[2].Value; //IMEI
  1093. ws.Cells[8, 7] = dataGridView_Sensor.Rows[0].Cells[3].Value;// PHONE
  1094. ws.Cells[8, 8] = dataGridView_Sensor.Rows[0].Cells[4].Value;//USIM
  1095. ws.Cells[8, 10] = dataGridView_Sensor.Rows[0].Cells[5].Value;//VER
  1096. ws.Cells[8, 13] = dataGridView_Sensor.Rows[0].Cells[6].Value;//BATT
  1097. ws.Cells[8, 14] = dataGridView_Sensor.Rows[0].Cells[7].Value;//SYS
  1098. ws.Cells[8, 15] = dataGridView_Sensor.Rows[0].Cells[8].Value;//TEMP
  1099. ws.Cells[8, 16] = dataGridView_Sensor.Rows[0].Cells[9].Value;//BMA
  1100. ws.Cells[8, 17] = dataGridView_Sensor.Rows[0].Cells[10].Value;//BMA
  1101. ws.Cells[8, 18] = dataGridView_Sensor.Rows[0].Cells[11].Value;//BMA
  1102. /*ws.Cells[1, 3] = "123";
  1103. ws.Cells[1, 4] = "1234";
  1104. ws.Cells[2, 1] = "Test1"; //A2
  1105. ws.Cells[3, 1] = "Test12"; //A3
  1106. ws.Cells[4, 1] = "Test123"; //A4*/
  1107. //다른 이름으로 저장하기...
  1108. wb.Save(); // => 오픈한 파일 그대로 저장...
  1109. /* wb.SaveAs("C:\\Test.xls",
  1110. Excel.XlFileFormat.xlWorkbookNormal,
  1111. Type.Missing,
  1112. Type.Missing,
  1113. Type.Missing,
  1114. Type.Missing,
  1115. Excel.XlSaveAsAccessMode.xlNoChange,
  1116. Type.Missing,
  1117. Type.Missing,
  1118. Type.Missing,
  1119. Type.Missing,
  1120. Type.Missing);
  1121. */
  1122. //파일 닫기...
  1123. wb.Close(false, Type.Missing, Type.Missing);
  1124. wb = null;
  1125. ExcelApp.Quit();
  1126. }
  1127. catch (Exception ex)
  1128. {
  1129. //객체들 메모리 해제
  1130. ReleaseExcelObject(ws);
  1131. ReleaseExcelObject(wb);
  1132. ReleaseExcelObject(ExcelApp);
  1133. GC.Collect();
  1134. }
  1135. finally
  1136. {
  1137. //객체들 메모리 해제
  1138. ReleaseExcelObject(ws);
  1139. ReleaseExcelObject(wb);
  1140. ReleaseExcelObject(ExcelApp);
  1141. GC.Collect();
  1142. }
  1143. }
  1144. private void ReleaseExcelObject(object obj)
  1145. {
  1146. try
  1147. {
  1148. if (obj != null)
  1149. {
  1150. Marshal.ReleaseComObject(obj);
  1151. obj = null;
  1152. }
  1153. }
  1154. catch (Exception ex)
  1155. {
  1156. obj = null;
  1157. throw ex;
  1158. }
  1159. finally
  1160. {
  1161. GC.Collect();
  1162. }
  1163. }
  1164. // CommonOpenFileDialog 클래스 생성
  1165. CommonOpenFileDialog dialog = new CommonOpenFileDialog();
  1166. private void button_Path_Click(object sender, EventArgs e)
  1167. {
  1168. // 처음 보여줄 폴더 설정(안해도 됨)
  1169. //dialog.InitialDirectory = "";
  1170. dialog.IsFolderPicker = true;
  1171. if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
  1172. {
  1173. FileSaveSet = dialog.FileName;
  1174. // label.Text = dialog.FileName;
  1175. // 테스트용, 폴더 선택이 완료되면 선택된 폴더를 label에 출력
  1176. }
  1177. }
  1178. private void dataGridView_Gps_KeyDown(object sender, KeyEventArgs e)
  1179. {
  1180. if (e.KeyCode == Keys.Enter)
  1181. {
  1182. button_GpsTestSave_Click(null,null);
  1183. //to do
  1184. }
  1185. else
  1186. {
  1187. return;
  1188. }
  1189. }
  1190. private void button_PrevFile_Click(object sender, EventArgs e)
  1191. {
  1192. if (openFileDialog.ShowDialog() == DialogResult.OK)
  1193. {
  1194. foreach (string x in openFileDialog.FileNames)
  1195. {
  1196. listBox_PreviousFile.Items.Add(x);
  1197. }
  1198. }
  1199. }
  1200. private void button_AfterFile_Click(object sender, EventArgs e)
  1201. {
  1202. if (openFileDialog.ShowDialog() == DialogResult.OK)
  1203. {
  1204. foreach (string x in openFileDialog.FileNames)
  1205. {
  1206. listBox_AfterFile.Items.Add(x);
  1207. }
  1208. }
  1209. }
  1210. private void label_Merge_Click(object sender, EventArgs e)
  1211. {
  1212. panel_GpsTest.Visible = false;
  1213. panel_SensorTest.Visible = false;
  1214. panel_Catm1Test.Visible = false;
  1215. panel_MergeMain.Visible = true;
  1216. panel_SnCheckMain.Visible = false;
  1217. }
  1218. private void label_SnCheck_Click(object sender, EventArgs e)
  1219. {
  1220. panel_GpsTest.Visible = false;
  1221. panel_SensorTest.Visible = false;
  1222. panel_Catm1Test.Visible = false;
  1223. panel_MergeMain.Visible = false;
  1224. panel_SnCheckMain.Visible = true;
  1225. }
  1226. private void button_FileFindLoad_Click(object sender, EventArgs e)
  1227. {
  1228. if (openFileDialog.ShowDialog() == DialogResult.OK)
  1229. {
  1230. /*foreach (string x in openFileDialog.FileNames)
  1231. {
  1232. listBox_Previous.Items.Add(x);
  1233. }*/
  1234. }
  1235. }
  1236. private void button_Merge_Click(object sender, EventArgs e)
  1237. {
  1238. OpenExcelFile(listBox_PreviousFile.Items[0].ToString());
  1239. }
  1240. #region
  1241. /// <summary>
  1242. /// 엑셀 정식 파일 형식이 아닌 파일 읽기
  1243. /// </summary>
  1244. /// <param name="FileName">파일명.확장자</param>
  1245. /// <returns></returns>
  1246. public void OpenExcelFile(string FileName)
  1247. {
  1248. Excel.Application excelApp = new Excel.Application();
  1249. Excel.Workbook workbook;
  1250. Excel.Worksheet worksheet;
  1251. Excel.Range range;
  1252. workbook = excelApp.Workbooks.Open(FileName); //Environment.CurrentDirectory+ "\\Excel.xlsx");
  1253. worksheet = (Excel.Worksheet)workbook.Sheets["Sheet1"];
  1254. try
  1255. {
  1256. //MessageWrite(LogTraceLevel.Info, "OpenExcelFile 함수가 시작 되었습니다.", exception);
  1257. int column = 0;
  1258. int row = 0;
  1259. string sTmpColumnName = "SKT 일련번호";
  1260. string[] colHeader = sTmpColumnName.Split(',');
  1261. range = worksheet.UsedRange;
  1262. String data = "";
  1263. data += ((range.Cells[4, 5] as Excel.Range).Value2.ToString() + " ");
  1264. /* for (int i = 1; i <= range.Rows.Count; ++i)
  1265. {
  1266. for (int j = 1; j <= range.Columns.Count; ++j)
  1267. {
  1268. data += ((range.Cells[i, j] as Excel.Range).Value2.ToString() + " ");
  1269. }
  1270. data += "\n";
  1271. }*/
  1272. workbook.Close(false, Type.Missing, Type.Missing);
  1273. excelApp.Quit();
  1274. //MessageBox.Show("OpenExcelFile 함수가 종료 되었습니다.");
  1275. }
  1276. catch (Exception ex)
  1277. {
  1278. MessageBox.Show("OpenExcelFile오류가 발생되었습니다.[{0}]");
  1279. excelApp.Quit();
  1280. //throw new Exception(ex.Message);
  1281. }
  1282. }
  1283. #endregion
  1284. }
  1285. }