Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I am develop serial port receive data software in window application.
My software working fine when i operate it manually but when i connect it with device.
I am using this code to receive data:
C#
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {

               if (comPort.IsOpen == true)
               {
                   //Thread.Sleep(150);
                   bytes = comPort.BytesToRead;
                   comBuffer = new byte[bytes];
                   comPort.Read(comBuffer, 0, bytes);
                   string mm = ByteArrayToHex(comBuffer);
               }
        }

I am using timer for other use and function and i set it to 1ms. But when i use Thread.Sleep(150); then it will work proper so it means i have to wait for some timer to read complete data. How i do it without using Thread.Sleep.
Posted
Updated 6-Feb-12 22:39pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Feb-12 4:35am    
Not clear. It depends what's on the other side of your RS-232 cable. :-)
--SA

1 solution

The problem is probably that you are assuming that when you get the DataReceived event all the data is ready for you - this is not necessarily true. You will typically get multiple DataReceived events - one for each character, as they take time to arrive as a bit stream. When the first character has completely arrived, you will get the first event. Adding Thread.Sleep allows the other chatracters time to arrive, and be available to read as a stream.

Instead of processing just the characters that arrive each time, you need to store them, and look at the complete stream, probably by looking for a terminator or bytes count of some kind - it depends on teh device at the other end of the wire, and how it talks to your PC.

[edit]Typos - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
jaideepsinh 7-Feb-12 5:18am    
First Thank You for reply.
Can you explain with some code because first time i develop this type of application.
OriginalGriff 7-Feb-12 5:26am    
:laugh:
Not really, no - I have no idea what device you are talking to, or what the data looks like!
glennPattonWork3 7-Feb-12 5:26am    
Hmmm, have you attached comPort_DataRecieved event argument, simple I know but it stumped me the first time too.
jaideepsinh 7-Feb-12 5:29am    
But if i wait for some ms then it completely receive.Is it right way to use Thread.sleep or other function work delay if i use Thread.sleep.
OriginalGriff 7-Feb-12 5:48am    
No - it is nearly always a bad idea to use Thread.Sleep, and always bad on the default thread - every time you use Thread.Sleep you are preventing anything else (such as the user keyboard or mouse) from doing anything.

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