Main.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO.Ports;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace APL_TestCheck
  13. {
  14. public partial class Main : Form
  15. {
  16. public string Serial_Name { get => serialPort.PortName; set => serialPort.PortName = value; }
  17. public Main()
  18. {
  19. InitializeComponent();
  20. }
  21. private void panel_PanelMode_MouseLeave(object sender, EventArgs e)
  22. {
  23. System.Windows.Forms.Panel Panel = (System.Windows.Forms.Panel)sender;
  24. Panel.BackColor = Color.FromArgb(0, 159, 200);
  25. label_GPSTest_MouseLeave(null, null);
  26. }
  27. private void panel_PanelMode_MouseMove(object sender, MouseEventArgs e)
  28. {
  29. System.Windows.Forms.Panel Panel = (System.Windows.Forms.Panel)sender;
  30. Panel.BackColor = Color.FromArgb(0, 159, 255);
  31. }
  32. private void label_GPSTest_MouseLeave(object sender, EventArgs e)
  33. {
  34. if (panel_GpsTest.Visible == true)
  35. {
  36. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  37. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  38. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  39. }
  40. else if(panel_Catm1Test.Visible == true)
  41. {
  42. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  43. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  44. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  45. }
  46. else if (panel_SensorTest.Visible == true)
  47. {
  48. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  49. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  50. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  51. }
  52. }
  53. private void label_Catm1Test_MouseLeave(object sender, EventArgs e)
  54. {
  55. if (panel_GpsTest.Visible == true)
  56. {
  57. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  58. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  59. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  60. }
  61. else if (panel_Catm1Test.Visible == true)
  62. {
  63. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  64. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  65. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  66. }
  67. else if (panel_SensorTest.Visible == true)
  68. {
  69. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  70. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  71. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  72. }
  73. }
  74. private void label_SensorTest_MouseLeave(object sender, EventArgs e)
  75. {
  76. if (panel_GpsTest.Visible == true)
  77. {
  78. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  79. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  80. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  81. }
  82. else if (panel_Catm1Test.Visible == true)
  83. {
  84. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  85. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  86. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  87. }
  88. else if (panel_SensorTest.Visible == true)
  89. {
  90. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  91. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  92. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  93. }
  94. }
  95. private void label_Catm1Test_MouseMove(object sender, MouseEventArgs e)
  96. {
  97. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  98. }
  99. private void label_SensorTest_MouseMove(object sender, MouseEventArgs e)
  100. {
  101. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  102. }
  103. private void label_GPSTest_MouseMove(object sender, MouseEventArgs e)
  104. {
  105. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  106. }
  107. public struct POINT { public int X, Y; }
  108. [DllImport("user32.dll")] // 현재 마우스 위치를 얻기위한 API함수.
  109. public extern static void GetCursorPos(out POINT point);
  110. Point FormLocation; // 현재 폼 위치
  111. POINT LastLocation = new POINT(); // 방금 전의 마우스 위치
  112. POINT CurrentLocation = new POINT(); // 현재 마우스 위치
  113. // 폼이 움직일 양 = CurrentLocation - LastLocation.
  114. bool IsMouseMoveStart = false; // 현재 마우스 움직이기 기능이 켜져있는가.
  115. // 만약 이게 없으면 그냥 폼위에서
  116. private void panel_Main_MouseDown(object sender, MouseEventArgs e)
  117. {
  118. GetCursorPos(out CurrentLocation);
  119. FormLocation = this.Location;
  120. IsMouseMoveStart = true;
  121. }
  122. private void panel_Main_MouseMove(object sender, MouseEventArgs e)
  123. {
  124. if (!IsMouseMoveStart) return;
  125. GetCursorPos(out LastLocation);
  126. FormLocation.X -= (CurrentLocation.X - LastLocation.X);
  127. FormLocation.Y -= (CurrentLocation.Y - LastLocation.Y);
  128. this.Location = FormLocation;
  129. CurrentLocation = LastLocation;
  130. }
  131. private void panel_Main_MouseUp(object sender, MouseEventArgs e)
  132. {
  133. IsMouseMoveStart = false;
  134. }
  135. private void pictureBox_min_red_Click(object sender, EventArgs e)
  136. {
  137. this.WindowState = FormWindowState.Minimized;
  138. }
  139. private void pictureBox_X_red_Click(object sender, EventArgs e)
  140. {
  141. Application.Exit();
  142. }
  143. private void label_GPSTest_Click(object sender, EventArgs e)
  144. {
  145. panel_GpsTest.Visible = true;
  146. panel_SensorTest.Visible = false;
  147. panel_Catm1Test.Visible = false;
  148. }
  149. private void label_Catm1Test_Click(object sender, EventArgs e)
  150. {
  151. panel_GpsTest.Visible = false;
  152. panel_SensorTest.Visible = false;
  153. panel_Catm1Test.Visible = true;
  154. }
  155. private void label_SensorTest_Click(object sender, EventArgs e)
  156. {
  157. panel_GpsTest.Visible = false;
  158. panel_SensorTest.Visible = true;
  159. panel_Catm1Test.Visible = false;
  160. }
  161. private void Main_Load(object sender, EventArgs e)
  162. {
  163. Serial_Initialize(ref comboBox_Port);
  164. panel_GpsTest.Visible = true;
  165. panel_SensorTest.Visible = false;
  166. panel_Catm1Test.Visible = false;
  167. pictureBox_Check1.Visible = false;
  168. pictureBox_Check2.Visible = false;
  169. pictureBox_Check3.Visible = false;
  170. pictureBox_Check4.Visible = false;
  171. }
  172. public void Serial_Initialize(ref ComboBox cb_port)
  173. {
  174. this.serialPort = new System.IO.Ports.SerialPort();
  175. this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.Serial_DataRecvFunction);
  176. cb_port.BeginUpdate();
  177. foreach (string comport in SerialPort.GetPortNames())//foreach (string comport in SerialPort_TestProgram.GetPortNames())
  178. {
  179. cb_port.Items.Add(comport);
  180. }
  181. cb_port.EndUpdate();
  182. //SerialPort 초기 설정.
  183. // serialPort.Encoding = Encoding.GetEncoding("Windows-1252");
  184. cb_port.DataSource = SerialPort.GetPortNames();
  185. try
  186. {
  187. serialPort.PortName = Serial_Name = cb_port.SelectedItem.ToString();
  188. serialPort.BaudRate = (int)115200;
  189. serialPort.DataBits = (int)8;
  190. serialPort.Parity = System.IO.Ports.Parity.None;
  191. serialPort.StopBits = StopBits.One;
  192. }
  193. catch { }
  194. }
  195. private delegate void StringSend(string Text);
  196. public void Serial_DataRecvFunction(object sender, SerialDataReceivedEventArgs e)
  197. {
  198. int nLnegth = serialPort.BytesToRead;
  199. byte[] btdata = new byte[nLnegth];
  200. serialPort.Read(btdata, 0, nLnegth);
  201. string data = Encoding.Default.GetString(btdata);
  202. if (checkBox_TerminalStop.Checked == true)
  203. return;
  204. // string data = serialPort.ReadExisting();
  205. this.Invoke(new StringSend(Data_Recv_Str), data);
  206. /****
  207. *메모리 누수 방지용 코드
  208. */
  209. System.GC.Collect(0, GCCollectionMode.Forced);
  210. System.GC.WaitForFullGCComplete();
  211. }
  212. private void label_Port_MouseClick(object sender, MouseEventArgs e)
  213. {
  214. }
  215. int LineLimit = 500;
  216. [DllImport("user32.dll")]
  217. public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
  218. private const int WM_SETREDRAW = 11;
  219. public void Data_Recv_Str(string text)
  220. {
  221. System.Windows.Forms.RichTextBox tbReceived = null;
  222. if (panel_GpsTest.Visible == true) {
  223. tbReceived = richTextBox_Gps;
  224. }
  225. else if (panel_Catm1Test.Visible == true) {
  226. tbReceived = richTextBox_catm1;
  227. }
  228. else if (panel_SensorTest.Visible == true) {
  229. tbReceived = richTextBox_Sensor;
  230. }
  231. int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
  232. try
  233. {
  234. try
  235. {
  236. SendMessage(this.Handle, WM_SETREDRAW, false, 0);
  237. }
  238. catch (Exception e) { MessageBox.Show(e.StackTrace); }
  239. if (tbReceived.Lines.Length > nLimitLines)
  240. {
  241. LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
  242. while ((tempLines.Count - nLimitLines) > 0)
  243. {
  244. tempLines.RemoveFirst();
  245. }
  246. tbReceived.Lines = tempLines.ToArray();
  247. }
  248. try
  249. {
  250. SendMessage(this.Handle, WM_SETREDRAW, true, 0);
  251. }
  252. catch { return; }
  253. if (checkBox_GPGSV.Checked == true && panel_GpsTest.Visible == true)
  254. {
  255. tbReceived.AppendText(GPS_TestFunc(text));
  256. }
  257. else
  258. {
  259. tbReceived.AppendText(text);
  260. }
  261. tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택...
  262. tbReceived.ScrollToCaret();
  263. }
  264. catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
  265. }
  266. int GPS_TestSnRValue = 0;
  267. int Gps_CurrentCount = 0;
  268. int TestVal = 0;
  269. private string GPS_TestFunc(string data)
  270. {
  271. int Gps_Count, Sentence,SnrIndex = 3;
  272. // 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";
  273. // string tmpstr1 = "$GPGSV,2,2,08,24,36,052,37,25,48,159,42,31,17,245,,32,46,312,45*74\r\n";
  274. string str = data;
  275. string[] sp = str.Split(',');
  276. int location1, location2,LastLocation ;
  277. try
  278. {
  279. /*
  280. if(TestVal == 0)
  281. {
  282. str = tmpstr;
  283. TestVal++;
  284. }
  285. else
  286. {
  287. str = tmpstr1;
  288. TestVal = 0;
  289. }
  290. */
  291. location1 = str.IndexOf("$GPGSV");
  292. if (location1 != -1)
  293. {
  294. str = str.Substring(location1);
  295. location2 = str.IndexOf("\r\n");
  296. str = str.Substring(0, location2 + 2);
  297. sp = str.Split(',');
  298. if (sp[3].Contains("*"))
  299. {
  300. sp[3] = sp[3].Substring(0, 2);
  301. dataGridView_Gps.Rows[0].Cells[1].Value = sp[3];
  302. return str;
  303. }
  304. else
  305. {
  306. Sentence = Convert.ToInt32(sp[2]);
  307. if(Sentence == 1)
  308. {
  309. GPS_TestSnRValue = 0;
  310. Gps_CurrentCount = 0;
  311. }
  312. Gps_Count = Convert.ToInt32(sp[3]);
  313. for (int i = 0; i < 4; i++)
  314. {
  315. SnrIndex += 4;
  316. if (sp[SnrIndex].Contains("*"))
  317. {
  318. LastLocation = sp[SnrIndex].IndexOf("*") - 2;
  319. sp[SnrIndex] = sp[SnrIndex].Substring(0,2);
  320. GPS_TestSnRValue += Convert.ToInt32(sp[SnrIndex]);
  321. }
  322. else
  323. {
  324. if (sp[SnrIndex] == "")
  325. sp[SnrIndex] = "0";
  326. GPS_TestSnRValue += Convert.ToInt32(sp[SnrIndex]);
  327. }
  328. Gps_CurrentCount++;
  329. if (Gps_CurrentCount == Gps_Count)
  330. {
  331. GPS_TestSnRValue /= Gps_Count;
  332. dataGridView_Gps.Rows[0].Cells[1].Value = GPS_TestSnRValue.ToString();
  333. break;
  334. }
  335. }
  336. }
  337. return str;
  338. }
  339. return "";
  340. }
  341. catch (Exception e) { MessageBox.Show(e.StackTrace); }
  342. return "";
  343. }
  344. private void label_Port_MouseLeave(object sender, EventArgs e)
  345. {
  346. panel_Port.BackColor = Color.FromArgb(0, 159, 200);
  347. }
  348. private void label_Port_MouseMove(object sender, MouseEventArgs e)
  349. {
  350. panel_Port.BackColor = Color.FromArgb(0, 159, 255);
  351. }
  352. private void label_Port_Click(object sender, EventArgs e)
  353. {
  354. // Boolean ret = false;
  355. try
  356. {
  357. if (serialPort.IsOpen) // 이미 포트가 열려 있을 때
  358. { //When the port is open
  359. serialPort.Close();
  360. label_Port.Text = "Port Open";
  361. //ret = true;
  362. }
  363. else//When the port is close //포트가 열려 있지 않을 때
  364. {
  365. if (comboBox_Port.Text != "")
  366. {
  367. serialPort.PortName = Serial_Name = comboBox_Port.SelectedItem.ToString();
  368. serialPort.BaudRate = Convert.ToInt32(comboBox_bps.Text);
  369. serialPort.Open();
  370. label_Port.Text = "Port Close";
  371. // Debug.Debug_Main_Form_Get(this.main_form);
  372. }
  373. else
  374. {
  375. MessageBox.Show("Port is not set");
  376. // ret = true;
  377. }
  378. }
  379. }
  380. catch
  381. {
  382. MessageBox.Show("Port Open Faile" + Serial_Name);
  383. }
  384. // return ret;
  385. }
  386. public static DialogResult InputBox(string title, string promptText, ref string value)
  387. {
  388. Form form = new Form();
  389. Label label = new Label();
  390. TextBox textBox = new TextBox();
  391. Button buttonOk = new Button();
  392. Button buttonCancel = new Button();
  393. form.Text = title;
  394. label.Text = promptText;
  395. textBox.Text = value;
  396. buttonOk.Text = "OK";
  397. buttonCancel.Text = "Cancel";
  398. buttonOk.DialogResult = DialogResult.OK;
  399. buttonCancel.DialogResult = DialogResult.Cancel;
  400. label.SetBounds(9, 20, 372, 13);
  401. textBox.SetBounds(12, 36, 372, 20);
  402. buttonOk.SetBounds(228, 72, 75, 23);
  403. buttonCancel.SetBounds(309, 72, 75, 23);
  404. label.AutoSize = true;
  405. textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
  406. buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  407. buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  408. form.ClientSize = new Size(396, 107);
  409. form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
  410. form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
  411. form.FormBorderStyle = FormBorderStyle.FixedDialog;
  412. form.StartPosition = FormStartPosition.CenterScreen;
  413. form.MinimizeBox = false;
  414. form.MaximizeBox = false;
  415. form.AcceptButton = buttonOk;
  416. form.CancelButton = buttonCancel;
  417. DialogResult dialogResult = form.ShowDialog();
  418. value = textBox.Text;
  419. return dialogResult;
  420. }
  421. private void dataGridView_Sensor_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  422. {
  423. string value = "";
  424. if (checkBox_Edit.Checked == false)
  425. {
  426. if (InputBox("번호 입력", "일련 번호:", ref value) == DialogResult.OK)
  427. {
  428. Name = value;
  429. }
  430. }
  431. }
  432. private void dataGridView_Catm1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  433. {
  434. string value = "";
  435. if (checkBox_Edit.Checked == false)
  436. {
  437. if (InputBox("번호 입력", "일련 번호:", ref value) == DialogResult.OK)
  438. {
  439. Name = value;
  440. }
  441. }
  442. }
  443. private void dataGridView_Gps_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  444. {
  445. string value = "";
  446. if (checkBox_Edit.Checked == false)
  447. {
  448. if (InputBox("번호 입력", "일련 번호:", ref value) == DialogResult.OK)
  449. {
  450. Name = value;
  451. }
  452. }
  453. }
  454. private void button_Clear_Click(object sender, EventArgs e)
  455. {
  456. System.Windows.Forms.RichTextBox tbReceived = null;
  457. if (panel_GpsTest.Visible == true)
  458. {
  459. tbReceived = richTextBox_Gps;
  460. }
  461. else if (panel_Catm1Test.Visible == true)
  462. {
  463. tbReceived = richTextBox_catm1;
  464. }
  465. else if (panel_SensorTest.Visible == true)
  466. {
  467. tbReceived = richTextBox_Sensor;
  468. }
  469. tbReceived.Text = "";
  470. }
  471. }
  472. }