Click here to Skip to main content
15,903,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In a class I want to read data from serial port.
There is a string value(PortData) in which I want to store all data received from port when I click on a button and at the end when all data is received,I want the PortData value to be shown in a textbox.
C#
public class pro
{
   StringBuilder PortData = new StringBuilder();


 private void btnOpenPort_Click(object sender, EventArgs e)
 {
                mySerialPort.DataReceived += new                 SerialDataReceivedEventHandler(DataReceivedHandler);
                try
                {
                    mySerialPort.Open();
                    txbShowFile.Text = PortData.ToString();
                }
 }
 private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            PortData.Append(sp.ReadExisting());
        }
}

the problem is after receiving data portdata shows the first value of variable. it means null value.
Posted
Updated 29-Aug-15 2:34am
v2

1 solution

Just because you have opened a port, doesn;t mean that any dtaa has been received - indeed it's very, very unlikely that you will get data immediately, because serial ports are slow - 9600 baud means 9600 bits per second data transfer, which equates to roughly 1000 characters per second. So unless you processor is spectacularly slow, i't going to take at least 1 millisecond before a character is ready for you.

And remember that when you start looking at the actual data: you will probably get an event for each character, rather than a single event for the whole message.

You need to use the DataRecieved event to tell your UI that data is available - but remember that it happens on a different thread, so you will need to invoke any controls you want to use with the handler.

And BTW: it's a very bad idea to add a new handler every time you open the port - they do accumulate, so if you click the button twice, you will get two handlers per character received, and so on.
 
Share this answer
 
Comments
mit62 29-Aug-15 9:28am    
when I use a breakpoint on DataReceivedHandler , the portdata gets data and everything is ok but at the end when process back to this line "txbShowFile.Text = PortData.ToString();"
portdata is null
OriginalGriff 29-Aug-15 9:52am    
"portdata is null"
No, it isn't.
If it was, you would get a "Null reference exception" and I know you don't because
1) You would have mentioned that.
And
2) You assign an instance to the variable when you create it.

So what you mean is "it's empty".
Now, if you read what I said, it explains why...
mit62 29-Aug-15 9:34am    
and about this problem you mentioned "it's a very bad idea to add a new handler every time you open the port - they do accumulate, so if you click the button twice, you will get two handlers per character received, and so on."
what can I do?
OriginalGriff 29-Aug-15 9:53am    
Don't attach the handler in the Click event - do it in the class constructor instead - that only happens once per instance of your class, and hence once per instance of the mySerialPort variable.
mit62 29-Aug-15 14:04pm    
thank you but if I assign a value to the string builder:
StringBuilder PortData = new StringBuilder("Initiated Value");
and then in run time I trace Portdata value in event handler, it takes data from serial port and the value of PortData will chang but at the end in this line:
txbShowFile.Text = PortData.ToString();
PortData shows this value:"Initiated Value"

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