SerialDataReceivedEventHandler doesnt work

Hello all, I've problems with the serial communication between my Arduino Leonardo and my PC. My C# program can not receive data in text box send by arduino.

here my sketch code

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println("1111111");
delay(1000);
}

C# Code

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 WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
updatePorts(); //Call Update COMPORT
}
private void updatePorts() //COMPORT UPDATE FUCNTION
{
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
_comBox1.Items.Add(port);
}
}
private SerialPort ComPort = new SerialPort(); // Initial Comport variable
private void connect()
{
bool error = false;
//Check All
if(_comBox1.SelectedIndex != -1 & _buadRate.SelectedIndex != -1)
{ //If Currect
ComPort.Handshake = Handshake.RequestToSend;
ComPort.DtrEnable = true;
ComPort.RtsEnable = true;
ComPort.PortName = _comBox1.Text;
ComPort.BaudRate = int.Parse(_buadRate.Text);
ComPort.Parity = Parity.None;
ComPort.DataBits = 8;
ComPort.StopBits = StopBits.One;
try
{
ComPort.Open();
}
catch (UnauthorizedAccessException) { error = true; }
catch (System.IO.IOException) { error = true; }
catch (ArgumentException) { error = true; }

if (error) MessageBox.Show(this, "Could not open the Com port. Maybe is already in use, has been removed, or is unavailable.", "Com port unavailable", MessageBoxButtons.OK, MessageBoxIcon.Stop);

}
else
{
MessageBox.Show("Please select COM Port Settings", "Serial Port Interface", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
//If the port open, change the connect button to disconnect enable the send button.
//Disable the groupBox to prevent changing configuration of an open port.
if (ComPort.IsOpen)
{
_conBUTT.Text = "Disconnect";
//_sendBUTT.Enabled = true;
groupBox1.Enabled = false;
}
}
private void disconnect()
{
ComPort.Close();
_conBUTT.Text = "Connect";
//_sendBUTT.Enabled = false;
groupBox1.Enabled = true;
}

private void _conBUTT_Click(object sender, EventArgs e)
{
if (ComPort.IsOpen)
{
disconnect();
}
else
{
connect();
}
}
private void Form1_FormClosing(object sender, FormClosedEventArgs e)
{
if (ComPort.IsOpen) ComPort.Close();
}

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string recievedData = serialPort1.ReadExisting();
_rtbData.AppendText(recievedData);
}

}
}

Do I missing something in my C# code??. Thank you

Do I missing something in my C# code?

Code tags? 8)

Add a Console.WriteLine() statement to the callback. Is it even being called?

In my code, serial data is read on one thread. The UI runs in another. One thread can't write to another thread's widgets, directly, as you are doing.

Hi xyz1117,

yes you do miss something in your code.

The serialPort1_DataReceived event runs on a different thead.
To communicate with controls on the UI-thread change it to this:

  private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
      {
         string recievedData = serialPort1.ReadExisting();
         
         this.Invoke((MethodInvoker)delegate {
         _rtbData.AppendText(recievedData); // runs on UI thread
         });
      }

If you have more questions DO ask,

Groover