SerialPort.DataReceived事件未触发C#

SerialPort.DataReceived事件未触发C#,c#,event-handling,serial-port,C#,Event Handling,Serial Port,为了捕获通过串行连接传入的信息,我正在尝试实现以下代码。Im使用.NET SerialPort类: //serial port initialization serialPort = new SerialPort(); serialPort.PortName = "COM7"; serialPort.BaudRate = 19200; serialPort.Parity = Parity.Even; serialPort.DataBits = 8; serialP

为了捕获通过串行连接传入的信息,我正在尝试实现以下代码。Im使用.NET SerialPort类:

//serial port initialization           
serialPort = new SerialPort();    

serialPort.PortName = "COM7";
serialPort.BaudRate = 19200;
serialPort.Parity = Parity.Even;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.Handshake = Handshake.None;
serialPort.ReceivedBytesThreshold = 1;



//this is how the handler is added in Form1.Designer.cs
this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.DataReceivedHandler);


//Handler
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        Console.WriteLine("Got Here");
        while (serialPort.BytesToRead > 0)
        {
            byteBuffer.Add((byte)serialPort.ReadByte());
            ProcessByteBuffer(byteBuffer);
        }

    }
我在RS232端口上物理连接了我的Rx和Tx引脚,我试图写一条消息来触发事件,看看它是否工作。我似乎无法让事件触发“get Here”文本从未出现在我的控制台中。我知道数据将进入我的接收缓冲区,因为我可以调用serialPort.ReadExisting()并查看我最初输入的消息

有人能告诉我为什么这件事可能不会发生吗

 serialPort = new SerialPort();  
你搞错了。您重写了设计器创建的SerialPort对象引用。已分配DataReceived事件的一个。您的新对象没有指定该事件。所以它永远不会开火


只需删除该行。

请提供有关如何实例化
串行端口本身的更多信息,以及添加
处理程序的方式。
您是否尝试使用putty或hyperterminal连接到您的端口?我也遇到了同样的问题,没有找到解决方案,最后在后台线程中ping com端口以查看是否有数据。@MehrdadKamelzadeh我添加了更多的信息以澄清。@Arie没有,我只是缩短了tx和rx线路,以模拟PC在写入数据时接收数据。我知道它可以工作,因为我可以读取与输入相同的输出字符串,只是事件没有触发。