Serial.readline - 内存不足

本文关键字:内存不足 readline Serial | 更新日期: 2023-09-27 18:37:11

我正在从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);
        }
    }
}

}

Serial.readline - 内存不足

这可能是由类 SerialPort 的实现引起的。它可能永远不会将控制权交给其他线程,因此终结线程无法完成对象并释放内存。请参阅我关于它的文章:http://alexatnet.com/articles/net-memory-management-and-garbage-collector#best-practices

要修复它,您需要添加

Thread.CurrentThread.Join(100)

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