want to show serial data in rich text box

Abdul Hayee

Member
Joined
Mar 31, 2020
Messages
24
Programming Experience
Beginner
Hi
I am a beginner in .Net(Visual C #) platform. Basically i am an electronic engineer and having microcontroller programming experience.
I have to do some programming in C# which send and receive some data from hardware. For this purpose i am writing a program in C# to make a desktop application.
Somehow i wrote a program which is sending the data from C#. Now i want to receive data in C# but i am getting error in it.
Kindly tell me about its solution.
The error i am getting is
csharp-1.png


Here i want to show the received data
csharp-2.png


waiting for your replies

Thanks
 
The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread.
Here's a general article about it: Make thread-safe calls to controls - Windows Forms

I find lambdas convenient for creating inline delegated methods to be invoked, for example:
C#:
Action appendText = () => richTextBox1.AppendText("something");
richTextBox1.Invoke(appendText);
 
Do you understand why you need to delegate, and why invoking is required? If the control wasn't created on the thread where your code is running, you won't be able to access those controls. (Hence the error: cross-thread operation not valid). You can only access controls from the same thread as they were created on. Otherwise invoke will need to be called.

Delegating puts you back on talking terms with your UI where the control was originally created. You could almost consider it like creating a bridge to your UI when you use either Action or a Delegate to pass your method back to the control.
 
One could also say that instead of current method (the event handler) runs code to access the control (which is not allowed), you let the control run that code in its own context. Invoke is how the control does that, and the delegate is the reference to the method to call. Action is just a predefined delegate in .Net libraries.
 
EventHandler is a delegate, and your DisplayText is a method - so what's different?
 
You set a field strRx outside the method before invoking, and read this field in the invoked method. That is not necessary and opens for vulnerability, it is not good object-oriented code, the value should be passed directly.
With the inline method that I posted the input string is passed directly to the method, it can be done because it is inline. A variation is to use a method and delegate with an input parameter, and pass the argument to the Invoke call. Here's an example where the Action<T> delegate is used:
C#:
Action<string> appendText = (txt) => richTextBox1.AppendText(txt);
richTextBox1.Invoke(appendText, "something");
 
OK Thanks JohnH.
As i worked in MicroController programming in C from years. I never ever used .Net, C++,C# etc. So could you recommend me some website or books from which i can learn these basic things. I know there are lot of material available from google but which one to follow.

Thanks
 
 
there are lot of material available from google but which one to follow.
Almost none of them. If you want to learn, then read it from Microsofts dev network only.

Try this example. It's the same as what John is showing you, just done differently.
C#:
        public Form1()
        {
            InitializeComponent();

        }
        public void ExecuteThis()
        {
            /* Start a new task and run it */
            Thread new_Thread = new Thread(Execute_Non_UI);
            new_Thread.Start();
        }

        public delegate void Call_Back_To(object s);

        private void Update_UI(object this_Object)
        {
            /* I am talking with your UI, and I can update your UI with the new value. */
            textBox1.Text = (string)this_Object;
        }
        private void Execute_Non_UI()
        {
            /* I am on a new thread, (the non UI thread) */
            /* Invoke the control, by calling on the delegation of the Update_UI method followed by the parameter value from the Update_UI method. */
            textBox1.Invoke(new Call_Back_To(Update_UI), "5150"); /* Whatever object type you pass will need to be cast or converted in the Update_UI method. */
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ExecuteThis();
        }

I've updated the example, so as not to run into handle not created yet error.
 
Last edited:
There is also a getting started tutorial in @jmcilhinney signature. Check that and the Link that JohnH posted. Stay away from youtube and any other video related tutorials unless they are from Microsoft themselves.

Also, to stay on the initial topic, and rather than just dumping you into the beginner documentation. Since you seem to have a relevant and basic understanding of what you're being told to do. So, I'll assume you should understand know what you need to do, and how and why our examples work?

If you're working with Micro-Controllers, this shouldn't be to hard to grasp.

If there is something you don't understand regarding what we are telling you. Ask us.
 
Back
Top Bottom