2015-10-30 48 views
0

当我打开Form1时,我可以访问com端口,但没有问题。我有一个Form1打开Form2的按钮。当Form2打开时,我得到“访问端口'COM4'被拒绝。”在Form1上。这里是打开COM端口的要求。打开第二个窗体后访问被拒绝com端口C#

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(); 

     } 
    } 

这是我如何打开窗体2:

private void button1_Click(object sender, EventArgs e) 
    { 

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



    } 

我新的C#和任何帮助,将不胜感激。

编辑

两个代码块在Form1中,我并不想从From2访问COM端口。

+0

是它已经从第一种形式开放?你不能打开它两次......你需要记住关闭COM端口,而不是关闭COM端口会导致问题(即使在随后的程序重新启动时,它可能认为端口在其他地方打开)。 –

+0

@Ron第二种形式不打开它,如果它尚未打开,我只打开它。在程序关闭之前,程序会关闭端口。 –

+0

那么你从第一个表单传递打开的实例到第二个表单来使用?你不能只用相同的端口名创建一个新的'SerialPort',并期望状态能够跨越实例。 –

回答

0

不知道为什么这是修复,但我在Form1中引用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(); 
    } 
} 
} 
+0

当您实例化Form2时,您创建第二个Form1。会发生什么是这也创建另一个串行端口对象。不幸的是,最初的Form1已经打开了该端口,当定时器触发重复的Form1时,它试图从不同的对象打开同一个端口,并且存在冲突。将'Form1 mainform = new Form1();'更改为一个可写属性,并从初始Form1中设置该值。这样您就不会创建第二个窗体,但您可以访问Form2中的Form1方法。 –