123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO.Ports;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace APL_TestCheck
- {
- public partial class Main : Form
- {
- public string Serial_Name { get => serialPort.PortName; set => serialPort.PortName = value; }
- public Main()
- {
- InitializeComponent();
- }
- private void panel_PanelMode_MouseLeave(object sender, EventArgs e)
- {
- System.Windows.Forms.Panel Panel = (System.Windows.Forms.Panel)sender;
- Panel.BackColor = Color.FromArgb(0, 159, 200);
- }
- private void panel_PanelMode_MouseMove(object sender, MouseEventArgs e)
- {
- System.Windows.Forms.Panel Panel = (System.Windows.Forms.Panel)sender;
- Panel.BackColor = Color.FromArgb(0, 159, 255);
- }
- private void label_GPSTest_MouseMove(object sender, MouseEventArgs e)
- {
- panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255);
- }
- private void label_GPSTest_MouseLeave(object sender, EventArgs e)
- {
- panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200);
- }
- private void label_Catm1Test_MouseMove(object sender, MouseEventArgs e)
- {
- panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255);
- }
- private void label_Catm1Test_MouseLeave(object sender, EventArgs e)
- {
- panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200);
- }
- private void label_SensorTest_MouseMove(object sender, MouseEventArgs e)
- {
- panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255);
- }
- private void label_SensorTest_MouseLeave(object sender, EventArgs e)
- {
- panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200);
- }
- public struct POINT { public int X, Y; }
- [DllImport("user32.dll")] // 현재 마우스 위치를 얻기위한 API함수.
- public extern static void GetCursorPos(out POINT point);
- Point FormLocation; // 현재 폼 위치
- POINT LastLocation = new POINT(); // 방금 전의 마우스 위치
- POINT CurrentLocation = new POINT(); // 현재 마우스 위치
- // 폼이 움직일 양 = CurrentLocation - LastLocation.
- bool IsMouseMoveStart = false; // 현재 마우스 움직이기 기능이 켜져있는가.
- // 만약 이게 없으면 그냥 폼위에서
- private void panel_Main_MouseDown(object sender, MouseEventArgs e)
- {
- GetCursorPos(out CurrentLocation);
- FormLocation = this.Location;
- IsMouseMoveStart = true;
- }
- private void panel_Main_MouseMove(object sender, MouseEventArgs e)
- {
- if (!IsMouseMoveStart) return;
- GetCursorPos(out LastLocation);
- FormLocation.X -= (CurrentLocation.X - LastLocation.X);
- FormLocation.Y -= (CurrentLocation.Y - LastLocation.Y);
- this.Location = FormLocation;
- CurrentLocation = LastLocation;
- }
- private void panel_Main_MouseUp(object sender, MouseEventArgs e)
- {
- IsMouseMoveStart = false;
- }
-
- private void pictureBox_min_red_Click(object sender, EventArgs e)
- {
- this.WindowState = FormWindowState.Minimized;
- }
- private void pictureBox_X_red_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void label_GPSTest_Click(object sender, EventArgs e)
- {
- panel_GpsTest.Visible = true;
- panel_SensorTest.Visible = false;
- panel_Catm1Test.Visible = false;
- }
- private void label_Catm1Test_Click(object sender, EventArgs e)
- {
- panel_GpsTest.Visible = false;
- panel_SensorTest.Visible = false;
- panel_Catm1Test.Visible = true;
- }
- private void label_SensorTest_Click(object sender, EventArgs e)
- {
- panel_GpsTest.Visible = false;
- panel_SensorTest.Visible = true;
- panel_Catm1Test.Visible = false;
- }
- private void Main_Load(object sender, EventArgs e)
- {
- Serial_Initialize(ref comboBox_Port);
- panel_GpsTest.Visible = true;
- panel_SensorTest.Visible = false;
- panel_Catm1Test.Visible = false;
- }
- public void Serial_Initialize(ref ComboBox cb_port)
- {
- this.serialPort = new System.IO.Ports.SerialPort();
- this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.Serial_DataRecvFunction);
- cb_port.BeginUpdate();
- foreach (string comport in SerialPort.GetPortNames())//foreach (string comport in SerialPort_TestProgram.GetPortNames())
- {
- cb_port.Items.Add(comport);
- }
- cb_port.EndUpdate();
- //SerialPort 초기 설정.
- // serialPort.Encoding = Encoding.GetEncoding("Windows-1252");
- cb_port.DataSource = SerialPort.GetPortNames();
- try
- {
- serialPort.PortName = Serial_Name = cb_port.SelectedItem.ToString();
- serialPort.BaudRate = (int)115200;
- serialPort.DataBits = (int)8;
- serialPort.Parity = System.IO.Ports.Parity.None;
- serialPort.StopBits = StopBits.One;
- }
- catch { }
- }
- public void Serial_DataRecvFunction(object sender, SerialDataReceivedEventArgs e)
- {
- /****
- *메모리 누수 방지용 코드
- */
- System.GC.Collect(0, GCCollectionMode.Forced);
- System.GC.WaitForFullGCComplete();
- }
- private void label_Port_MouseClick(object sender, MouseEventArgs e)
- {
- }
- int LineLimit = 500;
- [DllImport("user32.dll")]
- public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
- private const int WM_SETREDRAW = 11;
- public void Data_Recv_Str(string text)
- {
- System.Windows.Forms.RichTextBox tbReceived;
- tbReceived = richTextBox_catm1;
- tbReceived = richTextBox_Gps;
- tbReceived = richTextBox_Sensor;
- int nLimitLines = Convert.ToInt32(LineLimit); //제한 라인 수
- try
- {
- try
- {
- SendMessage(this.Handle, WM_SETREDRAW, false, 0);
- }
- catch (Exception e) { MessageBox.Show(e.StackTrace); }
- if (tbReceived.Lines.Length > nLimitLines)
- {
- LinkedList<string> tempLines = new LinkedList<string>(tbReceived.Lines);
- while ((tempLines.Count - nLimitLines) > 0)
- {
- tempLines.RemoveFirst();
- }
- tbReceived.Lines = tempLines.ToArray();
- }
- try
- {
- SendMessage(this.Handle, WM_SETREDRAW, true, 0);
- }
- catch { return; }
- tbReceived.AppendText(text);
- tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택...
- tbReceived.ScrollToCaret();
- }
- catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } }
- }
- }
- }
|