Click here to Skip to main content
11,581,025 members (65,868 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SerialPorts
Hi All,
I am having a bit of trouble with some serial port reading. I am reading the data using ReadExisting() but once it is read it appears to remain on the Port until cleared using the following in a seperate button click

ComPort.DiscardInBuffer();
 ComPort.DiscardOutBuffer();
 
  rtbIncomingData.Text = "";
If I manually click the button it clears the relavent buffers and rich textboxes and works. My thinking was "thats a bit odd, but got a get around" and I called the click routine from the read port function this did not work. so I copied the code out of the function and tried again, still not working. My thinking was this was too quick for the data to be there. If I use a standard Windows timer from the tool box, with a 1 Sec period this appears to 'cure' the issue but I have been bitten by these timers before and have sent code to clients that "Works on my Machine" but not the customers at the moment I am trying to avoid this. Any one any ideas, suggestions, please??
Posted 12-Dec-12 0:23am
Comments
lukeer at 12-Dec-12 7:31am
   
Where did you get the information that the data is still there, even after you have read it?
glennPattonWork at 12-Dec-12 8:07am
   
I know its wrong, I know the data isn't there but if I try to read it I get the previous reply. I think it is because it's still in the buffer ComPort buffers if I manually clear them it's fine I think. Glenn
lukeer at 13-Dec-12 3:16am
   
So it's two consecutive reads that return the same data?
Perhaps the data is indeed received twice. Try a null-modem cable to another COM port so you have full control over what is being sent. Then check the receiving port again.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,

Indeed, that is bit odd. Buffers should be cleaned automatically when reading is done.

Anyway, try Thread.Sleep(100) after reading. I think 100ms should be enough.

Regards.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
Thanks for that I think I have now worked out why. It's a Half Duplex System, I need to get my software to send and recieve from it. I wasn't expecting that. Any ideas....
Glenn
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

If you read the documentation on the ReadExisting function, you'll see there is a warning that bytes could get left in the internal buffer because of the way that function works. MSDN Library Link Here[^]

If the data coming in is terminated with a CR or CR/LF, you can use ReadLine instead... which will block until it sees the CR character on the input stream. Most devices will terminate their communications with a CR automatically. If you can tolerate a possible delay or handle a situation where you may not get a CR (by specifying a timeout on that function and handling the exception) then this is one of the best functions to use (in my opinion anyway). It returns essentially a single "message" from the device without worrying about detecting the CR/Length/etc.

Alternately, you can query the serial port on a regular basis looking for characters to be present by checking the BytesToRead property. If that is greater than 0, use ReadByte, ReadChar, or ReadTo depending on your encoding and needs.

Honestly, I'm not sure why ReadExisting is even available. I guess for high-speed applications where you might need the contents of the buffer and the underlying stream, you might need something like this, but for general serial port work, you should probably avoid it. The other "read" functions work only on the buffer which should be sufficient for nearly everything and avoids the complexity of dealing with both the buffer and the stream.

I've done a lot of serial port work over the years. If you have any more questions, shoot me a message and I'll try to give you some help.

Jason
  Permalink  

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

  Print Answers RSS
0 Frankie-C 195
1 OriginalGriff 184
2 F-ES Sitecore 80
3 Andy Lanng 75
4 Afzaal Ahmad Zeeshan 75
0 OriginalGriff 1,108
1 Sergey Alexandrovich Kryukov 785
2 Abhinav S 622
3 F-ES Sitecore 500
4 Suvendu Shekhar Giri 433


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100