串口读取+线程还是更好的东西?

时间:2013-05-02 10:26:29

标签: c# wpf multithreading serial-port

我不知道这是否是使用堆栈执行此任务的好方法,但我确信有更快的方法... 我从微控制器获取数据,但数据长度并不总是相同。 我想也许我可以在我的堆栈中推送数据,在一个线程中我可以弹出它并解码消息。我不想放慢DataReceivedHandler的速度,所以我创建了一个Thread,它可以弹出数据并将其写入我的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);
    }
}

THX

1 个答案:

答案 0 :(得分:3)

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

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
    }
}