Main.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. //tbReceived.AppendText(text);
  254. tbReceived.AppendText(GPS_TestFunc(text));
  255. tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택...
  256. tbReceived.ScrollToCaret();
  257. }
  258. catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
  259. }
  260. private string GPS_TestFunc(string data)
  261. {
  262. string str = data;
  263. string[] sp = str.Split(',');
  264. int location1, location2;
  265. try
  266. {
  267. location1 = str.IndexOf("$GPGSV");
  268. str = str.Substring(location1);
  269. location2 = str.IndexOf("\r\n");
  270. str = str.Substring(0,location2 + 2);
  271. return str;
  272. }
  273. catch
  274. {
  275. }
  276. return str;
  277. }
  278. private void label_Port_MouseLeave(object sender, EventArgs e)
  279. {
  280. panel_Port.BackColor = Color.FromArgb(0, 159, 200);
  281. }
  282. private void label_Port_MouseMove(object sender, MouseEventArgs e)
  283. {
  284. panel_Port.BackColor = Color.FromArgb(0, 159, 255);
  285. }
  286. private void label_Port_Click(object sender, EventArgs e)
  287. {
  288. // Boolean ret = false;
  289. try
  290. {
  291. if (serialPort.IsOpen) // 이미 포트가 열려 있을 때
  292. { //When the port is open
  293. serialPort.Close();
  294. label_Port.Text = "Port Open";
  295. //ret = true;
  296. }
  297. else//When the port is close //포트가 열려 있지 않을 때
  298. {
  299. if (comboBox_Port.Text != "")
  300. {
  301. serialPort.PortName = Serial_Name = comboBox_Port.SelectedItem.ToString();
  302. serialPort.BaudRate = Convert.ToInt32(comboBox_bps.Text);
  303. serialPort.Open();
  304. label_Port.Text = "Port Close";
  305. // Debug.Debug_Main_Form_Get(this.main_form);
  306. }
  307. else
  308. {
  309. MessageBox.Show("Port is not set");
  310. // ret = true;
  311. }
  312. }
  313. }
  314. catch
  315. {
  316. MessageBox.Show("Port Open Faile" + Serial_Name);
  317. }
  318. // return ret;
  319. }
  320. public static DialogResult InputBox(string title, string promptText, ref string value)
  321. {
  322. Form form = new Form();
  323. Label label = new Label();
  324. TextBox textBox = new TextBox();
  325. Button buttonOk = new Button();
  326. Button buttonCancel = new Button();
  327. form.Text = title;
  328. label.Text = promptText;
  329. textBox.Text = value;
  330. buttonOk.Text = "OK";
  331. buttonCancel.Text = "Cancel";
  332. buttonOk.DialogResult = DialogResult.OK;
  333. buttonCancel.DialogResult = DialogResult.Cancel;
  334. label.SetBounds(9, 20, 372, 13);
  335. textBox.SetBounds(12, 36, 372, 20);
  336. buttonOk.SetBounds(228, 72, 75, 23);
  337. buttonCancel.SetBounds(309, 72, 75, 23);
  338. label.AutoSize = true;
  339. textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
  340. buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  341. buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  342. form.ClientSize = new Size(396, 107);
  343. form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
  344. form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
  345. form.FormBorderStyle = FormBorderStyle.FixedDialog;
  346. form.StartPosition = FormStartPosition.CenterScreen;
  347. form.MinimizeBox = false;
  348. form.MaximizeBox = false;
  349. form.AcceptButton = buttonOk;
  350. form.CancelButton = buttonCancel;
  351. DialogResult dialogResult = form.ShowDialog();
  352. value = textBox.Text;
  353. return dialogResult;
  354. }
  355. private void dataGridView_Sensor_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  356. {
  357. string value = "0";
  358. if (checkBox_Edit.Checked == false)
  359. {
  360. if (InputBox("번호 입력", "일련 번호:", ref value) == DialogResult.OK)
  361. {
  362. Name = value;
  363. }
  364. }
  365. }
  366. private void dataGridView_Catm1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  367. {
  368. string value = "0";
  369. if (checkBox_Edit.Checked == false)
  370. {
  371. if (InputBox("번호 입력", "일련 번호:", ref value) == DialogResult.OK)
  372. {
  373. Name = value;
  374. }
  375. }
  376. }
  377. private void dataGridView_Gps_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  378. {
  379. string value = "0";
  380. if (checkBox_Edit.Checked == false)
  381. {
  382. if (InputBox("번호 입력", "일련 번호:", ref value) == DialogResult.OK)
  383. {
  384. Name = value;
  385. }
  386. }
  387. }
  388. private void button_Clear_Click(object sender, EventArgs e)
  389. {
  390. System.Windows.Forms.RichTextBox tbReceived = null;
  391. if (panel_GpsTest.Visible == true)
  392. {
  393. tbReceived = richTextBox_Gps;
  394. }
  395. else if (panel_Catm1Test.Visible == true)
  396. {
  397. tbReceived = richTextBox_catm1;
  398. }
  399. else if (panel_SensorTest.Visible == true)
  400. {
  401. tbReceived = richTextBox_Sensor;
  402. }
  403. tbReceived.Text = "";
  404. }
  405. }
  406. }