Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I've made a GUI in WPF to read data via RS232 and.
The only problem is I have to keep pressing
a button to send a 'Receive data' command to my controller
via RS232 so it sends the data back. I want to implement
this in continuously/real time so I don't have to keep
pressing a button to communicate with my device. I've
written while loops and that just freezes my program, i've
also tried a checkbox so if it's checked it gets data and
that also freezes the GUI.

Just wanted to know if there is any solution in C# and wpf

C#
SerialPort sp;
  sp.DataReceived += new SerialDataReceivedEventHandler(receive);


 private void receive(object sender, SerialDataReceivedEventArgs e)
        {
                string data;
                data = sp.ReadExisting();
                MyLog(data);
        }
private void MyLog(string msg)
        {

                this.Dispatcher.Invoke(new DisplayTextDelegate(DisplayText), msg);
        }
        private void DisplayText(string text)
        {
             TextBox2.AppendText(text);
        }

        private void SendButton_Click(object sender, RoutedEventArgs e)
        {

             sp.Write(TextBox1.Text+ "\r\n");

        }
Posted
Updated 16-Mar-12 10:56am
v2

This may be a little more help:

C#
public MainWindow()
{
  InitializeComponent();

  var bw = new BackgroundWorker();
  bw.DoWork += new DoWorkEventHandler(bw_DoWork);
  bw.WorkerSupportsCancellation = true;
  bw.WorkerReportsProgress = true;
  bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
  bw.RunWorkerAsync();

}

void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
  //Update UI here
  var newText = e.UserState;
}

void bw_DoWork(object sender, DoWorkEventArgs e)
{
  var bw = (BackgroundWorker) sender;
  while (!bw.CancellationPending)
  {
    //Do RS-232 stuff here
    var newText = "Put new information here";
    bw.ReportProgress(1, newText);
  }
}
 
Share this answer
 
Comments
getanswer 16-Mar-12 15:49pm    
Hello Clifford Nelson , thx for quick response

here is my code
SerialPort sp;
sp.DataReceived += new SerialDataReceivedEventHandler(receive);


private void receive(object sender, SerialDataReceivedEventArgs e)
{
string data;
data = sp.ReadExisting();
MyLog(data);
}
private void MyLog(string msg)
{

this.Dispatcher.Invoke(new DisplayTextDelegate(DisplayText), msg);
}
private void DisplayText(string text)
{
TextBox2.AppendText(text);
}

private void SendButton_Click(object sender, RoutedEventArgs e)
{

sp.Write(TextBox1.Text+ "\r\n");

}

can you tell me where should i apply these code in BackGroundWorker,thanks
TextBox1 is for sending Data, TextBox2 is receiving data,
Clifford Nelson 16-Mar-12 16:53pm    
Hey. I was looking at the problem now that I have seen the code. I suspect you got hung up because the port was not open. I would recommend that you look at http://www.dreamincode.net/forums/topic/35775-serial-port-communication-in-c%23/ Download the code and see if you can figure what your problem is. I really do not have a setup here to test a serial port.
[no name] 16-Mar-12 18:38pm    
It's not that the port is not open, the problem is that he apparently wants someone to write the code for him. You got my 5 for at least writing that much for him.
getanswer 16-Mar-12 17:08pm    
Hi , i have seen these code already, i tried with these code but i failed, with BackgroundWorker, i dont know which place i have to set my code, i tried but getting some problem, anybody with code please not URL link. thanks
[no name] 16-Mar-12 18:36pm    
"i failed" and "some problem" mean absolutely nothing to anyone trying to help you. What do you mean you don't know which place to put your code? Did you miss the "Do RS232 stuff here" or did you not read the answer?
Use System.ComponentModel.BackgroundWorker and put your code in it's DoWork event.
That way RS232 reading will happen in background and will not block GUI.
Your button (or checkbox) should then start/stop this BackgroungWorker.
There are many examples how to use BackgroundWorker properly and how to update GUI based on it's processing.
 
Share this answer
 
v2
Comments
getanswer 18-Mar-12 8:21am    
Hi all, any updates for my question please, waiting for someone answer....
sjelen 19-Mar-12 10:08am    
Why downvote this solution? It's the same as Clifford's.
Don't downvote something only because you're too lazy to do some research.
getanswer 19-Mar-12 10:32am    
hi sjelen, downvote ,sorry i did not seen that, am not lazy ,i tried my best ..i dont know where am wrong if you know solution please let me know, thanks
sjelen 19-Mar-12 11:48am    
The code under your question is correct. You need to provide more details about your application if you want more help, as Wes Aday said.
Does it work at all? Does it work only once?
Is the device connected to RS232 supposed to send data continuously, or you need to send 'read' command to it every time?
The code inyour question does not match the code in your comment above. Which one do you use?
getanswer 19-Mar-12 11:52am    
hi,i did used BackGroundWorker till now it giving only once receivingdata , so i did If checkbox is checked , while loop , and DispatcherTimer, with this am sending data continuously, but GUI is Freezing,
if(CheckBox.IsChecked)
{
while (sp.BytesToRead > 0)
{
string data = sp.ReadExisting();
this.Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate()
{
textBox1.Text+= data;


}));
}
}

so i want to use with BackGroundWorker Please

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900