Exemple #1
0
        }                                                                       //  結束button1_Click方法

        public void Debug_Information_Refresh()                                 //  宣告Debug_Information_Refresh除錯資料更新方法
        {                                                                       //  進入除錯資料更新方法
            label4.Text = Form4.Login_Date.ToString();                          //  顯示除錯模式登入時間
            label6.Text = (DateTime.Now.ToString());                            //  顯示現在時間
            label8.Text = Form1.UARTConnection1.GetConnectedCOMPortCount().ToString();
            //  顯示已連線之Serialport數量
            label10.Text = DebugVariables.Get_list_SerialPort_Runtimes().ToString();
            //  顯示list_SerialPort方法執行次數
            label12.Text = DebugVariables.Get_comport_DataReceived_Runtimes().ToString();
            //  顯示comport_DataReceived方法執行次數
            label14.Text = DebugVariables.Get_UARTConnectHandle_Runtimes().ToString();
            //  顯示UARTConnectHandle方法執行次數
            label16.Text = DebugVariables.Get_DisplayText_Runtimes().ToString(); //  顯示DisplayText方法執行次數
            label18.Text = DebugVariables.Get_button1_Click_Runtimes().ToString();
            //  顯示button1_Click方法執行次數
            label20.Text = DebugVariables.Get_button2_Click_Runtimes().ToString();
            //  顯示button2_Click方法執行次數
            label22.Text = DebugVariables.Get_button3_Click_Runtimes().ToString();
            //  顯示button3_Click方法執行次數
            label24.Text = DebugVariables.Get_Transmission_Setting_Click_Runtimes().ToString();
            //  顯示設定_傳輸設定ToolStripMenuItem_Click方法
            label26.Text = DebugVariables.Get_User_Interface_Setting_Click_Runtimes().ToString();
            //  顯示設定_介面設定ToolStripMenuItem_Click方法執行次數
            label101.Text = Form1.Uart_Buffer_Size.ToString();                  //  顯示Uart接收資料Buffer大小
            textBox1.Text = Form1.Uart_Buffer_ASCII_Data;                       //  顯示Uart傳輸之Buffer資料(ASCII編碼值)
        }                                                                       //  結束除錯資料更新方法
Exemple #2
0
        }                                                                    //	結束button1_Disable方法

        private void button2_Click(object sender, EventArgs e)               //	當按下"連線/中斷連線"按鈕
        {                                                                    //	進入button2_Click方法
            DebugVariables.Set_button2_Click_Runtimes();                     //	呼叫Set_button2_Click_Runtimes方法記錄button2_Click執行次數
            button2.Enabled = false;                                         //	暫時關閉"連線/中斷連線"按鈕功能
            //UARTConnection.UARTConnectHandle(comboBox1.Text);                 //	呼叫UARTConnectHandle方法
            UARTConnectHandle(comboBox1.Text);                               //	呼叫UARTConnectHandle方法
        }                                                                    //	結束button2_Click方法
Exemple #3
0
        }                                                                               //	結束設定_傳輸設定ToolStripMenuItem_Click方法

        private void 設定_介面設定ToolStripMenuItem_Click(object sender, EventArgs e)
        //	宣告設定_介面設定ToolStripMenuItem_Click方法
        {                                                                               //	進入設定_介面設定ToolStripMenuItem_Click方法
            DebugVariables.Set_User_Interface_Setting_Click_Runtimes();                 //	呼叫Set_User_Interface_Setting_Click_Runtimes方法記錄User_Interface_Setting_Click執行次數
            Form3 User_Interface_Setting_form = new Form3();                            //	宣告User_Interface_Setting_form代表Form3

            User_Interface_Setting_form.Show();                                         //	顯示User_Interface_Setting_form
        }                                                                               //	結束設定_介面設定ToolStripMenuItem_Click方法
Exemple #4
0
        }                                                                    //	結束button3_Disable方法

        //	ToolStripMenuItem選單相關方法
        private void 設定_傳輸設定ToolStripMenuItem_Click(object sender, EventArgs e)
        //	宣告設定_傳輸設定ToolStripMenuItem_Click方法
        {                                                                               //	進入設定_傳輸設定ToolStripMenuItem_Click方法
            DebugVariables.Set_Transmission_Setting_Click_Runtimes();                   //	呼叫Set_Transmission_Setting_Click_Runtimes方法記錄Transmission_Setting_Click執行次數
            Form2 Transmission_Setting_form = new Form2();                              //	宣告transmission_setting_form代表Form2

            Transmission_Setting_form.Show();                                           //	顯示transmission_setting_form
        }                                                                               //	結束設定_傳輸設定ToolStripMenuItem_Click方法
