C#Com端口关闭

时间:2017-03-05 20:37:02

标签: c# winforms

我需要从COM中的Form1读取数据,然后再次在Form2中读取数据。

最好的方法是什么?

Form1中阅读COM数据,然后mySerialPort.Close();,并在Form2中打开新连接?
如果像这样,我上面的代码中应该close吗?

或者不要关闭COM?如果不关闭,我如何阅读Form2

中的数据
namespace portreader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            SerialPort mySerialPort = new SerialPort();
            mySerialPort.PortName = "COM3";
            mySerialPort.BaudRate = 9600;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;
            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
            mySerialPort.Open();
        }

        string _buffer;
        private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (this.InvokeRequired)
            {
                // Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
                this.BeginInvoke(new EventHandler<SerialDataReceivedEventArgs>(mySerialPort_DataReceived), new object[] { sender, e });
                return;
            }
            SerialPort sp = (SerialPort)sender;
            string data = sp.ReadExisting();

            _buffer += data;
            if (_buffer.Length >= 8)
            {
                int chipnumber = Int32.Parse(_buffer, System.Globalization.NumberStyles.HexNumber);
                Form2 form2 = new Form2(chipnumber);
                form2.ShowDialog(this);
                _buffer = null;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

你需要创建一个form1和form2都可以使用的类,然后给form1和form2一个对该类的引用。

理想情况下,表格根本不知道有串口。他们只是使用您的类/服务来提问并获得答案,并且封装在该类中是进入串口并进行查询的逻辑。

这是软件的基本设计原则之一,将UI与应用程序逻辑分开。

在另一篇相关文章中查看我的答案

Serial Port Polling and Data handling

答案 1 :(得分:1)

您可以简单地将ComPort包装在类似

的类中
public class DataInput
{
    public event Action<string> DataReceived;

    private readonly SerialPort mySerialPort;

    public DataInput(string portName)
    {
        mySerialPort = new SerialPort();
        mySerialPort.PortName = portName;
        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
        mySerialPort.Open();
    }

    private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        string received = mySerialPort.ReadExisting();
        DataReceived?.Invoke(received);
    }
}

现在,每个表单(以及您软件的其他组件)都可以暂停DataReceived实例的事件DataInput。因此,每个组件都可以按照自己喜欢的方式处理数据。因此,您只需创建DataReceived的实例,并在表单中使用它,如:

public partial class Form1 : Form
{
    private readonly DataInput dataInput;

    public Form1()
    {
        InitializeComponent();
        dataInput = new DataInput("COM3");
        dataInput.dataReceived += OnDataReceived;
    }

    private void OnDataReceived(string Data)
    {
        //Do whatever you want with the data
    }
}

现在,您可以将dataInput传递到第二个表单,然后又可以再次使用dataReceived事件。