7 Replies - 938 Views - Last Post: 30 July 2014 - 05:43 PM Rate Topic: -----

#1 makinen456   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-July 14

One thread trying to pass data to another thread using Serial Port

Posted 30 July 2014 - 08:05 AM

For the past 2 days I'm stuck on something but without solution.

I have a class which I wrote and one of its object is "SerialPort" .NET class.
In my MainWindow I created instance of my class called "SerialPortComm", then I send through some functions of mine, commands to the Serial Port, and I receive answers through "DataReceived" event.

But when I trying to use Dispatcher.BeginInvoke to write my data I have received (successfully), nothing shows on the RichTextBox which I'm trying to write to.

What can caused that, and How I can make it works?

SerialPortComm.cs

public partial class SerialPortComm : UserControl
{
   public SerialPort mySerialPort = new SerialPort();

   public void Open_Port(string comNumber, int baudRate)
   {
      mySerialPort.PortName = comNumber;
      mySerialPort.BaudRate = baudRate;
      mySerialPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
      mySerialPort.Open();
   }

   public void SetStringDataFromControl(SerialPort sp, string content)
   {
      sp.Write(content + "\n");
   }

   public void SetStringDataFromControl(SerialPort sp, string content)
   {
      sp.Write(content + "\n");
   }

   public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
   {
      SerialPort sp = (SerialPort)sender;
      DataRX = sp.ReadExisting(); // Read the data from the Serial Port
      // Print it on the Log
      RichTextBox_logView.Dispatcher.BeginInvoke((Action)delegate()
      {
          RichTextBox_logView.AppendText(DataRX);
          RichTextBox_logView.ScrollToEnd();
      });
   }
}


Commands.cs

class Commands
{
   public void SetCommand(SerialPortComm sp, string command)
   {
       sp.SetStringDataFromControl(sp.mySerialPort, command); 
   }
}



Mainwindow.cs

public partial class MainWindow : Window
{

   Commands cmd = new Commands();
   SerialPortComm sp1 = new SerialPortComm();  

   public MainWindow()
   {
       InitializeComponent();
       sp1.Open_Port("COM6", 115200);
   }

   private async void TextBox_input_KeyDown(object sender, KeyEventArgs e)
   {
       if (e.Key == Key.Enter)
       {
           cmd.SetCommand(sp1, "top");
           cmd.SetCommand(sp1, "run");
           // .... //
       }
   }
}


Is This A Good Question/Topic? 0
  • +

Replies To: One thread trying to pass data to another thread using Serial Port

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: One thread trying to pass data to another thread using Serial Port

Posted 30 July 2014 - 08:28 AM

That is just a really screwy way to be doing it.
Don't try to micro manage the richtextbox like that.

First, I would use a ListView not a richtextbox. That way one line is one entry.

You should just have a collection in your code for the entries:
List<string> LogEntries;


Bind that collection to the DataSource of the ListView.

Then when you add to your collection it will automatically update the ListView.

This separates the code behind from the UI. The logic C# will run regardless of having a UI or not.

See the tutorial in my signature block for "WPF for the WinForms developer - Part 1" - it does a lot of explanation for binding.
Was This Post Helpful? 1
  • +
  • -

#3 makinen456   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-July 14

Re: One thread trying to pass data to another thread using Serial Port

Posted 30 July 2014 - 09:36 AM

Maybe its not the perfect way to do that, but this piece of code is not mine, and I need some time to sit and write all this from scratch with binding and in more efficient way.

But for now, I need it to work, and I will glad to solutions for that specific problem.
(e.g: your guide is nice and helpful, I will read it later).
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: One thread trying to pass data to another thread using Serial Port

Posted 30 July 2014 - 10:29 AM

Don't be mad when I say

tlhIn`toq said:

Making band-aides to fix really poor design issues is not something I promote or participate in.


Maybe someone else has a suggestion of a patch for you. But I tend to avoid it because it always comes back to bite me (and the OP) on the arse. Generally what happens is the OP never goes back to fix it, then later says "I did the same thing in these 10 other places and I've coded myself into a corner. Now what can I patch?".

** AND ** some other reader will take the suggestion given as a recommendation that they in turn will do the same crappy coding and want help with it saying "Well you recommended doing it this way, back on another thread."

This post has been edited by tlhIn`toq: 30 July 2014 - 10:30 AM

Was This Post Helpful? 0
  • +
  • -

#5 makinen456   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-July 14

Re: One thread trying to pass data to another thread using Serial Port

Posted 30 July 2014 - 10:53 AM

I appreciate your opinion, thank you.
But it still not helping me.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: One thread trying to pass data to another thread using Serial Port

Posted 30 July 2014 - 01:45 PM

I don't know how to reply to that. Maybe its a language barrier issue.
Let me try it another way:

You don't want to do it right (at least for now), and I for one won't promote doing it wrong. "No time to do it right, always time to do it over" is not good.

Not sure what else you can expect/want.
Was This Post Helpful? 0
  • +
  • -

#7 makinen456   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-July 14

Re: One thread trying to pass data to another thread using Serial Port

Posted 30 July 2014 - 01:53 PM

OK!
So patch here is not a good idea. fine.
What can you recommend me to do, or how I should implements in a right way, a program which will know to work simultaneously with several Serial Port COMs, and for every COM I will have a Log Screen for its own?

From where you recommend me to begin?
again, Thanks for your time and help.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: One thread trying to pass data to another thread using Serial Port

Posted 30 July 2014 - 05:43 PM

I already told you where to look. We have tutorials for this sort of thing. We don't repeat all that information for each and every post. You need to work through the tutorials in order to better understand the foundation concepts that you are missing.

View PosttlhIn`toq, on 30 July 2014 - 09:28 AM, said:

See the tutorial in my signature block for "WPF for the WinForms developer - Part 1" - it does a lot of explanation for binding.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1