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!");
}
}