C# Serial.readline-内存不足

C# Serial.readline-内存不足,c#,.net,serial-port,arduino,C#,.net,Serial Port,Arduino,我正在读一个Arduino,它通过USB端口发送文本。Arduino每秒发送其输出的状态。在命令接收事件上,我设置了两个复选框(快门打开或电源打开,或灯打开),并且它还将接收到的数据输出到多行文本框。 无论如何这一切都能正常工作,持续几秒钟,然后变慢,最后大约10分钟后,我出现了内存不足异常。我不知道出了什么问题,我假设它在读取串行数据的类中-这是代码,有人能看到任何错误吗 using System; using System.IO.Ports; namespace WOCA.Core.Ser

我正在读一个Arduino,它通过USB端口发送文本。Arduino每秒发送其输出的状态。在命令接收事件上,我设置了两个复选框(快门打开或电源打开,或灯打开),并且它还将接收到的数据输出到多行文本框。
无论如何这一切都能正常工作,持续几秒钟,然后变慢,最后大约10分钟后,我出现了内存不足异常。我不知道出了什么问题,我假设它在读取串行数据的类中-这是代码,有人能看到任何错误吗

using System;
using System.IO.Ports;

namespace WOCA.Core.SerialComms
{
    internal class ArduinoCommunicator : ICommunicator
    {
        public event EventHandler<CommsEventsArg> CommandReceived;

    internal ArduinoCommunicator(string comPort)
    {
        Port = new SerialPort(comPort) {BaudRate = 9600, DtrEnable = true};
        Port.DataReceived += PortOnDataReceived;
    }

    private SerialPort Port { get; set; }


    public bool IsOpen { get; set; }


    public void Open()
    {
        try
        {
            if (!Port.IsOpen)
            {
                Port.Open();
                IsOpen = true;
            }
            else
            {
                throw new InvalidSerialCommsException("Serial port already open");
            }
        }
        catch (Exception ex)
        {
            throw new InvalidSerialCommsException("Serial Port error: " + ex.Message);
        }
    }

    public void Close()
    {
        try
        {
            if (Port.IsOpen)
            {
                Port.Close();
                IsOpen = false;
            }
            else
            {
                throw new InvalidSerialCommsException("Serial port not open");
            }
        }
        catch (Exception)
        {

            throw new InvalidSerialCommsException("Serial port error");
        } 

    }

    public void SendCommand(string command)
    {
        try
        {
            if (Port.IsOpen)
            {
                Port.Write(command);
            }
            else
            {
                throw new InvalidSerialCommsException("Serial port not open");
            }
        }
        catch (Exception)
        {

            throw new InvalidSerialCommsException("Serial port error, the command has not been sent");
        }

    }

    private void PortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
    {
        SerialPort serialPort = sender as SerialPort;

        if (serialPort != null)
        {
            string command = serialPort.ReadLine();
            command = command.Remove(command.Length-1,1);
            CommsEventsArg args = new CommsEventsArg(command);
            OnCommandReceived(args);
        }
    }

    protected virtual void OnCommandReceived(CommsEventsArg e)
    {
        EventHandler<CommsEventsArg> handler = CommandReceived;
        if (handler != null)
        {
            handler(this, e);
        }
    }
}
使用系统;
使用System.IO.Ports;
命名空间WOCA.Core.SerialComms
{
内部类通讯器:ICommunicator
{
已接收公共事件事件处理程序命令;
内部通讯器(字符串组件)
{
Port=newserialport(comPort){BaudRate=9600,DtrEnable=true};
Port.DataReceived+=PortOnDataReceived;
}
专用串行端口{get;set;}
公共布尔等参元{get;set;}
公开作废
{
尝试
{
如果(!Port.IsOpen)
{
Port.Open();
IsOpen=真;
}
其他的
{
抛出新的InvalidSerialCommsException(“串行端口已打开”);
}
}
捕获(例外情况除外)
{
抛出新的InvalidSerialCommsException(“串行端口错误:+ex.Message”);
}
}
公众假期结束()
{
尝试
{
if(端口等参线)
{
Port.Close();
IsOpen=假;
}
其他的
{
抛出新的InvalidSerialCommsException(“串行端口未打开”);
}
}
捕获(例外)
{
抛出新的InvalidSerialCommsException(“串行端口错误”);
} 
}
公共void SendCommand(字符串命令)
{
尝试
{
if(端口等参线)
{
端口写入(命令);
}
其他的
{
抛出新的InvalidSerialCommsException(“串行端口未打开”);
}
}
捕获(例外)
{
抛出新的InvalidSerialCommsException(“串行端口错误,命令尚未发送”);
}
}
私有void PortOnDataReceived(对象发送方,SerialDataReceivedEventArgs SerialDataReceivedEventArgs)
{
SerialPort SerialPort=发送方作为SerialPort;
如果(serialPort!=null)
{
string命令=serialPort.ReadLine();
command=command.Remove(command.Length-1,1);
COMMSEventsargs=新CommsEventsArg(命令);
未收到命令(args);
}
}
收到命令时受保护的虚拟无效(CommsEventsArg e)
{
EventHandler=CommandReceived;
if(处理程序!=null)
{
处理者(本,e);
}
}
}

}

这可能是由SerialPort类的实现引起的。它可能永远不会将控制权交给其他线程,因此终结线程无法终结对象并释放内存。请参阅我的文章:

要修复它,您需要添加

Thread.CurrentThread.Join(100)

例如,它可以添加到
PortOnDataReceived
。这将允许终结线程运行挂起的终结器。

向我们显示您附加到
CommandReceived
的位置以及响应该位置的方法。您是否尝试查找您在哪里收到此异常。我是说在哪一行?可能是平台错误。串行驱动程序代码可能在某个点使用非斜体指针。您可以尝试在try/catch块中包围ReadLine()并忽略OOM异常?大约有5种方法订阅CommandReceiveVD事件,但它们所做的只是将复选框设置为true或false,或者将接收到的数据打印到文本框中。如何找到内存不足的位置?如果我在暂停按钮挂起时(在错误发生之前)点击暂停按钮,那么它位于CommandReceivedHandler方法,这就是我所期望的!它似乎挂在这一行:txtbxMessages.AppendText(txtbxMessages.Text+e.Command+Environment.NewLine);