예제 #1
0
파일: Modbus.cs 프로젝트: Somuns-Yjx/Modbus
 /* Name:     Modbus_FormClosing(object sender, FormClosingEventArgs e)
  * Function: If the Form closed , then close all thread, and close the sp
  * Return:   void
  */
 private void Modbus_FormClosing(object sender, FormClosingEventArgs e)
 {
     try
     {
         CloseFlag = true;
         if (socket != null)
         {
             socket.Close();
         }
         if (tAcceptMsg != null)
         {
             tAcceptMsg.Abort();
         }
         if (SerialPortRecv != null)
         {
             SerialPortRecv.Abort();
         }
         if (Savethread != null)
         {
             Savethread.Abort();
         }
         //if (SerialPortSend != null)
         //    SerialPortSend.Abort();
         if (sp != null)
         {
             if (sp.IsOpen)
             {
                 sp.Close();
             }
         }
     }
     catch (Exception er)
     {
         MessageBox.Show("Error:" + er.Message, "Error");
     }
 }
예제 #2
0
파일: Modbus.cs 프로젝트: Somuns-Yjx/Modbus
        private void Serial_OpenBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (Serial_OpenBtn.Text == "Open Serial")
                {
                    if (sp.IsOpen)
                    {
                        sp.Close();

                        SerialPortRecv.Abort();

                        Serial_OpenBtn.Text = "Open Serial";
                        MessageBox.Show("Error ,the serial port opened already \nAnd it is closed now");
                    }
                    else
                    {
                        sp.PortName = comboPortName.Text;                      //设置串口号
                        sp.BaudRate = Convert.ToInt32(comboPortBaudRate.Text); //设置波特率
                        sp.DataBits = Convert.ToInt32(comboDataBits.Text);     //设置数据位
                        //设置停止位
                        switch (comboStopBits.Text)
                        {
                        case "1":
                            sp.StopBits = StopBits.One;
                            break;

                        case "1.5":
                            sp.StopBits = StopBits.OnePointFive;
                            break;

                        case "2":
                            sp.StopBits = StopBits.Two;
                            break;

                        default:
                            MessageBox.Show("Error:参数不正确", "Error");
                            break;
                        }

                        //设置校验位
                        switch (comboParity.Text)
                        {
                        case "NONE":
                            sp.Parity = Parity.None;
                            break;

                        case "ODD":
                            sp.Parity = Parity.Odd;
                            break;

                        case "EVEN":
                            sp.Parity = Parity.Even;
                            break;

                        default:
                            MessageBox.Show("Error:参数不正确", "Error");
                            break;
                        }

                        /*-------- Set Time Out ---------*/
                        sp.ReadTimeout = 10;
                        sp.Open();

                        Serial_OpenBtn.Text = "Close Serial";
                        MessageBox.Show("Open serial successfully");

                        /* Thread serial port Recv start */
                        SerialPortRecv = new Thread(new ThreadStart(SpAcceptMessage));
                        SerialPortRecv.Start();
                        Spconnection = true;

                        /*------------ Rtu Data Init ------------*/
                        _Rtu_Data_Init();
                    }
                }
                else
                {
                    /*------ Close Serial -------*/
                    sp.Close();
                    SerialPortRecv.Abort();
                    Spconnection        = false;
                    Serial_OpenBtn.Text = "Open Serial";
                    MessageBox.Show("Close serial successfully");
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("Error:" + er.Message, "Error");
                return;
            }
        }