串口读取+线程或更好的东西? [英] Serial port reading + Threads or something better?
问题描述
我不知道这是否是一个堆栈此任务的工作一个很好的方式,但我敢肯定有一个更快的方法...
我从微控制器获取数据,但数据长度并不总是相同的长度。
我想也许我可以在我的压栈数据,并在一个线程我可以弹出它和解码的消息。我没有想慢下来DataReceivedHandler所以后来我创建了一个线程,可以弹出数据,并在我的decodeMessage()函数写信给我的列表视图。
I dont know if this is a good way to work with a stack for this task but I'm sure there is a faster way ... I get data from my microcontroller but the data length is not always the same length. I thought maybe I can push data in my stack and in a thread I can pop it and decode the message. I didnt wanted slow down the DataReceivedHandler so then I created a Thread which can pop the data and write it to my Listview in my decodeMessage() function.
在很短的时间我得到一个System.OutOfMemories异常。
After a short time I get a System.OutOfMemories Exception..
任何想法我怎么能以更好的方式呢?
Any ideas how I can do it in a better way ?
我是从我的串口读取只是当数据到达这里:
I'm reading from my serial port just when data arrives here:
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);
}
这是我的主题:
And this is my Thread:
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
thx
推荐答案
此代码中使用线程安全的队列。我简化了一些我自己的代码,所以这段代码没有进行测试或编译。如果你有问题,编译或产生错误,注释添加到我,我会帮你。
This code use a thread safe queue. I simplified some of my own code, so this code is not tested or compiled. If you have problems compiling or it produce errors, add a comment to me and I will help you out.
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
}
}
这篇关于串口读取+线程或更好的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!