aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Transport/Components
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2018-07-10 17:10:13 +0300
committerRoy Ben-Shabat <Roy@Twine-s.com>2018-07-10 17:10:13 +0300
commit606db1b64a49a154fa1ba597a455ba96b42aad2b (patch)
tree2a7c76c50dec3459f8b5c74fadac7026cf8d8814 /Software/Visual_Studio/Tango.Transport/Components
parent5d795170b304199383ac967583830acaf313ff05 (diff)
downloadTango-606db1b64a49a154fa1ba597a455ba96b42aad2b.tar.gz
Tango-606db1b64a49a154fa1ba597a455ba96b42aad2b.zip
Implemented auto usb port scanning.
Diffstat (limited to 'Software/Visual_Studio/Tango.Transport/Components')
-rw-r--r--Software/Visual_Studio/Tango.Transport/Components/ComPortEnumerator.cs81
1 files changed, 81 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Transport/Components/ComPortEnumerator.cs b/Software/Visual_Studio/Tango.Transport/Components/ComPortEnumerator.cs
new file mode 100644
index 000000000..7fe67759c
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Transport/Components/ComPortEnumerator.cs
@@ -0,0 +1,81 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Management;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.Transport.Components
+{
+ public static class ComPortEnumerator
+ {
+ private class ProcessConnection
+ {
+ public static ConnectionOptions ProcessConnectionOptions()
+ {
+
+ ConnectionOptions options = new ConnectionOptions();
+ options.Impersonation = ImpersonationLevel.Impersonate;
+ options.Authentication = AuthenticationLevel.Default;
+ options.EnablePrivileges = true;
+ return options;
+ }
+
+ public static ManagementScope ConnectionScope(string machineName, ConnectionOptions options, string path)
+ {
+ ManagementScope connectScope = new ManagementScope();
+ connectScope.Path = new ManagementPath(@"\\" + machineName + path);
+ connectScope.Options = options;
+ connectScope.Connect();
+ return connectScope;
+ }
+ }
+
+ public class ComPortInfo
+ {
+ public string Port { get; set; }
+ public string Description { get; set; }
+
+ public override string ToString()
+ {
+ return String.Format("{0}, {1}", Port, Description);
+ }
+ }
+
+ public static List<ComPortInfo> EnumerateComPorts()
+ {
+ List<ComPortInfo> comPortInfoList = new List<ComPortInfo>();
+
+ ConnectionOptions options = ProcessConnection.ProcessConnectionOptions();
+ ManagementScope connectionScope = ProcessConnection.ConnectionScope(Environment.MachineName, options, @"\root\CIMV2");
+ ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0");
+ ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher(connectionScope, objectQuery);
+
+ using (comPortSearcher)
+ {
+ string caption = null;
+
+ foreach (ManagementObject obj in comPortSearcher.Get())
+ {
+ if (obj != null)
+ {
+ object captionObj = obj["Caption"];
+
+ if (captionObj != null)
+ {
+ caption = captionObj.ToString();
+ if (caption.Contains("(COM"))
+ {
+ ComPortInfo comPortInfo = new ComPortInfo();
+ comPortInfo.Port = caption.Substring(caption.LastIndexOf("(COM")).Replace("(", string.Empty).Replace(")", string.Empty);
+ comPortInfo.Description = caption;
+ comPortInfoList.Add(comPortInfo);
+ }
+ }
+ }
+ }
+ }
+ return comPortInfoList;
+ }
+ }
+}