i am an engineering student and i am currently working on my final year project. i am trying to build a rfid-based parking lot management system in C#. based on some research i have written the following code to read data from rfid reader but i am facing this problem. the event handler
which i use to read the received data fires twice, which i don't understand how...i did some research on the internet and found many people are facing similar problem of an event handler firing twice but most of them are button-click events. i could only find a few threads on custom event handler but those solutions don't seem to work in my project. any possible solutions to this problem?
private void Parking_layout_Load(object sender, EventArgs e)
{
foreach (string port in System.IO.Ports.SerialPort.GetPortNames())
{
comboBox1.Items.Add(port);
if (comboBox1.Items.Count > 0)
comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
}
RFID = new SerialPort();
RFID.PortName = comboBox1.Text;
RFID.BaudRate = 9600;
RFID.DataBits = 8;
RFID.Parity = Parity.None;
RFID.StopBits = StopBits.One;
RFID.Open();
RFID.DataReceived += new SerialDataReceivedEventHandler(RFID_DataReceived);
}
private void RFID_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
tag_id = RFID.ReadExisting().ToString();
SetLabel(tag_id);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
the RFID_DataReceived executes twice. if i put a messageBox.show() statement to test it, it shows the message twice. how do i fix this problem?
catchblock; all it does is destroy information. - SLakstry { ... } catch () { throw; }or without anytry...catchat all. And I'd be curious to know what happens if you actually remove the event subscription as suggested by Babak Naffas. Have you tried it? I know that RFID scans occur repeatedly as long as the chip is within reach of the RFID reader, have you handled this to filter all those readings as a single one so that you perhaps may filter the raise of the RFID_DataReceived event? - Will Marcouiller