Click here to Skip to main content
15,891,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to display something when I receive a data. Could u tell me how to fix my code? I think invoking new EventHandler doesn't work correctly.
Thanks!

What I have tried:

C#
private void button_Click(object sender, RoutedEventArgs e)
{
    try
    {
        if (!sPort.IsOpen)
        {
            sPort.PortName = comboBox.Text;
            sPort.BaudRate = 9600;
            sPort.Parity = Parity.None;
            sPort.DataBits = 8;
            sPort.StopBits = StopBits.One;

            sPort.Open();

            sPort.DataReceived += sPort_DataReceived;
        }
    }
    catch { }
}

private void sPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    Dispatcher.Invoke(new EventHandler(DisplayData));
}

private void DisplayData(object o, EventArgs e)
{
    Example_label.Content = "received";
}
Posted
Updated 19-Jul-16 21:48pm

Quote:
I need to display something when I receive a data. Could u tell me how to fix my code? I think invoking new EventHandler doesn't work correctly.

There is nothing to fix, you need to rewrite your code.
But first of all you need to understand how serial port is working. It is slooooow, very sloooooow, do not expect your data to wait in buffer instantly after opening the port.
Serial Comms in C# for Beginners[^]
C#
sPort.DataReceived += sPort_DataReceived;

The serial port object is not a storage space for the data, there is a buffer, but you can't use is to store some data.
Serial Port Communication Tutorial With C# Examples - CodeSamplez.com[^]
 
Share this answer
 
Comments
Member 12643399 20-Jul-16 9:34am    
Well, there must be something wrong with this code because when the data comes I get "The application is in break mode". Accualy I know how it's working with WinForm, but it looks like WPF works a bit different?
Use working examples as a starting point wherever possible. This shows how to read from a SerialPort:
SerialPort.DataReceived Event (System.IO.Ports)[^]
The order in which you set up and open the port is crucial (instantiate, set parameters and attach datareceived handler). Your example has many errors and a little background reading may be necessary. This example should get you on the right track: Simple Serial for Microsoft Visual C# Express[^] also: c# - How to Read and Write from the Serial Port - Stack Overflow[^]

Your application should be designed to buffer the incoming data (which may occur at any time). This data may then be used as needed by other parts of your application.

[edit following reply]
See: Serial Communication using WPF, RS232 and PIC Communication[^]
 
Share this answer
 
v4
Comments
Member 12643399 20-Jul-16 10:21am    
OK, so i changed "sPort.DataReceived += sPort_DataReceived;" => "sPort.DataReceived += new SerialDataReceivedEventHandler(sPort_DataReceived);" => "private static void sPort_DataReceived(object sender, SerialDataReceivedEventArgs e)", and I see that changing label content or "Dispatcher.Invoke(new EventHandler(DisplayData));" can't be used in this way beacause "An object reference is required for the nonstatic field, method, or property ..." so how do I fix this problem? Second and third link didn't help me, those are WinForm examples and my code worked there :(
[no name] 20-Jul-16 10:39am    
No need for static. See my edit and please learn to use Google. I cannot comment any further because I can't see your code.
Member 12643399 20-Jul-16 12:31pm    
Yea, I saw that one but thought there is a better way and I left that. However now it seems to work fine, so thank you :)
You tagged your question with "WPF", but your code looks more like Windows Forms. Use a ViewModel with properties bound to the UI. The PropertyChanged event will automagically be sent to the UI thread, so you avoid all cross-threading issues (DataReceived happens always in a different thread).
And you should also find out when you won't receive any further bytes (look at the protocol for your device) to change the status message again.
 
Share this answer
 

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