2

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.

Raj
  • 151
  • 1
  • 14
  • 1
    Well you could try to read with a buffer (manually reading the data into a `byte[]`) instead of with `ReadExisting` but I don't know if this will solve your issue. See the second part of [this answer](https://stackoverflow.com/a/13755084/10883465). – Joelius Jul 25 '19 at 09:28
  • I have tried the read byte ( which has been commented out in my post) method which also gives me the same issue if I do not put the delay. – Raj Jul 25 '19 at 09:33
  • You should mention that in the question :) – Joelius Jul 25 '19 at 09:34
  • Sorry I just have edited my previous post and Thanks for your feedback. – Raj Jul 25 '19 at 09:39
  • the answer from the link posted by @Joelius addresses your problem exactly. It doesn't just use port.Read(byte[]) to read the data, but queries it, and then you read from the query. This approach will work if 1) your data packages have fixed length or if 2) you have some delimiter character(s) that you could use to determine when the package ends. Otherwise you're stuck with the delay method. – Arie Jul 25 '19 at 10:12
  • You have to design and program with consideration that one data can be notified separately and it can happen that multiple data are notified at once. For example, you can use the way of thinking. [Parsing/formatting data from serial port - C#](https://stackoverflow.com/q/9505197/9014308) – kunif Jul 25 '19 at 11:16
  • The issue has been solved in the Firmware code by sending protocol message (Header+payload) as one chunk of data than sending them individually. So I can avoid multiple EventRaised in my C# application for data-received instead the FW will send one eventRaised for each protocol frame. – Raj Jul 25 '19 at 15:16

0 Answers0