0

I have a device that is connected to the computer with a USB cable (Prolific USB to Serial ), I use the following code to read data

(code taken from https://www.vgies.com/a-reliable-serial-port-in-c/)

       public class ReliableSerialPort : SerialPort
       {
       :
       :
         private void ContinuousRead()
         {
          byte[] buffer = new byte[4096];
          Action kickoffRead = null;
          kickoffRead = () =>
          {
             try
             {
                 BaseStream.BeginRead(buffer, 0, buffer.Length, delegate (IAsyncResult ar)
                {
                    try
                    {
                        int count = BaseStream.EndRead(ar);
                        byte[] dst = new byte[count];
                        Buffer.BlockCopy(buffer, 0, dst, 0, count);
                        OnDataReceived(dst);
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine($"OptimizedSerialPort exception!\r\n {exception.ToString()}");
                    }

                    kickoffRead();
                }, null);
            }
            catch (Exception e)
            {
                //When disconnecting while accessing BaseStream an InvalidOperationException is raised, we ignore it
            }
        };

        kickoffRead();
       
    }

Every few seconds I also send a message to the device.

After disconnecting the cable from the device (the cable stays connected to the computer) I still get data!!!

i.e. the line int count = BaseStream.EndRead (ar); Returns a number greater than 0.

the data is the message I sent.

What could be the reason?

Tomer Dror
  • 81
  • 5
  • To be exact, which connector in the board photo of the reference article is the point you disconnected? Is it a USB connector? Or is it an RS232C pin connector? – kunif Aug 17 '21 at 08:00
  • I am not using the board, just the code, I am using other device and i am disconnecting the cable from the device. the device has TX, RX and GND pins – Tomer Dror Aug 17 '21 at 16:46
  • Even if you don't use the article board, you should be using a conceptual equivalent. It is important that what you call a cable has a USB serial conversion chip such as FTDI built into it. As a phenomenon, is it set to perform some loopback operation? – kunif Aug 17 '21 at 17:51
  • Regarding the loopback operation, not that I know, can I test it with some software? – Tomer Dror Aug 18 '21 at 09:11
  • For example, are you using RS485 or a product that can perform loopback/echoback by itself with the intention of RS232C? It is written that `Supports Terminal / ECHO mode by mode selector` with such a product. [USB to RS-422/485 Adapter UC485](https://www.aten.com/us/en/products/usb-&-thunderbolt/usb-converters/uc485/) Please read the user's manual of the USB serial conversion cable you are using carefully. [SIO (Smart-IO) > USB to UART/Serial/Printer](http://www.prolific.com.tw/US/ShowProduct.aspx?pcid=41&showlevel=0017-0037-0041) – kunif Aug 18 '21 at 17:17

0 Answers0