wpf 串行端口连续实时数据c#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16877660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
serialport continuous real time data c#
提问by pgwri
Hi I'm trying to program a simple C# WPF that displays time information on a virtual scoreboard in real time from a timing system. I'm fairly new to programming so in depth explanation would be appreciated.
嗨,我正在尝试编写一个简单的 C# WPF,它可以从计时系统实时显示虚拟记分牌上的时间信息。我对编程还很陌生,因此将不胜感激。
I have created a new thread to handle the incoming data from the COM port and as the app is developed this data will be interpreted. For now I just wanted to display the raw information (in hex) that is coming from the timer into a textbox. This works but not as intended. I am receiving tons of duplicate information, my only explanation is I am reading the data too slowly or its reading the same byte over and over. What I would like to happen is to take out each byte and display them, all controlled by one start/stop button.
我创建了一个新线程来处理来自 COM 端口的传入数据,并且随着应用程序的开发,这些数据将被解释。现在我只想将来自计时器的原始信息(以十六进制)显示到文本框中。这有效但不符合预期。我收到了大量重复信息,我唯一的解释是我读取数据太慢或者它一遍又一遍地读取相同的字节。我想要发生的是取出每个字节并显示它们,所有这些都由一个开始/停止按钮控制。
Possible solutions include storing the entire buffer in a list or array which I'm not quite sure of yet, I don't want to add so many threads that the program freezes everything up.
可能的解决方案包括将整个缓冲区存储在我还不太确定的列表或数组中,我不想添加太多线程以致程序冻结所有内容。
Here is my code so far (I'm new to pretty much all the code I have written here, so if anything is bad practice please let me know):
到目前为止,这是我的代码(我在这里编写的几乎所有代码都是新手,所以如果有任何不好的做法,请告诉我):
public partial class MainWindow : Window
{
SerialPort comms;
Thread commThread;
bool flag;
string message;
public MainWindow()
{
InitializeComponent();
comms = new SerialPort();
}
private void PortControl_Click(object sender, RoutedEventArgs e)
{
if (!comms.IsOpen)
{
PortControl.Content = "Stop";
comms.PortName = "COM1";
comms.BaudRate = 9600;
comms.DataBits = 8;
comms.StopBits = StopBits.One;
comms.Parity = Parity.Even;
comms.ReadTimeout = 500;
comms.ReceivedBytesThreshold = 1;
commThread = new Thread(new ThreadStart(Handle));
comms.Open();
comms.DataReceived += new SerialDataReceivedEventHandler(ReadIn);
}
else
{
PortControl.Content = "Start";
flag = false;
comms.DataReceived -= ReadIn;
commThread.Join();
comms.Close();
}
}
private void ReadIn(object sender, SerialDataReceivedEventArgs e)
{
if (!commThread.IsAlive)
{
flag = true;
commThread.Start();
}
}
private void Handle()
{
while (flag)
{
if (comms.IsOpen)
{
try
{
message = comms.ReadByte().ToString("X2");
Dispatcher.BeginInvoke((Action)(() =>
{
ConsoleBox.Text += message + " ";
}));
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
}
采纳答案by user2019047
Here is one solution.
这是一种解决方案。
The serial port is receiving the data in its own thread, and you should read the incoming bytes in the data received handler.
串口在它自己的线程中接收数据,你应该在数据接收处理程序中读取传入的字节。
I propose to read the data and add it to a thread-safe FIFO list in the data received handler and read the data from the list in the main thread.
我建议读取数据并将其添加到数据接收处理程序中的线程安全FIFO列表中,并从主线程中的列表中读取数据。
See my solution in post Serial port reading + Threads or something better?
在Serial port reading + Threads 或更好的帖子中查看我的解决方案?

