using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO.Ports; namespace data_receivedhandler_gui { public partial class Form1 : Form { SerialPort port = new SerialPort("com4", 115200); public Form1() { InitializeComponent(); } private void b_open_Click(object sender, EventArgs e) { port.Open(); } private void serialPort1_DataReceived(object sender,System.IO.Ports.SerialDataReceivedEventArgs e) { try { if (InvokeRequired) { Invoke(new MethodInvoker(DoSerialInput)); } } catch { } } private void DoSerialInput() { try { textBox1.AppendText(port.ReadExisting()); } catch { return; } } private void b_close_Click(object sender, EventArgs e) { port.Close(); } } }