SerialPort.Datarecieved problems

本文关键字:problems Datarecieved SerialPort | 更新日期: 2023-09-27 18:32:29

我对 SerialPort.DataReceived 事件有问题。让我解释一下我的问题。我有 1 个表单,让我们称之为 mainform.cs我有一个类 pumpfuntions.cs,在 pumpfunctions 中.cs我制作我的串行端口并使用串行端口制作我需要的功能。

想要的是我收到的数据进入文本框。但问题是我的 Datareceived 类在我的泵函数.cs中。

我该如何解决这个问题。

代码泵函数.cs:

Class PumpFunctions
    {
        private SerialPort _comport = new SerialPort();
        public string RxString;
        public void SerialPortDataReceived()
        {
            if (_comport.DataReceived != false)
            {
                string ReadData = _comport.ReadExisting();

                RxString = ReadData;
            }
        }
        public void OpenSerialPort()
        {
            _comport.PortName = "COM6";
            _comport.BaudRate = 9600;
            _comport.Open();
        }
        public void CloseSerialPort()
        {
            _comport.Close();
        }
}

代码主窗体.cs

public partial class MainForm : Form
    {
        private PumpFunctions _pump = new PumpFunctions();
        private String rxString;
        public MainForm()
        {
            InitializeComponent();
            _pump.OpenSerialPort();
        }
private void SerialPortDataReceived()
        {
            string rxString = _pump.RxString;
            //this.Invoke(new EventHandler(_commandTextbox_TextChanged));
        }
        private void _commandTextbox_TextChanged(object sender, EventArgs e)
        {
            switch (rxString)
            {
                case "'r'n:": _commandTextbox.AppendText("Status: Stopped'r'n"); break;
                case "'r'n>": _commandTextbox.AppendText("Status: Running forward'r'n"); break;
                case "'r'n<": _commandTextbox.AppendText("Status: Running reverse'r'n"); break;
                case "'r'n*": _commandTextbox.AppendText("Status: Stalled'r'n"); break;
                case "'r'n*|": _commandTextbox.AppendText("Status: Infuse limit switch activated'r'n"); break;
                case "'r'n*W": _commandTextbox.AppendText("Status: Withdraw limit switch activated'r'n"); break;
                case "'r'n*D": _commandTextbox.AppendText("Status: Disable by emergency stop'r'n"); break;
                case "'r'n*T": _commandTextbox.AppendText("Status: Target volume reached'r'n"); break;
                default: _commandTextbox.AppendText(rxString); break;
            }
        }


    }

有人可以向我解释问题是什么。

这也是我得到的错误

错误 1

The event 'System.IO.Ports.SerialPort.DataReceived' can only appear on the left hand side of += or -=

提前致谢

SerialPort.Datarecieved problems

DataReceived 是由 SerialPort 类公开的事件
当您希望从端口接收数据时,向委托注册此事件。

您无法检查事件是否为真/假。 您可以检查其中是否有订阅者。
在此处阅读有关该类的信息 串行端口 MSDN

创建串行端口并注册事件的程序示例:

class Program
{
    static SerialPort serialPort;
    static void Main(string[] args)
    {
        serialPort = new SerialPort("COM6", 1200, Parity.None, 8, StopBits.One);
        serialPort.Open();
        if (serialPort.IsOpen)
        {
            serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
        }
        Thread.Sleep(10000);
    }
    static void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        var length = serialPort.BytesToRead;
        byte[] buffer = new byte[length];
        serialPort.Read(buffer, 0, length);
        for (int i = 0; i < buffer.Length; i++)
        {
            if (buffer.Length >= 2)
            {
                Console.WriteLine("Ok with = > " + buffer.ToString());
            }
        }
    }
}