I am using visual C# 2010 windows forms application serial port
object to receive hex data bytes on serial port.
However, I realized that my DataReceived
event is fired twice even though I have read all the data bytes in the buffer and buffer shows 0
bytes to read.
Why and how does that happen?
private void PortData(Object Sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(delegate { DisplayText(); }));
}
Private void DisplayText()
{
int dataLength = serialPort1.BytesToRead;
byte[] data = new byte[dataLength];
serialPort1.Read(data, 0, dataLentgh)
}
I am sending 8 hex bytes on serial port i-e FF FF FB FB FB FF FB which are received in data array by Read Function. But after this PortData function is invoked second time to read 0 bytes. I dont want it to invoke second time.
DisplayText
seems not to display text - it seems to read data from the port so it seems to be really misnamed plus you should do this in the event itself ;) – Random Dev