c# - 从多个类访问串口

标签 c#

我正在尝试使用串行端口在 arduino 和 c# 程序之间进行通信。我是 c# 编程的新手。该程序有多种用户控制形式。每个都需要访问串 Eloquent 能发送数据。我需要做的就是从每个类写入主窗体中的串行端口。

我了解如何设置和写入串口。这是我的 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 中,并传递给正在使用的其他窗体。

最佳答案

我假设您的两个表单都是父表单的子表单,或者您的其他表单可能是 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 对象,并通过类构造函数传递它。

关于c# - 从多个类访问串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17759124/

相关文章:

c# - N 层应用程序 : Getting a model from Data layer to UI layer

c# - 以编程方式为 LINQ 生成类型化数据集 - 缺少 "Metadata file"?

c# - 在 XNA 中画一条永无止境的线

c# - WPF 自定义控件 DependencyProperty 不会进行数据绑定(bind)

C#/MigraDoc : Long table breaking across pages, 上下边框

c# - 如何打开 Windows 10 ms-settings 特定页面而不访问其他设置

c# - RegisterType 与 UnityContainer 中的接口(interface)

c# - 如何使用 c# 导入 SPWeb 以具有与 PowerShell 相同的行为?

c# - Youtube API注释顺序不正确/网页无法正确加载youtube

c# - 正则表达式模式匹配 : Using only the start and end of a pattern for matching