Exemple #5
0
        }                                                                               //  結束UARTConnectHandle方法

        private void comport_DataReceived(object sender, SerialDataReceivedEventArgs e)
        //  comport資料接收處理方法
        {                                                                               //  進入comport資料接收處理方法
            DebugVariables.Set_comport_DataReceived_Runtimes();                         //	呼叫Set_comport_DataReceived_Runtimes方法記錄comport_DataReceived執行次數
            if (UARTConnection1.GetUartComportConnected() == false)                     //  若UartComport未連線(UartComportConnected值為False)
            {                                                                           //  進入if敘述
                return;                                                                 //  結束comport資料接收處理方法
            }                                                                           //  結束if敘述
            byte[] buffer = new byte[1024];                                             //  宣告buffer暫存讀取資料,型態為Byte
            int    length = 0;                                                          //  宣告length變數用於記錄串列資料緩衝區長度

            try                                                                         //  以try語法嘗試讀取Uart串列資料緩衝區
            {                                                                           //  進入try敘述
                length = (sender as System.IO.Ports.SerialPort).Read(buffer, 0, buffer.Length);
                //  讀取Uart串列資料緩衝區
            }                                                                               //  結束try敘述
            catch (ArgumentNullException)                                                   //  當發生NULL例外狀況
            {                                                                               //  進入catch敘述
                return;                                                                     //	結束comport_DataReceived方法
            }                                                                               //  結束catch敘述
            Uart_Buffer_Size = length;                                                      //  更新Uart_Buffer_Size數值
            try                                                                             //  嘗試調整buffer陣列大小
            {                                                                               //  進入try敘述
                Array.Resize(ref buffer, length);                                           //  調整buffer陣列大小
            }                                                                               //  結束try敘述
            catch (ArgumentOutOfRangeException outOfRange)                                  //  若調整buffer陣列空間時範圍錯誤
            {                                                                               //  進入catch敘述
                var result = MessageBox.Show                                                //  顯示錯誤訊息
                                 (                                                          //  進入錯誤訊息MessageBox設定
                                 outOfRange.Message, "",
                                 MessageBoxButtons.OK,                                      //  MessageBox選項為OK
                                 MessageBoxIcon.Error                                       //  顯示錯誤標誌
                                 );                                                         //  結束錯誤訊息MessageBox設定
            }                                                                               //  結束catch敘述
            Display d = new Display(DisplayText);

            try                                                                         //  以try執行invoke
            {                                                                           //  進入try敘述
                Invoke(d, new object[] { buffer });
            }                                                                           //  結束try敘述
            catch (Exception)                                                           //  若發生例外狀況
            {                                                                           //  進入catch敘述
                MessageBox.Show(e.ToString());                                          //  顯示錯誤訊息
                return;                                                                 //  跳出方法
            }                                                                           //  結束catch敘述
            string message = BitConverter.ToString(buffer, 0);                          //  取得Uart傳輸之Buffer資料(ASCII編碼值)

            Uart_Buffer_ASCII_Data = message;                                           //  將Uart傳輸之Buffer資料(ASCII編碼值)填入Uart_Buffer_ASCII_Data
        }                                                                               //  結束comport資料接收處理方法
Exemple #6
0
        }                                                                               //	結束comboBox1_text_change方法

        public void list_SerialPort()                                                   //	偵測並列出已連線SerialPort方法
        {                                                                               //	進入list_SerialPort方法
            DebugVariables.Set_list_SerialPort_Runtimes();                              //	呼叫Set_list_SerialPort_Runtimes方法遞增list_SerialPort_Runtimes變數
            ComportListToComboBox(UARTConnection1.GetComportList(), comboBox1);         //	將Comport偵測結果更新至下拉式選單

            /*if (ports.Length == 0)                                              //	若偵測不到任何已連接的SerialPort(ports.Length為0)
             * {                                                                   //	進入if敘述
             *      ErrorCodeMessage.Error_Message_Show((int)ErrorCodeMessage.ErrorCodeEncoding.NoSerialPortConnected);
             *      //	顯示錯誤訊息
             *      button2.Enabled = false;                                        //	關閉"連線/中斷連線"按鈕功能
             *      textBox1.Enabled = false;                                       //	關閉textBox1(接收字串資料文字方塊)功能
             *      return;                                                         //	提早結束list_SerialPort方法
             * }                                                                   //	結束if敘述
             * else                                                                //	若偵測到已連線的SerialPort
             * {                                                                   //	進入else敘述
             *      button2.Enabled = false;                                        //	暫時關閉"連線"按鈕功能,待使用者選定愈連線之Serialport(未選定連線Serialport,可避免發生Error_010002)
             *      textBox1.Enabled = true;                                        //	開啟textBox1(接收字串資料文字方塊)功能
             *      return;                                                         //	結束list_SerialPort方法
             * }                                                                   //	結束else敘述*/
        }                                                                               //	結束list_SerialPort方法
Exemple #7
0
        }                                                                               //  結束comport資料接收處理方法

        private void DisplayText(byte[] buffer)                                         //  DisplayText顯示文字方法
        {                                                                               //  進入DisplayText顯示文字方法
            DebugVariables.Set_DisplayText_Runtimes();                                  //	呼叫Set_DisplayText_Runtimes方法記錄DisplayText方法執行次數
            textBox1.Font = textBox1_Font;                                              //  設定文字方塊顯示字型
            //textBox1.Text += String.Format("{0}{1}", BitConverter.ToString(buffer), Environment.NewLine);
            //  將buffer中之接收文字(ascii原始碼)顯示於textBox1上
            //textBox1.Text += System.Text.Encoding.ASCII.GetString(buffer);    //  已修正使用AppendText方法顯示
            textBox1.AppendText(System.Text.Encoding.ASCII.GetString(buffer));
            //  將buffer中之接收資料(ascii原始碼)轉換為字串(符號字元),並顯示於textBox1上
            FileIO.Instance.FileWrite("Data.txt", System.Text.Encoding.ASCII.GetString(buffer));
            //  將buffer中之接收資料(ascii原始碼)轉換為字串(符號字元),並以FileMode.Append模式寫入"Data.txt"檔案
            FileIO.Instance.FileWrite("Date_Data.txt", DateTime.Now.ToString() + "\t" + System.Text.Encoding.ASCII.GetString(buffer) + "\r\n");
            //  以FileMode.Append模式建立包含傳輸時間資訊的資料記錄檔"Date_Data.txt"
            Transmission_Buffer_CharArray = System.Text.Encoding.ASCII.GetString(buffer).ToCharArray();
            //  將buffer中之接收資料(ascii原始碼)轉換為字串(符號字元)後,再轉換為字元陣列(CharArray),存入Transmission_Buffer_CharArray
            textBox1.ScrollBars     = ScrollBars.Vertical;                                  //  控制textBox1顯示垂直捲軸
            textBox1.SelectionStart = textBox1.Text.Length;                                 //  控制textBox1游標位置為textBox1內容結束處
            textBox1.ScrollToCaret();                                                       //  捲動textBox1捲軸至游標位置(自動捲動至最新資料)
            Total_Transmission_Length = Total_Transmission_Length + buffer.Length;
            //  累計接收字串長度
            Label2.Text = Total_Transmission_Length.ToString();                         //  顯示累計字串長度
            Uart_Data_Analysis();                                                       //  呼叫Uart_Data_Analysis方法
        }                                                                               //  結束DisplayText顯示文字方法
Exemple #8
0
        }                                                                       //  結束除錯資料更新方法

        public void Debug_Information_Reset()                                   //  宣告Debug_Information_Reset除錯資料重置方法
        {                                                                       //  進入除錯資料重置方法
            DebugVariables.ResetDebugVariables();                               //	呼叫ResetDebugVariables方法重置變數
        }                                                                       //  結束除錯資料重置方法
Exemple #9
0
        }                                                                    //	結束Form1_Load方法

        private void button1_Click(object sender, EventArgs e)               //	當按下"重新偵測SerialPort"按鈕
        {                                                                    //	進入button1_Click方法
            DebugVariables.Set_button1_Click_Runtimes();                     //	呼叫Set_button1_Click_Runtimes方法記錄button1_Click執行次數
            //UARTConnection.list_SerialPort();                                   //	呼叫list_SerialPort(偵測並列出已連線SerialPort)方法
            list_SerialPort();                                               //	呼叫list_SerialPort(偵測並列出已連線SerialPort)方法
        }                                                                    //	結束button1_Click方法
Exemple #10
0
        }                                                                               //	結束ComportListToComboBox方法

        /// <summary>
        /// UARTConnectHandle方法
        /// UARTConnectHandle方法用於處理UartComport連線設定
        /// 呼叫格式為UARTConnectHandle(comport名稱)
        ///
        /// </summary>
        /// <param name="comport_name"></param>
        public void UARTConnectHandle(string comport_name)                            //	串列埠連線處理UARTConnectHandle方法
        {                                                                             //	進入UARTConnectHandle方法
            DebugVariables.Set_UARTConnectHandle_Runtimes();                          //	呼叫Set_UARTConnectHandle_Runtimes方法遞增UARTConnectHandle_Runtimes變數
            if (UARTConnection1.GetUartComportConnected() == true)                    //  若UartComportConnected為True,代表UartComport連線中,將執行中斷連線
            {                                                                         //	進入if敘述
                label6.Text = (comport_name + "正在中斷連線");
                //  顯示連線狀態為(comport_name + "正在中斷連線"),如"COM1正在中斷連線"
                UARTConnection1.SetUartComportConnected(false);                          //	更新UartComportConnected
                UARTConnection1.UartComport.Close();                                     //	關閉UartComport連線
                button2.Text    = "連線";                                                  //	更改button2文字為"連線"
                button2.Enabled = true;                                                  //	重新開啟"連線/中斷連線"按鈕功能
                label6.Text     = "未連線";                                                 //	顯示連線狀態為"未連線"
                return;                                                                  //	結束UARTConnectHandle方法
            }                                                                            //	結束if敘述
            else                                                                         //	若UartComportConnected為False,執行連線
            {                                                                            //	進入else敘述
                label6.Text = "偵測連接埠設定";                                                 //	顯示連線狀態為"偵測連接埠設定"
                if (comport_name == "")                                                  //	若comport_name為空白(Combobox1未選定)
                {                                                                        //	進入if敘述
                    ErrorCodeMessage.Error_Message_Show((int)ErrorCodeMessage.ErrorCodeEncoding.NoSerialPortSelected);
                    //	顯示錯誤訊息
                    button2.Enabled = true;                                                   //	重新開啟"連線/中斷連線"按鈕功能
                    return;                                                                   //	結束UARTConnectHandle方法
                }                                                                             //	結束if敘述
                else                                                                          //	已選定連接埠
                {                                                                             //	進入else敘述
                    label6.Text = (comport_name + "正在嘗試連線");                                  //	顯示連線狀態為(comport_name + "正在嘗試連線"),如"COM1正在嘗試連線"
                    try                                                                       //	嘗試以comport_name建立串列通訊連線
                    {                                                                         //	進入try敘述
                        UARTConnection1.UartComport = new SerialPort(comport_name);
                        //	UartComport串列埠建立comport_name連線
                    }                                                                               //	結束try敘述
                    catch (System.IO.IOException)                                                   //	當IO發生錯誤時的例外狀況
                    {                                                                               //	進入catch敘述
                        ErrorCodeMessage.Error_Message_Show((int)ErrorCodeMessage.ErrorCodeEncoding.SerialPortConnectError);
                        //	顯示錯誤訊息
                        button2.Enabled = true;                                                     //	重新開啟"連線/中斷連線"按鈕功能
                        return;                                                                     //	結束UARTConnectHandle方法
                    }                                                                               //	結束catch敘述
                    try                                                                             //	以try方式執行資料接收
                    {                                                                               //	進入try敘述
                        if (!UARTConnection1.UartComport.IsOpen)                                    //	若UartComport未開啟
                        {                                                                           //	進入if敘述
                            UARTConnection1.UartComport.Open();                                     //	開啟UartComport
                            label6.Text = (comport_name + "已連線");                                   //	顯示連線狀態為(comport_name + "已連線"),如"COM1已連線"
                            UARTConnection1.SetUartComportConnected(true);                          //	更新UartComportConnected狀態
                            button2.Text    = "中斷連線";                                               //	更改button2文字為"中斷連線"
                            button2.Enabled = true;                                                 //	重新開啟"連線/中斷連線"按鈕功能
                        }                                                                           //	結束if敘述
                        UARTConnection1.UartComport.DataReceived += new SerialDataReceivedEventHandler(comport_DataReceived);
                        //  處理資料接收
                    }                                                                   //  結束try敘述
                    catch (Exception ex)                                                //  若發生錯誤狀況
                    {                                                                   //  進入catch敘述
                        var result = MessageBox.Show                                    //  顯示錯誤訊息
                                         (                                              //  進入錯誤訊息MessageBox設定
                                         ex.ToString(), "DataReceived Error",           //  顯示錯誤訊息ex.ToString(),標題為"DataReceived Error"
                                         MessageBoxButtons.OK,                          //  MessageBox選項為OK
                                         MessageBoxIcon.Error                           //  顯示錯誤標誌
                                         );                                             //  結束錯誤訊息MessageBox設定
                        UARTConnection1.UartComport.Close();                            //  關閉UartComport連線
                        button2.Text    = "連線";                                         //  更改button2文字為"連線"
                        button2.Enabled = true;                                         //  重新開啟"連線/中斷連線"按鈕功能
                        return;                                                         //  結束UARTConnectHandle方法
                    }                                                                   //  結束catch敘述
                }                                                                       //  結束else敘述
            }                                                                           //  結束else敘述
        }                                                                               //  結束UARTConnectHandle方法
Exemple #11
0
        }                                                                    //	結束button2_Disable方法

        private void button3_Click(object sender, EventArgs e)               //	當按下"清除"(button3)按鈕
        {                                                                    //	進入button3_Click方法
            DebugVariables.Set_button3_Click_Runtimes();                     //	呼叫Set_button3_Click_Runtimes方法記錄button3_Click執行次數
            textBox1.Clear();                                                //	清除textBox1(接收字串資料文字方塊)資料
        }                                                                    //	結束button3_Click方法