【问题标题】:Accessing a serial port from multiple classes从多个类访问串行端口
【发布时间】:2013-07-20 15:31:06
【问题描述】:

我正在尝试使用串行端口在 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);
    }      
}

到目前为止,我只写了另外一门课。在继续其余部分之前,我正在尝试设置串行通信。我创建了 sendData 函数,希望能够从另一个类访问它,但我似乎无法让它工作。我正在使用 Visual Studio 2012。

编辑: 它现在工作。这是我的 Form1 代码:

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 中,它正在传递给正在使用的其他表单。

【问题讨论】:

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

标签: c#


【解决方案1】:

我假设您的两个表单都是父表单的子表单,或者您的另一个表单是 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();

基本上你在你的程序结构中创建你的 SerialPort 对象并通过类构造函数传递它。

【讨论】:

    【解决方案2】:

    同时你只能通过一类连接到串口。所以你需要创建一个单例类并在其他类中使用它。

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

    现在像这样在每个类中使用它:

       SerialPortClass.Instance.Write();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多