本文介绍了读取串口的整个块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,当我点击时,我会向微控制器发送各种代码,并发回一个特殊字符的响应。我的问题是我发送第一个代码,我不知道如何等到我在发送seconde代码之前读取微控制器的全部信息。这是程序:



Hi, I'm writing a program that when I click, I send various codes to a microcontroller and it sends back a response followed by a special character. My problema is that I send the first code, and I don't know how to wait until I read the whole information of the microcontroller before sending the seconde code. Here is the program:

public void PuertoSerieDataReceived(object sender, SerialDataReceivedEventArgs e)
{	
	string input_data = SerialPort.ReadExisting();	
	rtbText.AppendText(input_data);					
}
		
void botonMostrarTramaClick(object sender, EventArgs e)
{			
	if (SerialPort.IsOpen)												
	{
		rtbText.Text = "";
		string string_end = "";	
		
		string code0 = "100567880000110000005181";
		string askForInformation0 = (char)2+ code0.ToString() +(char)4;
		SerialPort.Write(askForInformation0);
		
		while(input_data.EndsWith(string_end) == false)
		{
			
		} 
		
		string code1 = "100567880000110000005181";
		string askForInformation1 = (char)2+ code1.ToString() +(char)4;
		SerialPort.Write(askForInformation1);
		
		while(input_data.EndsWith(string_end) == false)
		{
			
		} 
		
		string code2 = "100567880000110000005181";
		string askForInformation2 = (char)2+ code2.ToString() +(char)4;
		SerialPort.Write(askForInformation2);
		
		while(input_data.EndsWith(string_end) == false)
		{
			
		} 
	}
}

推荐答案

const string m_strEND_SPECIALCHAR = "§"; // whatever your microcontroller defines as terminator char
m_serialport.ReadTo(m_strEND_SPECIALCHAR);





所以你应该创建一个新线程/ click或backgroundworker on click - 发送并等待该线程上的repsonses(可能是尊重超时)(不在你的点击处理程序中)。





OT:构建你的输入字符串



现在:



So you should create a new thread/task or backgroundworker on your click - send and wait for the repsonses on that thread (maybe respect timeouts) ( not in your click handler).


OT: Construct your Input strings

now:

string code1 = "100567880000110000005181";
string askForInformation1 = (char)2+ code1.ToString() +(char)4;





更好:



better:

string GetAskForInformationString(string code)
{
    return String.Format("{0}{1}{2}", (char) 2, code, (char) 4);
}





如果你想采用更高级的方法,可以考虑阅读:

关于WaitHandles和线程同步: []

或任务和延续: []

如何使用asyc / await来清除同步代码: []



我希望这会有所帮助,我建议从简单到更高级,并尝试学习在路上的概念。对于shure,您必须更改代码,以便它不会在UserInterface线程上运行。如果您单击按钮并且发送/接收需要一些时间(可能使用串行端口),您的应用程序将冻结。所以线程的使用是强制性的(线程总是那么复杂)。



亲切的问候



Johannes



If you want to go for a more advanced approach maybe consider reading:
about WaitHandles and thread sync: https://msdn.microsoft.com/en-us/library/ms173179.aspx[^]
or Tasks and Continuations: https://msdn.microsoft.com/en-us/library/ee372288(v=vs.110).aspx[^]
And how to make such async code cleaner with asyc/await: https://msdn.microsoft.com/en-us/library/hh191443.aspx[^]

I hope this helps, I'd recommend go from simple to more advanced and try to learn the concepts "on the way". For shure you have to Change your code, so that it not runs on the UserInterface thread. If you click your button and sending/receiving takes some time (likely with Serial ports) your app will freeze. so use of threads is mandatory (and threading is always Kind of "complicated").

Kind regards

Johannes



这篇关于读取串口的整个块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 00:35