Main.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. }
  26. private void panel_PanelMode_MouseMove(object sender, MouseEventArgs e)
  27. {
  28. System.Windows.Forms.Panel Panel = (System.Windows.Forms.Panel)sender;
  29. Panel.BackColor = Color.FromArgb(0, 159, 255);
  30. }
  31. private void label_GPSTest_MouseMove(object sender, MouseEventArgs e)
  32. {
  33. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
  34. }
  35. private void label_GPSTest_MouseLeave(object sender, EventArgs e)
  36. {
  37. panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
  38. }
  39. private void label_Catm1Test_MouseMove(object sender, MouseEventArgs e)
  40. {
  41. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
  42. }
  43. private void label_Catm1Test_MouseLeave(object sender, EventArgs e)
  44. {
  45. panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
  46. }
  47. private void label_SensorTest_MouseMove(object sender, MouseEventArgs e)
  48. {
  49. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
  50. }
  51. private void label_SensorTest_MouseLeave(object sender, EventArgs e)
  52. {
  53. panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
  54. }
  55. public struct POINT { public int X, Y; }
  56. [DllImport("user32.dll")] // 현재 마우스 위치를 얻기위한 API함수.
  57. public extern static void GetCursorPos(out POINT point);
  58. Point FormLocation; // 현재 폼 위치
  59. POINT LastLocation = new POINT(); // 방금 전의 마우스 위치
  60. POINT CurrentLocation = new POINT(); // 현재 마우스 위치
  61. // 폼이 움직일 양 = CurrentLocation - LastLocation.
  62. bool IsMouseMoveStart = false; // 현재 마우스 움직이기 기능이 켜져있는가.
  63. // 만약 이게 없으면 그냥 폼위에서
  64. private void panel_Main_MouseDown(object sender, MouseEventArgs e)
  65. {
  66. GetCursorPos(out CurrentLocation);
  67. FormLocation = this.Location;
  68. IsMouseMoveStart = true;
  69. }
  70. private void panel_Main_MouseMove(object sender, MouseEventArgs e)
  71. {
  72. if (!IsMouseMoveStart) return;
  73. GetCursorPos(out LastLocation);
  74. FormLocation.X -= (CurrentLocation.X - LastLocation.X);
  75. FormLocation.Y -= (CurrentLocation.Y - LastLocation.Y);
  76. this.Location = FormLocation;
  77. CurrentLocation = LastLocation;
  78. }
  79. private void panel_Main_MouseUp(object sender, MouseEventArgs e)
  80. {
  81. IsMouseMoveStart = false;
  82. }
  83. private void pictureBox_min_red_Click(object sender, EventArgs e)
  84. {
  85. this.WindowState = FormWindowState.Minimized;
  86. }
  87. private void pictureBox_X_red_Click(object sender, EventArgs e)
  88. {
  89. Application.Exit();
  90. }
  91. private void label_GPSTest_Click(object sender, EventArgs e)
  92. {
  93. panel_GpsTest.Visible = true;
  94. panel_SensorTest.Visible = false;
  95. panel_Catm1Test.Visible = false;
  96. }
  97. private void label_Catm1Test_Click(object sender, EventArgs e)
  98. {
  99. panel_GpsTest.Visible = false;
  100. panel_SensorTest.Visible = false;
  101. panel_Catm1Test.Visible = true;
  102. }
  103. private void label_SensorTest_Click(object sender, EventArgs e)
  104. {
  105. panel_GpsTest.Visible = false;
  106. panel_SensorTest.Visible = true;
  107. panel_Catm1Test.Visible = false;
  108. }
  109. private void Main_Load(object sender, EventArgs e)
  110. {
  111. Serial_Initialize(ref comboBox_Port);
  112. panel_GpsTest.Visible = true;
  113. panel_SensorTest.Visible = false;
  114. panel_Catm1Test.Visible = false;
  115. }
  116. public void Serial_Initialize(ref ComboBox cb_port)
  117. {
  118. this.serialPort = new System.IO.Ports.SerialPort();
  119. this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.Serial_DataRecvFunction);
  120. cb_port.BeginUpdate();
  121. foreach (string comport in SerialPort.GetPortNames())//foreach (string comport in SerialPort_TestProgram.GetPortNames())
  122. {
  123. cb_port.Items.Add(comport);
  124. }
  125. cb_port.EndUpdate();
  126. //SerialPort 초기 설정.
  127. // serialPort.Encoding = Encoding.GetEncoding("Windows-1252");
  128. cb_port.DataSource = SerialPort.GetPortNames();
  129. try
  130. {
  131. serialPort.PortName = Serial_Name = cb_port.SelectedItem.ToString();
  132. serialPort.BaudRate = (int)115200;
  133. serialPort.DataBits = (int)8;
  134. serialPort.Parity = System.IO.Ports.Parity.None;
  135. serialPort.StopBits = StopBits.One;
  136. }
  137. catch { }
  138. }
  139. public void Serial_DataRecvFunction(object sender, SerialDataReceivedEventArgs e)
  140. {
  141. /****
  142. *메모리 누수 방지용 코드
  143. */
  144. System.GC.Collect(0, GCCollectionMode.Forced);
  145. System.GC.WaitForFullGCComplete();
  146. }
  147. private void label_Port_MouseClick(object sender, MouseEventArgs e)
  148. {
  149. }
  150. int LineLimit = 500;
  151. [DllImport("user32.dll")]
  152. public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
  153. private const int WM_SETREDRAW = 11;
  154. public void Data_Recv_Str(string text)
  155. {
  156. System.Windows.Forms.RichTextBox tbReceived;
  157. tbReceived = richTextBox_catm1;
  158. tbReceived = richTextBox_Gps;
  159. tbReceived = richTextBox_Sensor;
  160. int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
  161. try
  162. {
  163. try
  164. {
  165. SendMessage(this.Handle, WM_SETREDRAW, false, 0);
  166. }
  167. catch (Exception e) { MessageBox.Show(e.StackTrace); }
  168. if (tbReceived.Lines.Length > nLimitLines)
  169. {
  170. LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
  171. while ((tempLines.Count - nLimitLines) > 0)
  172. {
  173. tempLines.RemoveFirst();
  174. }
  175. tbReceived.Lines = tempLines.ToArray();
  176. }
  177. try
  178. {
  179. SendMessage(this.Handle, WM_SETREDRAW, true, 0);
  180. }
  181. catch { return; }
  182. tbReceived.AppendText(text);
  183. tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택...
  184. tbReceived.ScrollToCaret();
  185. }
  186. catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
  187. }
  188. }
  189. }