I wrote down some app in C# to read the bytes from (USB-SerialPort) payment terminal.
But I can see the bytes are missing/overridden in my application while I read from the read-Buffer of windows serial port.
I have following piece of code for opening the port and reading the bytes( either from ReadExisting or ReadByteArray) from serial port when the DataReceivedEvent gets triggered.
//Open the port
m_port.Open();
//attach the event handler
m_port.ErrorReceived += OnSerialErrorReceived;
m_port.DataReceived += OnSerialDataReceived;
public void OnSerialDataReceived(object sender ,SerialDataReceivedEventArgs serialDataArgs)
{
//Thread.Sleep(20);
//int numberOfBytesToRead = m_port.BytesToRead;
//byte[] readByteArray = new byte[m_port.BytesToRead];
//m_port.Read(readByteArray, 0, readByteArray.Length);
string readData = m_port.ReadExisting();
ParseWLinkProtocolMsgToHexStringArray(Encoding.UTF8.GetBytes(readData));
}
But when I use the Thread.Sleep(20ms) before read the data in OnSerialDataReceived method , I have no missing bytes in my data.
Also I have not set any other properties on the serial port instance except the following. BaudRate,StopBits,Parity and DataBits.
Can some one please suggest me any other alternative way with out applying the time delay in my application.