Click here to Skip to main content
15,900,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am develop window application for reading serial port data.

Here is my code for reading serial port:
C#
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    try
    {
        if (comPort.IsOpen == true)//comPort is serial port which i use in my project
        {
            bytes = comPort.BytesToRead;
            byte[] comBuffer = new byte[bytes];
            comPort.Read(comBuffer, 0, bytes);
        }
    }
    catch (Exception ex)
    {
    }
}

My problem is when i connect two computer and send data from one computer to my software i am not receive complete data.But when i put break point and this event and check then it show complete data and also display complete data but without break point it not show complete data means lost many data.
Please solve this problem.

[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
Posted
Updated 9-Dec-11 0:38am
v2

You will get the DataRecieved event several times: potentially for each character as it arrives. If you read the bytes as you are, and add them to a buffer rather than reading them into a single buffer and throwing it away at the end of the event, you can parse the input to spot a complete message, and use the Timeout to detect when a receive has failed.
 
Share this answer
 
Comments
jaideepsinh 9-Dec-11 7:32am    
Sorry but i am not clear in this can you please give your suggestion with comparing my code which i wrote here means at which code i change and what i write in my code.
Thank's for this.
OriginalGriff 9-Dec-11 8:30am    
No, I'm not going to write it for you! :laugh:
Basically, set up a class level array (of suitable size) or a List, and add the data to that when you read it. Then you parse the data from that. So if your message was always 5 characters "[abc]" and it arrived in two events, the first could load three characters, and the second the remaining two. You then have a complete message to parse and work on.
jaideepsinh 9-Dec-11 9:43am    
I understand what you are saying but there is not fix how many byte is receive at a time any size of byte comes at any time when port is open.
And thank's for explain in brief.
Depending on the data you're receiving, SerialPort.ReadExisting() might solve your problem.

Quote:
Reads all immediately available bytes, based on the encoding, in both the stream and the input buffer of the SerialPort object.


Read this at MSDN:
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readexisting.aspx[^]
 
Share this answer
 
Comments
jaideepsinh 9-Dec-11 7:42am    
I can't use ReadExisting() with byte[] it give's error.
Michel [mjbohn] 9-Dec-11 8:03am    
That's correct. ReadExisting() returns string. Have a look at this string. Maybe it's the data you need and you can process this string.

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