public void BeginSearch(int sendInterval = 100) { EndSearch(); lock (searchSyncLock) { ahoyServiceInfoList.Clear(); shouldSearch = true; searchComplete.Reset(); searchThread = new Thread(delegate() { Connection imuConnection = null; string serialNumber; SerialConnectionInfo serialConnectionInfo = new SerialConnectionInfo() { PortName = portName, BaudRate = 115200, RtsCtsEnabled = false, }; try { do { //Thread.CurrentThread.Join(100); try { imuConnection = new Connection(serialConnectionInfo); imuConnection.Connect(); } catch { imuConnection?.Dispose(); imuConnection = null; } }while (shouldSearch == true && imuConnection == null); if (shouldSearch == false) { return; } do { //Thread.CurrentThread.Join(100); //SerialConnectionInfo info; //string serialNumber; //if (TryConnection(portName, 115200, false, sendInterval, 1, out info, out serialNumber) == false) //{ // continue; //} try { if (Commands.Send(imuConnection, Command.Ahoy, sendInterval, 1, out serialNumber) != CommunicationProcessResult.Success) { continue; } } catch { continue; } AhoyServiceInfo serviceInfo = new AhoyServiceInfo(IPAddress.Any, IPAddress.Any, 0, 0, serialNumber, string.Empty, new object[] { serialConnectionInfo }, 0); if (ahoyServiceInfoList.Contains(serviceInfo) == true) { continue; } ahoyServiceInfoList.Add(serviceInfo); ServiceDiscovered?.Invoke(serviceInfo); SerialDeviceDiscovered?.Invoke(serialNumber, serialConnectionInfo); return; }while (shouldSearch == true); } finally { imuConnection?.Dispose(); searchComplete.Set(); } }); searchThread.Name = "Ahoy Scan Port " + portName; searchThread.Start(); } }
private void OnSerialDeviceDiscovered(string serialNumber, SerialConnectionInfo serialConnectionInfo) { SerialDeviceDiscovered?.Invoke(serialNumber, serialConnectionInfo); }