在两种不同的形式上使用相同的串行端口 [英] Using same serialport on two different forms

查看:26
本文介绍了在两种不同的形式上使用相同的串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式 = Form1.CS 和 SMS.CS在第一个表单 (Form1) 上,我已经配置好并准备好使用 serialport1.我有按钮,称为发送消息.此按钮会打开名为 SMS.cs 的新表单.

I have two forms = Form1.CS and SMS.CS on the first form (Form1) i've configured and ready to work serialport1. I have button which is called send message. This button opens new form called SMS.cs.

private void SMS_Click(object sender, EventArgs e)
    {
        SMS settings = new SMS();
        settings.ShowDialog();
    }

我想将我配置的 serialport1 用于表单:Form1 和表单 SMS.cs.也可以从表单接收 SMS 数据,然后使用 Form1 上的 serialport1 发送它,例如:

I'd like to use my configured serialport1 on to forms: Form1 and form SMS.cs. It's also possible to receive from form SMS data, and send it using serialport1 on Form1 for example:

 private void SMS_Click(object sender, EventArgs e)
    {
        SMS settings = new SMS();
        settings.ShowDialog();
        SerialPort1.Writeln(Data from form SMS)
    }

但是我不知道该怎么做.我认为最好的主意是直接从 SMS 表单发送数据..

but i don't know how to do it. The best idea in my opinion is to send data directly from SMS form..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication1
{
    public partial class SMSForm : Form
    {
        SerialPort SerialP;

        public SMSForm(Object SerialP)
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) 
        {
            SerialP.WriteLine("ATI");
        }
    }
}

推荐答案

Form1 是 SerialPort 控制器 - 即 Form1 配置 SerialPort.如果您有一个影响 SerialPort 的事件,例如SerialPort1.Writeln",您应该将方法引用传递给对话框构造函数.这将在逻辑上保持类分开,同时共享方法.

Form1 is the SerialPort controller - that is, Form1 configures SerialPort. If you have an event that affects SerialPort, such as `SerialPort1.Writeln', you should pass a method reference to the dialog constuctor. This will logically keep classes separate while sharing methods.

您可以创建一个自定义的 EventArgs 对象来将字符串传回您的 SerialPort:

You can create a custom EventArgs object to pass the string back to your SerialPort:

public class WriteToSerialPortEventArgs : EventArgs
{
    public WriteToSerialPortEventArgs (string Write)
    { WriteBytes = Encoding.GetBytes(write); }  // simplified - see MSDN for more options 

    public byte[] WriteBytes
    { get; private set; }   
}

Form1 传递给 SMS 对话框的示例事件委托:

Form1 sample event delegate passed to your SMS dialog:

void OnWriteToSerialPort(object sender, WriteToSerialPortEventArgs e )
{
    SerialPort1.WriteLine(e.WriteBytes);
}

编写您的 SMS 构造函数:

Write your SMS constructor:

// field
Action<object, WriteToSerialPortEventArgs> writeDelegate;

public SMS (Action<object, WriteToSerialPortEventArgs> writeDelegate)
{
    this.writeDelegate = writeDelegate;
    this.button1.Click += new EventHandler(button1_Click);
}

...短信事件委托:

void button1_Click(object sender, EventArgs e)
{
    writeDelegate.Invoke(sender, new WriteToSerialPortEventArgs (txtBox.Text);
}

从技术上讲,您不需要完整的 Invoke(...) 方法签名.我把它包括在内是为了完整性,可以写成:

Technically, you don't need the full Invoke(...) method signature. I included it for completeness and can be written as:

writeDelegate(sender, new WriteToSerialPortEventArgs (txtBox.Text);

这篇关于在两种不同的形式上使用相同的串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