/// <summary> /// Scans ports for x-IMUs. /// </summary> /// <param name="isAsync"> /// Enables OnAsyncScanProgressChanged event for use when called within background worker. /// </param> /// <returns> /// Array of <see cref="PortAssignment"/> found during the scan. /// </returns> private PortAssignment[] DoScan(bool isAsync) { List <PortAssignment> portAssignmentsList = new List <PortAssignment>(); do { // Get list of available port names List <string> portNames = xIMUserial.GetPortNames().ToList(); if (portNames.Count == 0) { if (DontGiveUp) { if (isAsync) { OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(0, "No ports found. Retry in 1 second...")); } Thread.Sleep(1000); } else { if (isAsync) { OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(0, "No ports found.")); } break; } } else { int progressPercent = 0; string progressMessage; // Augment list with other potential port names as Bluetooth port names may have an extra numerical character appended int originalCount = portNames.Count; for (int i = 0; i < originalCount; i++) { if (portNames[i].Length == "COMxx".Length) { string altPortName = portNames[i].Substring(0, "COMx".Length); if (!portNames.Contains(altPortName)) { portNames.Add(altPortName); } } if (portNames[i].Length == "COMxxx".Length) { string altPortName = portNames[i].Substring(0, "COMxx".Length); if (!portNames.Contains(altPortName)) { portNames.Add(altPortName); } } } // Print port names progressMessage = "Found ports: "; for (int i = 0; i < portNames.Count; i++) { progressMessage += portNames[i] + (i < (portNames.Count - 1) ? ", " : "."); } if (isAsync) { progressPercent = (int)(Math.Round(100d * (1d / (1d + (3d * (portNames.Count + 1)))))); OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(progressPercent, progressMessage)); } // Scan for x-IMU on each port for (int i = 0; i < portNames.Count; i++) { xIMUserial xIMUserialobj = null; try { // Open serial port if (CancellationPending) { break; } if (isAsync) { progressPercent = (int)(Math.Round(100d * ((1d + (i * 3d) + 1d) / (1d + (3d * (portNames.Count + 1)))))); OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(progressPercent, "Opening " + portNames[i] + "...")); } xIMUserialobj = new xIMUserial(portNames[i]); xIMUserialobj.RegisterDataReceived += new x_IMU_API.xIMUserial.onRegisterDataReceived(xIMUserialobj_RegisterDataReceived); xIMUserialobj.Open(); // Read device ID if (CancellationPending) { break; } if (isAsync) { progressPercent = (int)(Math.Round(100d * ((1d + (i * 3d) + 2d) / (1d + (3d * (portNames.Count + 1)))))); OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(progressPercent, "Reading device ID...")); } receivedDeviceID = null; xIMUserialobj.SendReadRegisterPacket(new RegisterData(RegisterAddresses.DeviceID)); Thread.Sleep(500); if (receivedDeviceID == null) { throw new Exception("No response."); } // Found x-IMU if (CancellationPending) { break; } if (isAsync) { progressPercent = (int)(Math.Round(100d * ((1d + (i * 3d) + 3d) / (1d + (3d * (portNames.Count + 1)))))); OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(progressPercent, "Found x-IMU " + receivedDeviceID + ".")); } portAssignmentsList.Add(new PortAssignment(portNames[i], receivedDeviceID)); if (FirstResultOnly) { break; } } catch (Exception ex) { if (isAsync) { OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(progressPercent, "Failed: " + ex.Message)); } } finally { if (xIMUserialobj != null) { xIMUserialobj.Close(); } } } } } while ((portAssignmentsList.Count == 0) && DontGiveUp && !CancellationPending); return(portAssignmentsList.ToArray()); }
/// <summary> /// Scans ports for x-IMUs. /// </summary> /// <param name="isAsync"> /// Enables OnAsyncScanProgressChanged event for use when called within background worker. /// </param> /// <returns> /// Array of <see cref="PortAssignment"/> found during the scan. /// </returns> private PortAssignment[] DoScan(bool isAsync) { List<PortAssignment> portAssignmentsList = new List<PortAssignment>(); do { // Get list of available port names List<string> portNames = xIMUserial.GetPortNames().ToList(); if (portNames.Count == 0) { if (DontGiveUp) { if (isAsync) { OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(0, "No ports found. Retry in 1 second...")); } Thread.Sleep(1000); } else { if (isAsync) { OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(0, "No ports found.")); } break; } } else { int progressPercent = 0; string progressMessage; // Augment list with other potential port names as Bluetooth port names may have an extra numerical character appended int originalCount = portNames.Count; for (int i = 0; i < originalCount; i++) { if (portNames[i].Length == "COMxx".Length) { string altPortName = portNames[i].Substring(0, "COMx".Length); if (!portNames.Contains(altPortName)) { portNames.Add(altPortName); } } if (portNames[i].Length == "COMxxx".Length) { string altPortName = portNames[i].Substring(0, "COMxx".Length); if (!portNames.Contains(altPortName)) { portNames.Add(altPortName); } } } // Print port names progressMessage = "Found ports: "; for (int i = 0; i < portNames.Count; i++) { progressMessage += portNames[i] + (i < (portNames.Count - 1) ? ", " : "."); } if (isAsync) { progressPercent = (int)(Math.Round(100d * (1d / (1d + (3d * (portNames.Count + 1)))))); OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(progressPercent, progressMessage)); } // Scan for x-IMU on each port for (int i = 0; i < portNames.Count; i++) { xIMUserial xIMUserialobj = null; try { // Open serial port if (CancellationPending) { break; } if (isAsync) { progressPercent = (int)(Math.Round(100d * ((1d + (i * 3d) + 1d) / (1d + (3d * (portNames.Count + 1)))))); OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(progressPercent, "Opening " + portNames[i] + "...")); } xIMUserialobj = new xIMUserial(portNames[i]); xIMUserialobj.RegisterDataReceived += new x_IMU_API.xIMUserial.onRegisterDataReceived(xIMUserialobj_RegisterDataReceived); xIMUserialobj.Open(); // Read device ID if (CancellationPending) { break; } if (isAsync) { progressPercent = (int)(Math.Round(100d * ((1d + (i * 3d) + 2d) / (1d + (3d * (portNames.Count + 1)))))); OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(progressPercent, "Reading device ID...")); } receivedDeviceID = null; xIMUserialobj.SendReadRegisterPacket(new RegisterData(RegisterAddresses.DeviceID)); Thread.Sleep(500); if (receivedDeviceID == null) { throw new Exception("No response."); } // Found x-IMU if (CancellationPending) { break; } if (isAsync) { progressPercent = (int)(Math.Round(100d * ((1d + (i * 3d) + 3d) / (1d + (3d * (portNames.Count + 1)))))); OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(progressPercent, "Found x-IMU " + receivedDeviceID + ".")); } portAssignmentsList.Add(new PortAssignment(portNames[i], receivedDeviceID)); if (FirstResultOnly) { break; } } catch (Exception ex) { if (isAsync) { OnAsyncScanProgressChanged(new AsyncScanProgressChangedEventArgs(progressPercent, "Failed: " + ex.Message)); } } finally { if (xIMUserialobj != null) { xIMUserialobj.Close(); } } } } } while ((portAssignmentsList.Count == 0) && DontGiveUp && !CancellationPending); return portAssignmentsList.ToArray(); }