簡體   English   中英

打開第二個表單C#后拒絕訪問com端口

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

當我打開Form1時,我可以正常訪問com端口,而不會出現問題。 我在Form1上有一個打開Form2的按鈕。 打開Form2時,我得到“拒絕訪問端口'COM4'。” 在Form1上。 這是打開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個函數。 刪除引用並將兩個函數復制到窗體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();
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM