C# 从多个类访问串行端口

C# 从多个类访问串行端口,c#,C#,我正在尝试使用串行端口在arduino和c#程序之间进行通信。我对c#编程有点陌生。该程序有多个用户控件窗体。每个人都需要访问串行端口来发送数据。我所需要做的就是从每个类以主形式写入串行端口 我了解如何设置和写入串行端口。这是我的Form1代码: partial class Form1 : Form { lightsControl u1; fanControl u2; public Form1() { InitializeComponent();

我正在尝试使用串行端口在arduino和c#程序之间进行通信。我对c#编程有点陌生。该程序有多个用户控件窗体。每个人都需要访问串行端口来发送数据。我所需要做的就是从每个类以主形式写入串行端口

我了解如何设置和写入串行端口。这是我的Form1代码:

partial class Form1 : Form
{
    lightsControl u1;
    fanControl u2;
    public Form1()
    {
        InitializeComponent();
        u1 = new lightsControl();
        u2 = new fanControl();
        u1.Dock = DockStyle.Fill;
        u2.Dock = DockStyle.Fill;
        serialPort1.PortName = "COM4";
        serialPort1.BaudRate = 9600;
        serialPort1.Open();
    }

    private void lightsButton_Click(object sender, EventArgs e)
    {
        u2.Hide();
        u1.Show();
        controlPanel.Controls.Add(u1);
    }

    private void fansButton_Click(object sender, EventArgs e)
    {
        u1.Hide();
        u2.Show();
        controlPanel.Controls.Add(u2);
    }

    private void monitorButton_Click(object sender, EventArgs e)
    {
        u1.Hide();
        u2.Hide();
    }

     public void sendData(ref string tx1, ref string tx2, ref string tx3)
    {
        serialPort1.WriteLine(tx1);
        serialPort1.WriteLine(tx2);
        serialPort1.WriteLine(tx3);
    }      
}
partial class Form1 : Form
{
    lightsControl u1;
    fanControl u2;
    public Form1()
    {

        InitializeComponent();
        u1 = new lightsControl(serialPort);
        u2 = new fanControl();
        u1.Dock = DockStyle.Fill;
        u2.Dock = DockStyle.Fill;
        serialPort.PortName = "COM4";
        serialPort.BaudRate = 9600;
        serialPort.Open();
    }
到目前为止,我只写了一节课。我正在尝试在继续其他操作之前获取串行通信设置。我创建了sendData函数,希望能够从另一个类访问它,但我似乎无法让它正常工作。我正在使用VisualStudio2012

编辑: 现在开始工作了。这是我的Form1代码:

partial class Form1 : Form
{
    lightsControl u1;
    fanControl u2;
    public Form1()
    {
        InitializeComponent();
        u1 = new lightsControl();
        u2 = new fanControl();
        u1.Dock = DockStyle.Fill;
        u2.Dock = DockStyle.Fill;
        serialPort1.PortName = "COM4";
        serialPort1.BaudRate = 9600;
        serialPort1.Open();
    }

    private void lightsButton_Click(object sender, EventArgs e)
    {
        u2.Hide();
        u1.Show();
        controlPanel.Controls.Add(u1);
    }

    private void fansButton_Click(object sender, EventArgs e)
    {
        u1.Hide();
        u2.Show();
        controlPanel.Controls.Add(u2);
    }

    private void monitorButton_Click(object sender, EventArgs e)
    {
        u1.Hide();
        u2.Hide();
    }

     public void sendData(ref string tx1, ref string tx2, ref string tx3)
    {
        serialPort1.WriteLine(tx1);
        serialPort1.WriteLine(tx2);
        serialPort1.WriteLine(tx3);
    }      
}
partial class Form1 : Form
{
    lightsControl u1;
    fanControl u2;
    public Form1()
    {

        InitializeComponent();
        u1 = new lightsControl(serialPort);
        u2 = new fanControl();
        u1.Dock = DockStyle.Fill;
        u2.Dock = DockStyle.Fill;
        serialPort.PortName = "COM4";
        serialPort.BaudRate = 9600;
        serialPort.Open();
    }
这是我的第二个表单代码:

    private SerialPort port; //declare port
    public lightsControl(SerialPort port)//import serialPort from Form1 to port
    {
        InitializeComponent();
        this.port = port;
        hideTimer();
        updateTimer.Enabled = true;
        stopButton.Enabled = false;
    }

    public void WriteToPort(string data)
    {
        this.port.WriteLine(data); //function that writes to serial port
    }
    private void onButton_Click(object sender, EventArgs e)
    {
        statusLabel.Text = "Always On";
        hideTimer();
        switch (light)
        {
            case 0://Light 1
                com = light1Name + "On";
                Tx2 = "a";
                WriteToPort(Tx2);//This is where I need to write to the port
                light1Status = 1;
                light1Reset();
                break;

串行端口对象位于Form1中,并将其传递给正在使用的其他表单。

我假定您的两个表单都是父表单的子表单,或者您的其他表单是Form1的子表单。在这种情况下,您可以在父类中创建SerialPort对象,并通过其构造函数将其发送给子类

像这样:

private SerialPort port;
public Form2(SerialPort port)
{
    InitializeComponent();
    this.port = port;
}

// Use your port object throughout the class now.
public void WriteToPort(string data)
{
    this.port.WriteLine(data);
}
您将按如下方式加载Form2:

Form2 form = new Form2(serialPort);
form.Show();
   SerialPortClass.Instance.Write();

基本上,您可以在程序结构的高层创建SerialPort对象,并通过类构造函数传递它。

同时,您只能通过一个类连接到串行端口。因此,您需要创建一个单例类并在其他类中使用它

 public class SerialPortClass
 {
    private SerialPort _serialPort = new SerialPort() ;

    private SerialPortClass()
    {
       // you can set serialPort setting here
    }

    private static SerialPortClass _instance=new SerialPortClass();
    public static SerialPortClass Instance
    {
      get
      {
          return _instance;
      }
    }


    private void Write()
    {
        _serialPort.Write(.....);
    }
现在在每个类中使用它,如下所示:

Form2 form = new Form2(serialPort);
form.Show();
   SerialPortClass.Instance.Write();

任何错误。它到底在做什么?