Skip to main content
Added missing )
Source Link
001
  • 963
  • 6
  • 13

You are closing the port immediately after you open it. You need to keep it open while you are expecting data. Remove the line

serialPort1.Close();

Then, in the callback you try to access the GUI from the callback thread. That's not allowed. Use Invoke().

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
     SerialPort sp = (SerialPort)sender;
     //string indata = sp.ReadExisting();
     string indata = sp.ReadLine();
     //textBox1.Text = indata;
     Invoke(new Action<string>(
         (s) => 
         {
             textBox1.Text = s;
         }
     ), indata);
}

You are closing the port immediately after you open it. You need to keep it open while you are expecting data. Remove the line

serialPort1.Close();

Then, in the callback you try to access the GUI from the callback thread. That's not allowed. Use Invoke().

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
     SerialPort sp = (SerialPort)sender;
     string indata = sp.ReadExisting();
     //textBox1.Text = indata;
     Invoke(new Action<string>(
         (s) => 
         {
             textBox1.Text = s;
         }
     , indata);
}

You are closing the port immediately after you open it. You need to keep it open while you are expecting data. Remove the line

serialPort1.Close();

Then, in the callback you try to access the GUI from the callback thread. That's not allowed. Use Invoke().

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
     SerialPort sp = (SerialPort)sender;
     //string indata = sp.ReadExisting();
     string indata = sp.ReadLine();
     //textBox1.Text = indata;
     Invoke(new Action<string>(
         (s) => 
         {
             textBox1.Text = s;
         }
     ), indata);
}
Added link to MSDN page.
Source Link
001
  • 963
  • 6
  • 13

You are closing the port immediately after you open it. You need to keep it open while you are expecting data. Remove the line

serialPort1.Close();

Then, in the callback you try to access the GUI from the callback thread. That's not allowed. Use Invoke()Invoke().

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
     SerialPort sp = (SerialPort)sender;
     string indata = sp.ReadExisting();
     //textBox1.Text = indata;
     Invoke(new Action<string>(
         (s) => 
         {
             textBox1.Text = s;
         }
     , indata);
}

You are closing the port immediately after you open it. You need to keep it open while you are expecting data. Remove the line

serialPort1.Close();

Then, in the callback you try to access the GUI from the callback thread. That's not allowed. Use Invoke().

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
     SerialPort sp = (SerialPort)sender;
     string indata = sp.ReadExisting();
     //textBox1.Text = indata;
     Invoke(new Action<string>(
         (s) => 
         {
             textBox1.Text = s;
         }
     , indata);
}

You are closing the port immediately after you open it. You need to keep it open while you are expecting data. Remove the line

serialPort1.Close();

Then, in the callback you try to access the GUI from the callback thread. That's not allowed. Use Invoke().

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
     SerialPort sp = (SerialPort)sender;
     string indata = sp.ReadExisting();
     //textBox1.Text = indata;
     Invoke(new Action<string>(
         (s) => 
         {
             textBox1.Text = s;
         }
     , indata);
}
Source Link
001
  • 963
  • 6
  • 13

You are closing the port immediately after you open it. You need to keep it open while you are expecting data. Remove the line

serialPort1.Close();

Then, in the callback you try to access the GUI from the callback thread. That's not allowed. Use Invoke().

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
     SerialPort sp = (SerialPort)sender;
     string indata = sp.ReadExisting();
     //textBox1.Text = indata;
     Invoke(new Action<string>(
         (s) => 
         {
             textBox1.Text = s;
         }
     , indata);
}