1 Replies - 1805 Views - Last Post: 18 March 2012 - 06:48 AM Rate Topic: -----

#1 ma123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-March 12

When data received from RS232, we can see it in a textbox

Posted 18 March 2012 - 02:11 AM

Hello. I can read data from RS232 but I want to when data received, display it in a textbox.
My code is here:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace AVR_to_RS232
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            textBox1.Text = serialPort1.ReadExisting();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
        }
    }
}


But in "serialPort1_DataReceived" event I can't insert "textBox1.Text = serialPort1.ReadExisting();" because the program display an error. Here is the error:
"Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on."
What can i do.

MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 18 March 2012 - 03:49 AM


Is This A Good Question/Topic? 0
  • +

Replies To: When data received from RS232, we can see it in a textbox

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: When data received from RS232, we can see it in a textbox

Posted 18 March 2012 - 06:48 AM

Serial port tutorial help/links can be found in FAQ # 11. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Mar 2012
Spoiler




How to make thread-safe calls to WinForm controls.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1