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); label_GPSTest_MouseLeave(null, null); } 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_MouseLeave(object sender, EventArgs e) { if (panel_GpsTest.Visible == true) { panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200); panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200); panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255); } else if(panel_Catm1Test.Visible == true) { panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255); panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200); panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200); } else if (panel_SensorTest.Visible == true) { panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200); panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255); panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200); } } private void label_Catm1Test_MouseLeave(object sender, EventArgs e) { if (panel_GpsTest.Visible == true) { panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200); panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200); panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255); } else if (panel_Catm1Test.Visible == true) { panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255); panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200); panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200); } else if (panel_SensorTest.Visible == true) { panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200); panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255); panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200); } } private void label_SensorTest_MouseLeave(object sender, EventArgs e) { if (panel_GpsTest.Visible == true) { panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200); panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200); panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255); } else if (panel_Catm1Test.Visible == true) { panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 255); panel_SensorMode.BackColor = Color.FromArgb(0, 159, 200); panel_GpsMode.BackColor = Color.FromArgb(0, 159, 200); } else if (panel_SensorTest.Visible == true) { panel_Catm1Mode.BackColor = Color.FromArgb(0, 159, 200); panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255); 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_SensorTest_MouseMove(object sender, MouseEventArgs e) { panel_SensorMode.BackColor = Color.FromArgb(0, 159, 255); } private void label_GPSTest_MouseMove(object sender, MouseEventArgs e) { panel_GpsMode.BackColor = Color.FromArgb(0, 159, 255); } 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; pictureBox_Check1.Visible = false; pictureBox_Check2.Visible = false; pictureBox_Check3.Visible = false; pictureBox_Check4.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 { } } private delegate void StringSend(string Text); public void Serial_DataRecvFunction(object sender, SerialDataReceivedEventArgs e) { int nLnegth = serialPort.BytesToRead; byte[] btdata = new byte[nLnegth]; serialPort.Read(btdata, 0, nLnegth); string data = Encoding.Default.GetString(btdata); if (checkBox_TerminalStop.Checked == true) return; // string data = serialPort.ReadExisting(); this.Invoke(new StringSend(Data_Recv_Str), data); /**** *메모리 누수 방지용 코드 */ 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 = null; if (panel_GpsTest.Visible == true) { tbReceived = richTextBox_Gps; } else if (panel_Catm1Test.Visible == true) { tbReceived = richTextBox_catm1; } else if (panel_SensorTest.Visible == true) { 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 tempLines = new LinkedList(tbReceived.Lines); while ((tempLines.Count - nLimitLines) > 0) { tempLines.RemoveFirst(); } tbReceived.Lines = tempLines.ToArray(); } try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } if (checkBox_GPGSV.Checked == true && panel_GpsTest.Visible == true) { tbReceived.AppendText(GPS_TestFunc(text)); } else { tbReceived.AppendText(text); } tbReceived.SelectionStart = tbReceived.Text.Length;//맨 마지막 선택... tbReceived.ScrollToCaret(); } catch { try { SendMessage(this.Handle, WM_SETREDRAW, true, 0); } catch { return; } } } int GPS_TestSnRValue = 0; int Gps_CurrentCount = 0; int TestVal = 0; private string GPS_TestFunc(string data) { int Gps_Count, Sentence,SnrIndex = 3; // 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"; // string tmpstr1 = "$GPGSV,2,2,08,24,36,052,37,25,48,159,42,31,17,245,,32,46,312,45*74\r\n"; string str = data; string[] sp = str.Split(','); int location1, location2,LastLocation ; try { /* if(TestVal == 0) { str = tmpstr; TestVal++; } else { str = tmpstr1; TestVal = 0; } */ location1 = str.IndexOf("$GPGSV"); if (location1 != -1) { str = str.Substring(location1); location2 = str.IndexOf("\r\n"); str = str.Substring(0, location2 + 2); sp = str.Split(','); if (sp[3].Contains("*")) { sp[3] = sp[3].Substring(0, 2); dataGridView_Gps.Rows[0].Cells[1].Value = sp[3]; return str; } else { Sentence = Convert.ToInt32(sp[2]); if(Sentence == 1) { GPS_TestSnRValue = 0; Gps_CurrentCount = 0; } Gps_Count = Convert.ToInt32(sp[3]); for (int i = 0; i < 4; i++) { SnrIndex += 4; if (sp[SnrIndex].Contains("*")) { LastLocation = sp[SnrIndex].IndexOf("*") - 2; sp[SnrIndex] = sp[SnrIndex].Substring(0,2); GPS_TestSnRValue += Convert.ToInt32(sp[SnrIndex]); } else { if (sp[SnrIndex] == "") sp[SnrIndex] = "0"; GPS_TestSnRValue += Convert.ToInt32(sp[SnrIndex]); } Gps_CurrentCount++; if (Gps_CurrentCount == Gps_Count) { GPS_TestSnRValue /= Gps_Count; dataGridView_Gps.Rows[0].Cells[1].Value = GPS_TestSnRValue.ToString(); break; } } } return str; } return ""; } catch (Exception e) { MessageBox.Show(e.StackTrace); } return ""; } private void label_Port_MouseLeave(object sender, EventArgs e) { panel_Port.BackColor = Color.FromArgb(0, 159, 200); } private void label_Port_MouseMove(object sender, MouseEventArgs e) { panel_Port.BackColor = Color.FromArgb(0, 159, 255); } private void label_Port_Click(object sender, EventArgs e) { // Boolean ret = false; try { if (serialPort.IsOpen) // 이미 포트가 열려 있을 때 { //When the port is open serialPort.Close(); label_Port.Text = "Port Open"; //ret = true; } else//When the port is close //포트가 열려 있지 않을 때 { if (comboBox_Port.Text != "") { serialPort.PortName = Serial_Name = comboBox_Port.SelectedItem.ToString(); serialPort.BaudRate = Convert.ToInt32(comboBox_bps.Text); serialPort.Open(); label_Port.Text = "Port Close"; // Debug.Debug_Main_Form_Get(this.main_form); } else { MessageBox.Show("Port is not set"); // ret = true; } } } catch { MessageBox.Show("Port Open Faile" + Serial_Name); } // return ret; } public static DialogResult InputBox(string title, string promptText, ref string value) { Form form = new Form(); Label label = new Label(); TextBox textBox = new TextBox(); Button buttonOk = new Button(); Button buttonCancel = new Button(); form.Text = title; label.Text = promptText; textBox.Text = value; buttonOk.Text = "OK"; buttonCancel.Text = "Cancel"; buttonOk.DialogResult = DialogResult.OK; buttonCancel.DialogResult = DialogResult.Cancel; label.SetBounds(9, 20, 372, 13); textBox.SetBounds(12, 36, 372, 20); buttonOk.SetBounds(228, 72, 75, 23); buttonCancel.SetBounds(309, 72, 75, 23); label.AutoSize = true; textBox.Anchor = textBox.Anchor | AnchorStyles.Right; buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; form.ClientSize = new Size(396, 107); form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel }); form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height); form.FormBorderStyle = FormBorderStyle.FixedDialog; form.StartPosition = FormStartPosition.CenterScreen; form.MinimizeBox = false; form.MaximizeBox = false; form.AcceptButton = buttonOk; form.CancelButton = buttonCancel; DialogResult dialogResult = form.ShowDialog(); value = textBox.Text; return dialogResult; } private void dataGridView_Sensor_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { string value = ""; if (checkBox_Edit.Checked == false) { if (InputBox("번호 입력", "일련 번호:", ref value) == DialogResult.OK) { Name = value; } } } private void dataGridView_Catm1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { string value = ""; if (checkBox_Edit.Checked == false) { if (InputBox("번호 입력", "일련 번호:", ref value) == DialogResult.OK) { Name = value; } } } private void dataGridView_Gps_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { string value = ""; if (checkBox_Edit.Checked == false) { if (InputBox("번호 입력", "일련 번호:", ref value) == DialogResult.OK) { Name = value; } } } private void button_Clear_Click(object sender, EventArgs e) { System.Windows.Forms.RichTextBox tbReceived = null; if (panel_GpsTest.Visible == true) { tbReceived = richTextBox_Gps; } else if (panel_Catm1Test.Visible == true) { tbReceived = richTextBox_catm1; } else if (panel_SensorTest.Visible == true) { tbReceived = richTextBox_Sensor; } tbReceived.Text = ""; } } }