При повторном открытии дочерней формы настроек программы, COM порт не закрывается при нажатии на кнопку

MainForm

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO.Ports;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp3
    {
        public partial class MainForm : Form
        {
            SerialPort serialPort1 = new SerialPort();
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void настройкиToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Hide();
                SettingsForms form1 = new SettingsForms();
                form1.Show();
            }
    
            private void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                Properties.Settings.Default.Reset();
    
            }
    
            private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
            {
                Properties.Settings.Default.Reset();
                serialPort1.Close();
    
            }
        }
    }
    

SettingForm

        using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO.Ports;
    
    namespace WindowsFormsApp3
    {
        public partial class SettingsForms : Form
        {
            MainForm form2 = new MainForm();
            string dataIN;
            sbyte indexT;
            string sensorT = "0";
    
    
            public SettingsForms()
            {
                form2.Owner = this;
                InitializeComponent();
                comboBoxPorts.Text = Properties.Settings.Default.ComboBoxPortsName;
                buttonConnect.Text = Properties.Settings.Default.ButtonConnectText;
                comboBoxPorts.Enabled = Properties.Settings.Default.ComboBoxPortsEnabled;
                //serialPort1.IsOpen() = Properties.Settings.Default.SerialPortOpen;
    
    
            }
    
    
            private void buttonUpdatePorts_Click(object sender, EventArgs e)
            {
                string[] ports = SerialPort.GetPortNames();
                comboBoxPorts.Text = "";
                comboBoxPorts.Items.Clear();
                if(ports.Length != 0)
                {
                    comboBoxPorts.Items.AddRange(ports);
                    comboBoxPorts.SelectedIndex = 0;
                }
            }
    
            private void buttonConnect_Click(object sender, EventArgs e)
            {
                if(buttonConnect.Text == "Подключиться")
                {
                    try
                    {
                            serialPort1.PortName = comboBoxPorts.Text;
                            serialPort1.Open();
                            buttonConnect.Text = "Отключиться";
                            comboBoxPorts.Enabled = false;
                    }
                    catch
                    {
                        MessageBox.Show("ERROR"); 
                    }
                }
                else if (buttonConnect.Text == "Отключиться") {
                    serialPort1.Close();
                    buttonConnect.Text = "Подключиться";
                    comboBoxPorts.Enabled = true;
    
                }
            }
    
            private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
    
                dataIN = serialPort1.ReadLine();
                this.BeginInvoke(new EventHandler(processData));
            }
            private void processData(object sender, EventArgs e)
            {
                indexT = Convert.ToSByte(dataIN.IndexOf("T"));
                sensorT = dataIN.Substring(0, indexT);
                form2.label1.Text = Convert.ToString(sensorT);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Properties.Settings.Default.ComboBoxPortsName = comboBoxPorts.Text;
                Properties.Settings.Default.ButtonConnectText = buttonConnect.Text;
                Properties.Settings.Default.ComboBoxPortsEnabled = comboBoxPorts.Enabled;
                Properties.Settings.Default.Save();
                Hide();
                form2.Show();   
    
            }
    
            private void SettingsForms_Load(object sender, EventArgs e)
            {
                   if(serialPort1.IsOpen == false)     {  Properties.Settings.Default.Reset(); }
    
            }
        }
    }

При открытии главного окна и переходе во вкладку настройки открывается дочернее окно, где можно обновить порты и подключиться к ним. После подключения к последовательному порту и получению с него данных на главную форму и перехода в настройки снова последовательный порт не закрывается. Пробую реализовать сохранение настроек программы таким образом, чтобы после подключения к COM порту и получению с него данных, можно будет опять перейти в настройки и закрыть последовательный порт(Данные о состоянии порта в настройках сохраняются при выходе из формы настроек на главную форму, также проблема, что при переходе на главное окно после настроек данные с последовательного порта не выводятся ( в данном случае в lable выводится температура).

Свойства


Ответы (0 шт):