From 5d795170b304199383ac967583830acaf313ff05 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Tue, 10 Jul 2018 15:24:06 +0300 Subject: Some work on PPC. Implemented retrieval of column description for auto generated observables & pmr's. --- .../MachineStudio/Tango.MachineStudio.UI/Resources/BuildDate.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI') diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Resources/BuildDate.txt b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Resources/BuildDate.txt index f49ef650e..d3d1ae9e8 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Resources/BuildDate.txt +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Resources/BuildDate.txt @@ -1 +1 @@ -Mon 07/09/2018 16:27:01.58 +Tue 07/10/2018 15:17:20.87 -- cgit v1.3.1 From 606db1b64a49a154fa1ba597a455ba96b42aad2b Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Tue, 10 Jul 2018 17:10:13 +0300 Subject: Implemented auto usb port scanning. --- .../Tango.MachineStudio.UI/Resources/BuildDate.txt | 2 +- .../PPC/Tango.PPC.Common/PPCSettings.cs | 7 ++ .../Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs | 23 ++++ .../PPCApplication/DefaultPPCApplicationManager.cs | 10 +- .../Tango.Integration/Services/ComPortInfo.cs | 74 ------------ .../Services/ExternalBridgeScanner.cs | 2 +- .../Tango.Integration/Tango.Integration.csproj | 1 - .../Tango.PMR/Diagnostics/EventType.cs | 10 +- .../Tango.PMR/Hardware/HardwareMotor.cs | 3 + .../Components/ComPortEnumerator.cs | 81 +++++++++++++ .../Discovery/CommunicationScannerResult.cs | 22 ++++ .../Discovery/ICommunicationScanner.cs | 26 ++++ .../Discovery/UsbCommunicationScanner.cs | 132 +++++++++++++++++++++ .../Tango.Transport/Tango.Transport.csproj | 5 + .../Tango.Transport/TransporterBase.cs | 36 ++++-- 15 files changed, 339 insertions(+), 95 deletions(-) delete mode 100644 Software/Visual_Studio/Tango.Integration/Services/ComPortInfo.cs create mode 100644 Software/Visual_Studio/Tango.Transport/Components/ComPortEnumerator.cs create mode 100644 Software/Visual_Studio/Tango.Transport/Discovery/CommunicationScannerResult.cs create mode 100644 Software/Visual_Studio/Tango.Transport/Discovery/ICommunicationScanner.cs create mode 100644 Software/Visual_Studio/Tango.Transport/Discovery/UsbCommunicationScanner.cs (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI') diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Resources/BuildDate.txt b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Resources/BuildDate.txt index d3d1ae9e8..00990166f 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Resources/BuildDate.txt +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Resources/BuildDate.txt @@ -1 +1 @@ -Tue 07/10/2018 15:17:20.87 +Tue 07/10/2018 16:54:33.30 diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs index 2e7af8844..5ed4c35d9 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCSettings.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tango.Logging; using Tango.Settings; namespace Tango.PPC.Common @@ -18,11 +19,17 @@ namespace Tango.PPC.Common /// public String MachineSerialNumber { get; set; } + /// + /// Gets or sets the logging categories. + /// + public List LoggingCategories { get; set; } + /// /// Initializes a new instance of the class. /// public PPCSettings() { + LoggingCategories = new List(); MachineSerialNumber = "1111"; } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs index 4b4c7acb9..57c627bfb 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/App.xaml.cs @@ -9,6 +9,8 @@ using System.Windows; using Tango.BL; using Tango.Core; using Tango.Core.Helpers; +using Tango.Logging; +using Tango.PPC.Common; using Tango.Settings; namespace Tango.PPC.UI @@ -18,13 +20,34 @@ namespace Tango.PPC.UI /// public partial class App : Application { + private LogManager LogManager = LogManager.Default; + /// /// Raises the event. /// /// A that contains the event data. protected override void OnStartup(StartupEventArgs e) { + LogManager.RegisterLogger(new VSOutputLogger()); + LogManager.RegisterLogger(new FileLogger()); + + LogManager.Log("Application Started..."); + base.OnStartup(e); + + LogManager.Categories.Clear(); + + var settings = SettingsManager.Default.GetOrCreate(); + + if (settings.LoggingCategories.Count == 0) + { + settings.LoggingCategories.Add(LogCategory.Critical); + settings.LoggingCategories.Add(LogCategory.Error); + settings.LoggingCategories.Add(LogCategory.Info); + settings.LoggingCategories.Add(LogCategory.Warning); + } + + LogManager.Categories.AddRange(settings.LoggingCategories); } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs index c9d0ab35c..4978fa6e0 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs @@ -17,6 +17,7 @@ using Tango.Core.Helpers; using Tango.PPC.Common.Modules; using System.Windows.Threading; using Tango.Transport.Adapters; +using Tango.PMR.Connection; namespace Tango.PPC.UI.PPCApplication { @@ -192,9 +193,12 @@ namespace Tango.PPC.UI.PPCApplication private async void ConnectToMachine() { - //var machine = new MachineOperator(new UsbTransportAdapter("COM3", UsbSerialBaudRates.BR_9600)); - //await machine.Connect(); - //ConnectedMachine = machine; + Transport.Discovery.UsbCommunicationScanner scanner = new Transport.Discovery.UsbCommunicationScanner(UsbSerialBaudRates.BR_9600); + var response = await scanner.Scan(new ConnectRequest() { Password = "1234" }, TimeSpan.FromSeconds(60)); + + var machine = new MachineOperator(response.Adapter); + await machine.Connect(); + ConnectedMachine = machine; } } } diff --git a/Software/Visual_Studio/Tango.Integration/Services/ComPortInfo.cs b/Software/Visual_Studio/Tango.Integration/Services/ComPortInfo.cs deleted file mode 100644 index 7ab116f67..000000000 --- a/Software/Visual_Studio/Tango.Integration/Services/ComPortInfo.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Management; -using System.Text; -using System.Threading.Tasks; - -namespace Tango.Integration.Services -{ - public class COMPortInfo - { - 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 string Port { get; set; } - public string Description { get; set; } - public COMPortInfo() { } - - public static List EnumerateComPorts() - { - List comPortInfoList = new List(); - - 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; - } - } -} diff --git a/Software/Visual_Studio/Tango.Integration/Services/ExternalBridgeScanner.cs b/Software/Visual_Studio/Tango.Integration/Services/ExternalBridgeScanner.cs index ae3edb8cf..8aca73e46 100644 --- a/Software/Visual_Studio/Tango.Integration/Services/ExternalBridgeScanner.cs +++ b/Software/Visual_Studio/Tango.Integration/Services/ExternalBridgeScanner.cs @@ -102,7 +102,7 @@ namespace Tango.Integration.Services { while (IsStarted) { - foreach (var device in COMPortInfo.EnumerateComPorts()) + foreach (var device in Transport.Components.ComPortEnumerator.EnumerateComPorts()) { if (device.Description.Contains(_settings.EmbeddedDeviceName) || !_settings.FilterExternalBridgeUsbMachines) { diff --git a/Software/Visual_Studio/Tango.Integration/Tango.Integration.csproj b/Software/Visual_Studio/Tango.Integration/Tango.Integration.csproj index 59711fdfb..85a857c72 100644 --- a/Software/Visual_Studio/Tango.Integration/Tango.Integration.csproj +++ b/Software/Visual_Studio/Tango.Integration/Tango.Integration.csproj @@ -91,7 +91,6 @@ - diff --git a/Software/Visual_Studio/Tango.PMR/Diagnostics/EventType.cs b/Software/Visual_Studio/Tango.PMR/Diagnostics/EventType.cs index a6b8e1776..7777381a8 100644 --- a/Software/Visual_Studio/Tango.PMR/Diagnostics/EventType.cs +++ b/Software/Visual_Studio/Tango.PMR/Diagnostics/EventType.cs @@ -22,7 +22,7 @@ namespace Tango.PMR.Diagnostics { static EventTypeReflection() { byte[] descriptorData = global::System.Convert.FromBase64String( string.Concat( - "Cg9FdmVudFR5cGUucHJvdG8SFVRhbmdvLlBNUi5EaWFnbm9zdGljcyrHBwoJ", + "Cg9FdmVudFR5cGUucHJvdG8SFVRhbmdvLlBNUi5EaWFnbm9zdGljcyrWBwoJ", "RXZlbnRUeXBlEg8KC1RocmVhZEJyZWFrEAASHwobVGhyZWFkVGVuc2lvbkNv", "bnRyb2xGYWlsdXJlEAESGgoWRmVlZGVyQ29uZUluc3VmZmljaWFudBACEhYK", "EldpbmRlckdlbmVyYWxFcnJvchADEhcKE1dpbmRlckNvbmVOb3RFeGlzdHMQ", @@ -44,8 +44,8 @@ namespace Tango.PMR.Diagnostics { "aWxlZBAfEhgKFEFwcGxpY2F0aW9uRXhjZXB0aW9uECASGgoWQXBwbGljYXRp", "b25JbmZvcm1hdGlvbhAhEhYKEkFwcGxpY2F0aW9uU3RhcnRlZBAiEhkKFUFw", "cGxpY2F0aW9uVGVybWluYXRlZBAjEhQKEFJlY29yZGluZ1N0YXJ0ZWQQJBIU", - "ChBSZWNvcmRpbmdTdG9wcGVkECVCIQofY29tLnR3aW5lLnRhbmdvLnBtci5k", - "aWFnbm9zdGljc2IGcHJvdG8z")); + "ChBSZWNvcmRpbmdTdG9wcGVkECUSDQoJSm9iU3RhdHVzECZCIQofY29tLnR3", + "aW5lLnRhbmdvLnBtci5kaWFnbm9zdGljc2IGcHJvdG8z")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, new pbr::FileDescriptor[] { }, new pbr::GeneratedClrTypeInfo(new[] {typeof(global::Tango.PMR.Diagnostics.EventType), }, null)); @@ -207,6 +207,10 @@ namespace Tango.PMR.Diagnostics { ///Occures when a diagnostics recording has been stopped (Group = Application, Category = Info, Actions = ) /// [pbr::OriginalName("RecordingStopped")] RecordingStopped = 37, + /// + ///Occures when a job status message has been received from the embedded device (Group = Application, Category = Info, Actions = ) + /// + [pbr::OriginalName("JobStatus")] JobStatus = 38, } #endregion diff --git a/Software/Visual_Studio/Tango.PMR/Hardware/HardwareMotor.cs b/Software/Visual_Studio/Tango.PMR/Hardware/HardwareMotor.cs index 101559b58..bbf37f866 100644 --- a/Software/Visual_Studio/Tango.PMR/Hardware/HardwareMotor.cs +++ b/Software/Visual_Studio/Tango.PMR/Hardware/HardwareMotor.cs @@ -371,6 +371,9 @@ namespace Tango.PMR.Hardware { /// Field number for the "FsSpd" field. public const int FsSpdFieldNumber = 25; private int fsSpd_; + /// + ///The speed in which the motor moves to full step operation. + /// [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public int FsSpd { get { return fsSpd_; } 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 EnumerateComPorts() + { + List comPortInfoList = new List(); + + 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; + } + } +} diff --git a/Software/Visual_Studio/Tango.Transport/Discovery/CommunicationScannerResult.cs b/Software/Visual_Studio/Tango.Transport/Discovery/CommunicationScannerResult.cs new file mode 100644 index 000000000..860e5293c --- /dev/null +++ b/Software/Visual_Studio/Tango.Transport/Discovery/CommunicationScannerResult.cs @@ -0,0 +1,22 @@ +using Google.Protobuf; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Transport.Discovery +{ + public class CommunicationScannerResult where TAdapter : ITransportAdapter where TResponse : IMessage + { + /// + /// Gets or sets the adapter. + /// + public TAdapter Adapter { get; set; } + + /// + /// Gets or sets the response. + /// + public TResponse Response { get; set; } + } +} diff --git a/Software/Visual_Studio/Tango.Transport/Discovery/ICommunicationScanner.cs b/Software/Visual_Studio/Tango.Transport/Discovery/ICommunicationScanner.cs new file mode 100644 index 000000000..bdf27d4df --- /dev/null +++ b/Software/Visual_Studio/Tango.Transport/Discovery/ICommunicationScanner.cs @@ -0,0 +1,26 @@ +using Google.Protobuf; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Transport.Discovery +{ + /// + /// Represents a communication scanner. + /// + /// The type of the adapter. + /// The type of the request. + /// The type of the response. + public interface ICommunicationScanner where TAdapter : ITransportAdapter where TRequest : IMessage where TResponse : IMessage + { + /// + /// Scans the environment with the specified request while expecting the type of response. + /// + /// The request. + /// The timeout. + /// + Task> Scan(TRequest request, TimeSpan timeout); + } +} diff --git a/Software/Visual_Studio/Tango.Transport/Discovery/UsbCommunicationScanner.cs b/Software/Visual_Studio/Tango.Transport/Discovery/UsbCommunicationScanner.cs new file mode 100644 index 000000000..9707d523a --- /dev/null +++ b/Software/Visual_Studio/Tango.Transport/Discovery/UsbCommunicationScanner.cs @@ -0,0 +1,132 @@ +using Google.Protobuf; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Management; +using System.Text; +using System.Threading.Tasks; +using Tango.Logging; +using Tango.Transport.Adapters; +using Tango.Transport.Components; +using Tango.Transport.Transporters; + +namespace Tango.Transport.Discovery +{ + /// + /// Represents a USB port scanner which will scan all known ports on PC with the specified request and response types. + /// + /// The type of the request. + /// The type of the response. + /// + public class UsbCommunicationScanner : ICommunicationScanner where TRequest : IMessage where TResponse : IMessage + { + /// + /// Gets the baud rate. + /// + public UsbSerialBaudRates BaudRate { get; private set; } + + /// + /// Initializes a new instance of the class. + /// + /// The baud rate. + public UsbCommunicationScanner(UsbSerialBaudRates baudRate) + { + BaudRate = baudRate; + } + + /// + /// Scans the environment with the specified request while expecting the type of response. + /// + /// The request. + /// The timeout. + /// + public Task> Scan(TRequest request, TimeSpan timeout) + { + var logManager = LogManager.Default; + + logManager.Log("Starting com ports scanning using baud rate " + BaudRate.ToInt32() + "..."); + + DateTime startTime = DateTime.Now; + bool found = false; + + TaskCompletionSource> source = new TaskCompletionSource>(); + + Task.Factory.StartNew(async () => + { + CommunicationScannerResult result = new CommunicationScannerResult(); + + logManager.Log("Enumerating com ports..."); + var comPorts = ComPortEnumerator.EnumerateComPorts(); + + while (!found && DateTime.Now < startTime.Add(timeout)) + { + foreach (var port in comPorts) + { + if (DateTime.Now > startTime.Add(timeout)) + { + break; + } + + logManager.Log("Scanning " + port.ToString() + "..."); + + BasicTransporter transporter = new BasicTransporter(new UsbTransportAdapter(port.Port, BaudRate)); + + try + { + logManager.Log("Connecting transporter..."); + await transporter.Connect(); + logManager.Log("Sending scanning request..."); + var response = await transporter.SendRequest(request, TimeSpan.FromSeconds(0.5)); + + if (response is TResponse) + { + logManager.Log("Response received from " + port.ToString()); + + ITransportAdapter adapter = null; + + try + { + adapter = transporter.Adapter; + logManager.Log("Disconnecting transporter... (keeping adapter)"); + transporter.Adapter = null; + await transporter.Disconnect(); + } + catch { } + + found = true; + + logManager.Log("Setting scan task result."); + source.SetResult(new CommunicationScannerResult() + { + Adapter = adapter as UsbTransportAdapter, + Response = (TResponse)response, + }); + + break; + } + + logManager.Log("Disconnecting transporter..."); + await transporter.Disconnect(); + } + catch (Exception ex) + { + try + { + logManager.Log(ex, "Disconnecting transporter..."); + await transporter.Disconnect(); + } + catch { } + } + } + } + + if (!found) + { + source.SetException(new TimeoutException(logManager.Log("The com port scanning operation had timed out after " + timeout.TotalSeconds + " seconds."))); + } + }); + + return source.Task; + } + } +} diff --git a/Software/Visual_Studio/Tango.Transport/Tango.Transport.csproj b/Software/Visual_Studio/Tango.Transport/Tango.Transport.csproj index 1ac3bf41e..5045b7e43 100644 --- a/Software/Visual_Studio/Tango.Transport/Tango.Transport.csproj +++ b/Software/Visual_Studio/Tango.Transport/Tango.Transport.csproj @@ -36,6 +36,7 @@ + ..\packages\System.Reactive.Core.3.1.1\lib\net46\System.Reactive.Core.dll @@ -67,12 +68,16 @@ + + + + diff --git a/Software/Visual_Studio/Tango.Transport/TransporterBase.cs b/Software/Visual_Studio/Tango.Transport/TransporterBase.cs index 7dabd76c3..4abedce27 100644 --- a/Software/Visual_Studio/Tango.Transport/TransporterBase.cs +++ b/Software/Visual_Studio/Tango.Transport/TransporterBase.cs @@ -56,7 +56,11 @@ namespace Tango.Transport public ITransportAdapter Adapter { get { return _adapter; } - set { _adapter = value; OnAdapterChanged(value); } + set + { + var previous = _adapter; + _adapter = value; OnAdapterChanged(previous, value); + } } /// @@ -118,25 +122,33 @@ namespace Tango.Transport /// /// Called when the has changed. /// - /// The adapter. - protected async virtual void OnAdapterChanged(ITransportAdapter adapter) + /// The adapter. + protected async virtual void OnAdapterChanged(ITransportAdapter oldAdapter, ITransportAdapter newAdapter) { - LogManager.Log("Adapter Changed:"); + if (oldAdapter != null) + { + oldAdapter.StateChanged -= OnAdapterStateChanged; + oldAdapter.DataAvailable -= OnAdapterDataAvailable; + } - if (adapter != null) + if (newAdapter != null) { - LogManager.Log(adapter.GetType().Name + ", " + adapter.Address + ", " + adapter.State.ToString()); + LogManager.Log(String.Format("Adapter Changed: Type = {0}, Address = {1}, State = {2}", newAdapter.GetType().Name, newAdapter.Address, newAdapter.State)); - adapter.StateChanged -= OnAdapterStateChanged; - adapter.StateChanged += OnAdapterStateChanged; - adapter.DataAvailable -= OnAdapterDataAvailable; - adapter.DataAvailable += OnAdapterDataAvailable; + newAdapter.StateChanged -= OnAdapterStateChanged; + newAdapter.DataAvailable -= OnAdapterDataAvailable; + newAdapter.StateChanged += OnAdapterStateChanged; + newAdapter.DataAvailable += OnAdapterDataAvailable; - if (State == TransportComponentState.Connected && adapter.State == TransportComponentState.Disconnected) + if (State == TransportComponentState.Connected && newAdapter.State == TransportComponentState.Disconnected) { - await adapter.Connect(); + await newAdapter.Connect(); } } + else + { + LogManager.Log("Adapter Changed: null"); + } } /// -- cgit v1.3.1