void sp_DataReceived(object sender, SerialDataReceivedEventArgs e) { Thread.Sleep(500); Char[] buf = new Char[5]; buf[2] = '\0'; int iRet = port.Read(buf,0,2);
string str = new string(buf); Console.WriteLine($"read: {str}"); if (str == "AB") { port.Write("CD"); } // Invokes the delegate on the UI thread, and sends the data that was received to the invoked method. // ---- The "si_DataReceived" method will be executed on the UI thread which allows populating of the textbox. // this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data }); }