简体   繁体   English

C#SerialPort DataReceived随机时间后无法触发

[英]C# SerialPort DataReceived fails to Trigger after random time

I'm using Visual Studio C# WPF SerialPort class to communicate with an microcontroller using USB Cable and driver. 我正在使用Visual Studio C#WPF SerialPort类与使用USB电缆和驱动程序的微控制器进行通信。

I want to read input from COM port continuously. 我想连续读取来自COM端口的输入。

This is my current implementation: 这是我当前的实现:

public SerialConnectionHandler()
{
        port = new SerialPort("COM3");
        port.BaudRate = 9600;
        port.Parity = Parity.None;
        port.StopBits = StopBits.One;
        port.Handshake = Handshake.None;
        port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

        port.Open();
}

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    String indata = port.ReadLine();
    Debug.WriteLine(indata);

    processInput(inputData);

    if (port.IsOpen) Debug.WriteLine("--Port is Open--");
}

After an unspecified time my output console reports at least 5 thread ends with code 0. This sometimes takes seconds, minutes or even longer. 经过一段不确定的时间后,我的输出控制台报告至少有5个线程以代码0结尾。这有时需要几秒钟,几分钟甚至更长的时间。

I could not reproduce the problem. 我无法重现该问题。

Port Settings Match. 端口设置匹配。

Datareceived called
--Port is Open--
Datareceived called
--Port is Open--
Datareceived called
--Port is Open--
Der Thread 0xbb4 hat mit Code 0 (0x0) geendet.
Datareceived called
--Port is Open--
Datareceived called
--Port is Open--
Datareceived called
--Port is Open--
Datareceived called
--Port is Open--
Datareceived called
--Port is Open--
Datareceived called

Thread Ends occur randomly, but at one point, a lot of Thread End reports occur. 线程结束是随机发生的,但是在某一点上,会出现很多线程结束报告。 The event won't be triggered from now on anymore. 此事件从现在开始将不再触发。

The port is reported to be opened just before this happens. 据报告,端口即将打开。

--Port is Open--
Datareceived called
--Port is Open--
Datareceived called
--Port is Open--
Der Thread 0x3bc hat mit Code 0 (0x0) geendet.
Der Thread 0x2f54 hat mit Code 0 (0x0) geendet.
Der Thread 0x2ea0 hat mit Code 0 (0x0) geendet.
Der Thread 0x1a6c hat mit Code 0 (0x0) geendet.
Der Thread 0x308 hat mit Code 0 (0x0) geendet.
Der Thread 0x2b0c hat mit Code 0 (0x0) geendet.
Der Thread 0x7fc hat mit Code 0 (0x0) geendet.

So I noticed I had the same problem. 所以我注意到我有同样的问题。 On one project DataReceived works perfect, as expected, but on another - this problem. 在一个项目上, DataReceived可以按预期完成,但在另一个项目上-此问题。

As it turns out, Glorin is right. 事实证明,格洛林是正确的。 The The thread 0xc28 has exited with code 0 I am seeing after few seconds of running my app, is because I was keeping all this code in a seperate class USBCommunicationManager , called from my window class (I'm using WPF) like this: 运行我的应用几秒钟后The thread 0xc28 has exited with code 0 ,这是因为我将所有这些代码都保存在单独的类USBCommunicationManager ,该类从我的窗口类(我正在使用WPF)中调用,如下所示:

new USBCommunicationManager(this);

I was simply initializing it, not assigning it to any variable, that's why garbage collector later picked it up and ended the serial read thread. 我只是在初始化它,而不是将其分配给任何变量,这就是为什么垃圾收集器后来将其选中并结束了串行读取线程的原因。 Just changing it like this, solved the problem: 只是这样更改即可解决问题:

USBCommunicationManager usbCommunicationManager;
..
..
usbCommunicationManager = new USBCommunicationManager(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM