Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working in windows Application. I am using Serial Port (Barcode). I am calling serial Port Data receive event as
_SerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler2);

And In every scan it got fire. The problem is when I scan card for break of 5 seconds its give me data correct, but when I scan before 5 seconds I got duplicate data (old scan data). As I search in internet I found DiscardBufferOut and I implemented it but still the same result.

Code :-
SerialPort _SerialPort1 = new SerialPort();

_SerialPort1.BaudRate = 9600;
_SerialPort1.DataBits = 8;
_SerialPort1.Parity = Parity.None;
_SerialPort1.StopBits = StopBits.One;
_SerialPort1.PortName = "COM3";
_SerialPort1.Handshake = Handshake.None;

_SerialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler1);

_SerialPort1.Open();


Event Implementation
public static void DataReceivedHandler1(object sender, SerialDataReceivedEventArgs e)
{
 string indata = sp.ReadLine();
}
If I scan Card before 5 seconds I get duplicate Data(old scanned data),but If I scan after 5 seconds or 6 seconds I get new data.
If you have any solutions please let me know.


[Agent_Spock]
- Added Code brackets
- Changed Heading of the question
Posted
Updated 16-Jun-14 23:58pm
v3
Comments
OriginalGriff 17-Jun-14 3:32am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Show us the code you are using - to add the handler and the handler code itself, we don't want the whole application.
Use the "Improve question" widget to edit your question and provide better information.
RAHUL(10217975) 17-Jun-14 3:54am    
I updated the question..is this fine?

1 solution

ReadLine is a blocking method: it does not return before a newline code is received from the serial port.
But... The DataRecieved event happens every time that any data is received - so you will get a bunch of them "queued up" all trying to read the same data from the same port. And since DataRecieved is raised on a different thread to the UI thread, it is quite possible that you are sitting in that method many times, all trying to retrieve the same line of data. Nasty.

Don't do that: instead, get all available data in the DataRecieved event, and process it into lines (or other command forms) before passing completed data onward for command processing.
 
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