Hi after reading your code again I saw you are doing it all on one thread. so a more simple solution is possible. Just call read like this:
const string m_strEND_SPECIALCHAR = "§";
m_serialport.ReadTo(m_strEND_SPECIALCHAR);
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);
}
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