打开第二种形式 C# 后拒绝访问 com 端口

Access denied to com port after opening 2nd form C#

当我打开 Form1 时,我可以毫无问题地访问 com 端口。我在 Form1 上有一个按钮可以打开 Form2。打开 Form2 时,我在 Form1 上得到 "Access to the port 'COM4' is denied."。这里是打开COM端口的调用。

private void GeneralTimer_Tick(object sender, EventArgs e)
    {
        if (firstRun == 0)
        {
            init();
        }
        string selectedItem;
        selectedItem = comSelect.Text;
        updateSpeed();

        Fan1Val.Value = Convert.ToInt32(Fan1Val.Value);
        Fan2Val.Value = Convert.ToInt32(Fan2Val.Value);

        if (selectedItem != null && (selectedItem.Contains("COM")))
        {
            if (!serialPort1.IsOpen)
            {
                if (COMPORT != selectedItem)
                {
                    COMPORT = selectedItem;
                    serialPort1.PortName = selectedItem;
                    saveToFile("bin", "com", COMPORT);
                }
                serialPort1.Open(); //<----- I get the error on this line

            }
            updateRPMs();

        }
    }

这是我打开 Form2 的方式:

private void button1_Click(object sender, EventArgs e)
    {

        Form2 form2 = new Form2("Fan1");
        form2.ShowDialog();



    }

我是 C# 新手,如有任何帮助,我们将不胜感激。

编辑

两个代码块都在 Form1 中,我没有尝试从 From2 访问 COM 端口。

不确定为什么这是修复,但我在 form2 中引用 Form1 以使用 Form1 中的 2 个函数。删除引用并将两个函数复制到 form 2 后,它似乎可以工作。

作品:

namespace FanHubController
{
public partial class Form2 : Form
{
    Form1 mainForm = new Form1();

    public Form2(String currFan)
    {
        InitializeComponent();
        currFanBox.Text = currFan;

    }
    public void saveToFile(String path, String fileName, String data)
    {
        String appLoc = AppDomain.CurrentDomain.BaseDirectory;

        String fullPath = appLoc + "\" + path + "\" + fileName + ".txt";

        string createText = data;
        File.WriteAllText(fullPath, createText);
    }

    public String readFile(String path, String fileName)
    {
        String appLoc = AppDomain.CurrentDomain.BaseDirectory;

        String fullPath = appLoc + "\" + path + "\" + fileName + ".txt";
        if (File.Exists(fullPath))
        {
            return System.IO.File.ReadAllText(fullPath);
        }
        else
        {
            return null;
        }

    }
    private void button1_Click(object sender, EventArgs e)
    {

        if (readFile("bin", "fanSpeeds") != null)
        {
            String[] allData = readFile("bin", "fanSpeed").Split(new[] { ";" }, StringSplitOptions.None);

        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        var form2 = new Form2("");
        form2.Close();
    }
}
}

不工作:

namespace FanHubController
{
public partial class Form2 : Form
{
    Form1 mainForm = new Form1();

    public Form2(String currFan)
    {
        InitializeComponent();
        currFanBox.Text = currFan;

    }

    private void button1_Click(object sender, EventArgs e)
    {

        if (mainForm.readFile("bin", "fanSpeeds") != null)
        {
            String[] allData = mainForm.readFile("bin", "fanSpeed").Split(new[] { ";" }, StringSplitOptions.None);

        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        var form2 = new Form2("");
        form2.Close();
    }
}
}