private delegate void SafeWinFormsThreadDelegate(string msg); private void WriteMsg(string msg) { SafeWinFormsThreadDelegate d = new SafeWinFormsThreadDelegate(UpdateUI); Invoke(d, new object[] { msg }); } private void UpdateUI(string msg) { this.tboxRec.Text = msg; } private void btnRec_Click(object sender, EventArgs e) { port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); btnRec.Enabled = false; } void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { int rsLength = 0; bool loop=true; strBuilder.Remove(0, strBuilder.Length); while (loop) { if (port.BytesToRead == rsLength) { loop = false; } else { rsLength = port.BytesToRead; } //Thread.Sleep(100); } byte[] buf = new byte[rsLength]; port.Read(buf, 0, rsLength); int i = 0; foreach (byte b in buf) { if (i > 0 && i % 2 == 0) { strBuilder.Append(" "); } string strByt = b + ""; int intByt = int.Parse(strByt); char charByt = (char)intByt; strBuilder.Append(charByt + ""); i++; } string result = strBuilder.ToString(); WriteMsg(result); }