1

I need to get data from the a Lidar under 100ms. This data consists of 1122 bytes and I receive this data when i sends 10 Byte request data to Lidar. Communication is via RS422. My computer has Rs422 port. it can support up to 5M baudrate. I get this data by reading the serial port in C#. Lidar i use can support up to 500k baudrate.

Here is my question; Although I set BaudRate to 115200,230400,460800, I can't get below 190ms for all read operations. I don't know where am i wrong. Is there a way programmatic way to do this faster?

on avarage For 115200-> 210ms,

For 230400-> 200ms,

For 460800-> 190ms

My get data function code;

public static void GetData() 
 {  
      System.Diagnostics.Stopwatch watch;

            if (isLidarStart)
            {
                serialport.DiscardInBuffer();
                serialport.DiscardOutBuffer();

                sendByte(ScanDataRequreBytes);
                int byteslen;
                watch = new System.Diagnostics.Stopwatch();
                long timeOut1 = 220; // for protection
                watch.Start();
                byteslen = serialport.BytesToRead;

                while (watch.ElapsedMilliseconds < timeOut1 && byteslen != 1122)
                {
                    byteslen = serialport.BytesToRead;
                }

                watch.Stop();
                System.Diagnostics.Debug.WriteLine("Time elapsed only when receiving data = " + watch.ElapsedMilliseconds + " ms");

                if (byteslen != 1122)
                {
                    System.Diagnostics.Debug.WriteLine("Error  = " + serialport.BytesToRead);
                    return null;
                }
                else
                {
                    byte[] SerialPortbytes = new byte[byteslen];

                    serialport.Read(SerialPortbytes, 0, byteslen);

                   // Do something
                 }

        }

My init Serial port function code;

  public static void initSerialPortCommunication()
        {
            try
            {
                serialport = new SerialPort();
                serialport.BaudRate = 460800;
                serialport.PortName = "COM2";
                serialport.Parity = Parity.None;
                serialport.StopBits = StopBits.One;
                serialport.DataBits = 8;
                serialport.Open();
                isLidarStart = true;
            }
            catch (Exception)
            {
                ErrorOccured = false;
                MessageBox.Show("Error starting port!");
            }
      }
  • Can you add some details on the serial port type? USB to serial converters (such as FTDI) have internal buffers that are handled in polling by the OS. Read time is not guaranteed (depends on polling time of the USB buffers). See https://www.ftdichip.com/Support/Documents/TechnicalNotes/TN_103_FTDI_USB_Data_Transfer_Efficiency(FT_000097).pdf – Fab Jul 27 '19 at 12:09
  • Use ReadAsync instead of Read. The Asynchronous method will support faster data rates while the Read will not. Discovered this using a 8080 PC in 1980 and it still applies today. – jdweng Jul 27 '19 at 12:49
  • For example, this article will be helpful. [SerialPort.BaseStream.ReadAsync drops or scrambles bytes when reading from a USB Serial Port](https://stackoverflow.com/q/41777981/9014308) – kunif Jul 27 '19 at 12:57
  • Yeah I know this is old, but did you try pipelining and flushing requests? – Jeremy Lakeman Jul 06 '22 at 01:04

1 Answers1

-2

Maybe it is a limitation of the port settings. Device Manager > Port Properties > Port Setting > Advanced...

advanced port setting

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77