0

我不知道这是否是使用堆栈完成此任务的好方法,但我确信有更快的方法......我从微控制器获取数据,但数据长度并不总是相同的长度。我想也许我可以将数据推送到我的堆栈中,然后在一个线程中我可以弹出它并解码消息。我不想减慢 DataReceivedHandler 的速度,所以我创建了一个线程,它可以弹出数据并将其写入我的 decodeMessage() 函数中的 Listview。

不久之后,我得到一个 System.OutOfMemories 异常..

任何想法我可以如何以更好的方式做到这一点?

当数据到达这里时,我正在从我的串口读取数据:

Stack<byte[]> stack = new Stack<byte[]>();

......

public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    byte[] data = new byte[sp.BytesToRead];
    sp.Read(data, 0, data.Length);

    stack.Push(data);
}

这是我的主题:

private void formatData()
{
    try
    {
        while (true)
        {
            byte[] data;
            int i=0;

            Dispatcher.BeginInvoke(new Action(() =>
            {
                while (stack.Count > 0)
                {
                    data = stack.Pop();
                    while (i < data.Length)
                    {
                        decodeMessage(data[i]);
                        i++;
                    }
                }
            }));          
        }
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

谢谢

4

1 回答 1

4

此代码使用线程安全队列。我简化了一些我自己的代码,所以这段代码没有经过测试或编译。如果您在编译时遇到问题或产生错误,请给我添加评论,我会帮助您。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Windows.Threading;
using System.Collections.Concurrent;

void someRoutine()
{
    // initialize queue before using it
    serialDataQueue = new ConcurrentQueue<char>();

}


/// <summary>
/// data from serialPort is added to the queue as individual chars, 
/// a struct may be better
/// </summary>
public ConcurrentQueue<char> serialDataQueue;

// get data
void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = sender as SerialPort;
    int bytesAvailable = sp.BytesToRead;

    // array to store the available data    
    char[] recBuf = new char[bytesAvailable];

    try
    {    
        // get the data
        sp.Read(recBuf, 0, bytesAvailable);

        // put data, char by char into a threadsafe FIFO queue
        // a better aproach maybe is putting the data in a struct and enque the struct        
        for (int index = 0; index < bytesAvailable; index++)
           serialDataQueue.Enqueue(recBuf[index]);

    }
    catch (TimeoutException ex)
    {
        // handle exeption here
    }
}



/// <summary>
/// Check queue that contains serial data, call this 
/// routine at intervals using a timer or button click
/// or raise an event when data is received
/// </summary>
private void readSearialDataQueue()
{
    char ch;

    try
    {
        while (serialDataQueue.TryDequeue(out ch))
        {
            // do something with ch, add it to a textbox 
            // for example to see that it actually works
            textboxDataReceived.Text += ch;
        }

    }
    catch (Exception ex)
    {
        // handle ex here
    }
}
于 2013-05-02T18:47:27.363 回答