diff --git a/BOB/App.xaml.cs b/BOB/App.xaml.cs index 536f904..1fb808a 100644 --- a/BOB/App.xaml.cs +++ b/BOB/App.xaml.cs @@ -56,8 +56,8 @@ namespace BOB containerRegistry.RegisterDialog("DeviceSetting"); //注册全局变量 containerRegistry.RegisterSingleton(); - containerRegistry.RegisterSingleton(); containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); } protected override void OnExit(ExitEventArgs e) { diff --git a/BOB/Models/DeviceModel.cs b/BOB/Models/DeviceModel.cs deleted file mode 100644 index c231f5a..0000000 --- a/BOB/Models/DeviceModel.cs +++ /dev/null @@ -1,52 +0,0 @@ -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection.Metadata; -using System.Text; -using System.Threading.Tasks; - -namespace BOB.Models -{ - public class DeviceModel - { - public DeviceModel() - { - - } - - public DeviceModel(DeviceModel source) - { - ID = source.ID; - ParameterID = source.ParameterID; - Name = source.Name; - Connected = source.Connected; - ErrorMessage = source.ErrorMessage; - Type = source.Type; - ConnectString = source.ConnectString; - CommunicationProtocol = source.CommunicationProtocol; - Description = source.Description; - } - - public Guid ID { get; set; } = Guid.NewGuid(); - - public Guid ParameterID { get; set; } - - public string Name { get; set; } = ""; - - [JsonIgnore] - public bool Connected { get; set; } - - [JsonIgnore] - public string? ErrorMessage { get; set; } - - public string Type { get; set; } = "Tcp"; - - public string ConnectString { get; set; } = ""; - - [JsonIgnore] - public object? CommunicationProtocol { get; set; } - - public string Description { get; set; } = ""; - } -} diff --git a/BOB/Models/ProgramModel.cs b/BOB/Models/ProgramModel.cs index b7df03c..3aff52c 100644 --- a/BOB/Models/ProgramModel.cs +++ b/BOB/Models/ProgramModel.cs @@ -18,7 +18,6 @@ namespace BOB.Models { ID = source.ID; StepCollection = new ObservableCollection(source.StepCollection.Select(p => new StepModel(p))); - Devices = new ObservableCollection(source.Devices.Select(p => new DeviceModel(p))); Parameters = new ObservableCollection(source.Parameters.Select(p => new ParameterModel(p))); } @@ -40,11 +39,6 @@ namespace BOB.Models set => SetProperty(ref _parameters, value); } - private ObservableCollection _devices = new ObservableCollection(); - public ObservableCollection Devices - { - get => _devices; - set => SetProperty(ref _devices, value); - } + } } diff --git a/BOB/Singleton/Devices.cs b/BOB/Singleton/Devices.cs index b74681e..ac5b903 100644 --- a/BOB/Singleton/Devices.cs +++ b/BOB/Singleton/Devices.cs @@ -21,11 +21,9 @@ namespace BOB.Singleton private readonly ProxyGenerator _proxyGen = new ProxyGenerator(); private readonly IInterceptor _loggingInterceptor = new LoggingInterceptor(); - // 保留对单个实例引用(可选) - private ITcp E36233ADevice_1 { get; set; } - private ITcp E36233ADevice_2 { get; set; } - private ISerialPort IT6724CDevice_1 { get; set; } - private ISerialPort IT6724CDevice_2 { get; set; } + private ITcp E36233ADevice { get; set; } + private ISerialPort IT6724CDevice { get; set; } + private ISerialPort IT6724CReverseDevice{ get; set; } private IModbusDevice EAEL9080Device { get; set; } private IModbusDevice IOBoardevice { get; set; } private IModbusDevice LQ7500_DDevice { get; set; } @@ -45,23 +43,11 @@ namespace BOB.Singleton case "DeviceCommand.Device.E36233A": if (device.CommunicationConfig is TcpConfig tcp1) { - if (E36233ADevice_1 == null) - { - // 通过接口代理 - E36233ADevice_1 = _proxyGen.CreateInterfaceProxyWithTarget( + E36233ADevice = _proxyGen.CreateInterfaceProxyWithTarget( new E36233A(tcp1.IPAddress, tcp1.Port, tcp1.ReadTimeout, tcp1.WriteTimeout), _loggingInterceptor ); - DeviceDic["E36233A_1"] = E36233ADevice_1; - } - else - { - E36233ADevice_2 = _proxyGen.CreateInterfaceProxyWithTarget( - new E36233A(tcp1.IPAddress, tcp1.Port, tcp1.ReadTimeout, tcp1.WriteTimeout), - _loggingInterceptor - ); - DeviceDic["E36233A_2"] = E36233ADevice_2; - } + DeviceDic["E36233A"] = E36233ADevice; } else throw new InvalidOperationException("E36233A 必须使用 TcpConfig"); break; @@ -69,22 +55,23 @@ namespace BOB.Singleton case "DeviceCommand.Device.IT6724C": if (device.CommunicationConfig is SerialPortConfig sp1) { - if (IT6724CDevice_1 == null) - { - IT6724CDevice_1 = _proxyGen.CreateInterfaceProxyWithTarget( - new IT6724C(sp1.COMPort, sp1.BaudRate, sp1.DataBit, sp1.StopBit, sp1.ParityBit, sp1.ReadTimeout, sp1.WriteTimeout), - _loggingInterceptor - ); - DeviceDic["IT6724C_1"] = IT6724CDevice_1; - } - else - { - IT6724CDevice_2 = _proxyGen.CreateInterfaceProxyWithTarget( - new IT6724C(sp1.COMPort, sp1.BaudRate, sp1.DataBit, sp1.StopBit, sp1.ParityBit, sp1.ReadTimeout, sp1.WriteTimeout), - _loggingInterceptor - ); - DeviceDic["IT6724C_2"] = IT6724CDevice_2; - } + IT6724CDevice = _proxyGen.CreateInterfaceProxyWithTarget( + new IT6724C(sp1.COMPort, sp1.BaudRate, sp1.DataBit, sp1.StopBit, sp1.ParityBit, sp1.ReadTimeout, sp1.WriteTimeout), + _loggingInterceptor + ); + DeviceDic["IT6724C"] = IT6724CDevice; + } + else throw new InvalidOperationException("IT6724C 必须使用 SerialPortConfig"); + break; + + case "DeviceCommand.Device.IT6724CReverse": + if (device.CommunicationConfig is SerialPortConfig sp3) + { + IT6724CReverseDevice = _proxyGen.CreateInterfaceProxyWithTarget( + new IT6724C(sp3.COMPort, sp3.BaudRate, sp3.DataBit, sp3.StopBit, sp3.ParityBit, sp3.ReadTimeout, sp3.WriteTimeout), + _loggingInterceptor + ); + DeviceDic["IT6724CReverse"] = IT6724CReverseDevice; } else throw new InvalidOperationException("IT6724C 必须使用 SerialPortConfig"); break; diff --git a/BOB/Singleton/GlobalVariables.cs b/BOB/Singleton/GlobalVariables.cs index b2a310f..3bfb740 100644 --- a/BOB/Singleton/GlobalVariables.cs +++ b/BOB/Singleton/GlobalVariables.cs @@ -24,6 +24,5 @@ namespace BOB public PackIconKind RunIcon { get; set; } = PackIconKind.Play; public StepModel SelectedStep { get; set; } public ParameterModel SelectedParameter { get; set; } - public DeviceModel SelectedDevice { get; set; } } } diff --git a/BOB/Singleton/StepRunning.cs b/BOB/Singleton/StepRunning.cs index 8d26abd..f223bc3 100644 --- a/BOB/Singleton/StepRunning.cs +++ b/BOB/Singleton/StepRunning.cs @@ -1,4 +1,5 @@ using BOB.Models; +using BOB.Singleton; using Common.PubEvent; using Common.Tools; using Logger; @@ -19,6 +20,8 @@ namespace BOB public class StepRunning { private GlobalVariables _globalVariables; + private Devices _devices; + private IContainerProvider containerProvider; private IEventAggregator _eventAggregator; private readonly Dictionary tmpParameters = []; @@ -33,10 +36,11 @@ namespace BOB private bool SubSingleStep = false; private Guid TestRoundID; - public StepRunning(GlobalVariables globalVariables,IEventAggregator eventAggregator) + public StepRunning(GlobalVariables globalVariables,IEventAggregator eventAggregator,IContainerProvider containerProvider) { - _globalVariables = globalVariables; - _eventAggregator= eventAggregator; + _globalVariables = containerProvider.Resolve(); + _eventAggregator = eventAggregator; + } public async Task ExecuteSteps(ProgramModel program, int depth = 0, CancellationToken cancellationToken = default) { @@ -359,7 +363,7 @@ namespace BOB { try { - instance = Activator.CreateInstance(targetType); + instance = _devices.DeviceDic[targetType.Name]; } catch (Exception ex) { diff --git a/BOB/ViewModels/CommandTreeViewModel.cs b/BOB/ViewModels/CommandTreeViewModel.cs index 2737a6d..342fa45 100644 --- a/BOB/ViewModels/CommandTreeViewModel.cs +++ b/BOB/ViewModels/CommandTreeViewModel.cs @@ -487,13 +487,6 @@ namespace BOB.ViewModels var tmp = JsonConvert.DeserializeObject(jsonstr); if (tmp != null) { - if (tmp.Devices != null && tmp.Devices.Count > 0) - { - foreach (var device in tmp.Devices) - { - // _ = DeviceConnect.InitAndConnectDevice(tmp, device); - } - } newStep.SubProgram = tmp; } if (insertIndex >= 0 && insertIndex <= Program.StepCollection.Count) diff --git a/BOB/ViewModels/ShellViewModel.cs b/BOB/ViewModels/ShellViewModel.cs index 46e47f7..95154e0 100644 --- a/BOB/ViewModels/ShellViewModel.cs +++ b/BOB/ViewModels/ShellViewModel.cs @@ -101,7 +101,6 @@ namespace BOB.ViewModels private GlobalVariables _globalVariables; private IContainerProvider _containerProvider; private StepRunning _stepRunning; - private Devices _devices; private Task? currentExecutionTask; public ShellViewModel(IEventAggregator eventAggregator, IContainerProvider containerProvider,GlobalVariables globalVariables, StepRunning stepRunning) { @@ -163,7 +162,6 @@ namespace BOB.ViewModels { _globalVariables.CurrentFilePath = null; _globalVariables.Program.Parameters.Clear(); - _globalVariables.Program.Devices.Clear(); _globalVariables.Program.StepCollection.Clear(); } @@ -207,7 +205,6 @@ namespace BOB.ViewModels } _globalVariables.Program.Parameters = program.Parameters; - _globalVariables.Program.Devices = program.Devices; _globalVariables.Program.StepCollection = program.StepCollection; _globalVariables.CurrentFilePath = filePath; LoggerHelper.SuccessWithNotify($"成功打开文件: {filePath}"); @@ -258,10 +255,6 @@ namespace BOB.ViewModels private ProgramModel ClearDeviceParameterValue(ProgramModel program) { var tmp = new ProgramModel(program); - foreach (var device in tmp.Devices) - { - tmp.Parameters.Remove(tmp.Parameters.First(x => x.ID == device.ParameterID)); - } foreach (var step in tmp.StepCollection) { if (step.SubProgram != null) diff --git a/DeviceCommand/Device/IT6724CReverse.cs b/DeviceCommand/Device/IT6724CReverse.cs new file mode 100644 index 0000000..9cbfda4 --- /dev/null +++ b/DeviceCommand/Device/IT6724CReverse.cs @@ -0,0 +1,163 @@ +using Common.Attributes; +using DeviceCommand.Base; +using System; +using System.IO.Ports; +using System.Threading; +using System.Threading.Tasks; + +namespace DeviceCommand.Device +{ + [BOBCommand] + public class IT6724CReverse : Serial_Port + { + public IT6724CReverse(string COMPort,int BaudRate,int DataBits, StopBits stopBits, Parity parity,int ReadTimeout,int ReceiveTimeout) + { + ConfigureDevice(COMPort,BaudRate,DataBits,stopBits,parity, ReadTimeout, ReceiveTimeout); + + } + public IT6724CReverse() + { + + } + + #region 设置命令 + + /// + /// 设置输出状态,true 为开,false 为关 + /// + public Task SetOutputAsync(bool state, CancellationToken ct = default) + => SendAsync($"OUTPut {(state ? 1 : 0)}\r\n", ct); + + /// + /// 切换到远程控制模式 + /// + public Task SetRemoteModeAsync(CancellationToken ct = default) + => SendAsync("SYSTem:REMote\r\n", ct); + + /// + /// 蜂鸣器测试 + /// + public Task BeeperTestAsync(CancellationToken ct = default) + => SendAsync("SYSTem:BEEPer\r\n", ct); + + /// + /// 设置输出电流(单位:A) + /// + public Task SetCurrentAsync(double current, CancellationToken ct = default) + => SendAsync($"CURRent {current}\r\n", ct); + + /// + /// 设置过流保护电流(OCP,单位:A) + /// + public Task SetOCPCurrentAsync(double current, CancellationToken ct = default) + => SendAsync($"CURRent:PROTection {current}\r\n", ct); + + /// + /// 设置过流保护状态,true 为启用,false 为禁用 + /// + public Task SetOCPStateAsync(bool state, CancellationToken ct = default) + => SendAsync($"CURRent:PROTection:STATe {(state ? 1 : 0)}\r\n", ct); + + /// + /// 清除过流保护触发状态 + /// + public Task ClearOCPAsync(CancellationToken ct = default) + => SendAsync("CURRent:PROTection:CLEar\r\n", ct); + + /// + /// 设置输出电压(单位:V) + /// + public Task SetVoltageAsync(double voltage, CancellationToken ct = default) + => SendAsync($"VOLTage {voltage}\r\n", ct); + + /// + /// 设置过压保护电压(OVP,单位:V) + /// + public Task SetOVPVoltageAsync(double voltage, CancellationToken ct = default) + => SendAsync($"VOLT:PROTection {voltage}\r\n", ct); + + /// + /// 设置过压保护状态,true 为启用,false 为禁用 + /// + public Task SetOVPStateAsync(bool state, CancellationToken ct = default) + => SendAsync($"VOLT:PROTection:STATe {(state ? 1 : 0)}\r\n", ct); + + /// + /// 清除过压保护触发状态 + /// + public Task ClearOVPAsync(CancellationToken ct = default) + => SendAsync("VOLT:PROTection:CLEar\r\n", ct); + + /// + /// 发送自定义命令 + /// + public Task SendCustomCommandAsync(string command, CancellationToken ct = default) + => SendAsync($"{command}\r\n", ct); + + #endregion + + #region 查询命令 + + /// + /// 查询仪器标识,返回制造商、型号、序列号、固件版本 + /// + public async Task QueryIDAsync(CancellationToken ct = default) + { + await SendAsync("*IDN?\r\n", ct); + return await ReadAsync(ct: ct); + } + + /// + /// 查询过流保护是否触发,返回 true 表示触发 + /// + public async Task QueryOCPTrippedAsync(CancellationToken ct = default) + { + await SendAsync("CURRent:PROTection:TRIPed?\r\n", ct); + var result = await ReadAsync(ct: ct); + return result == "1"; + } + + /// + /// 查询过压保护是否触发,返回 true 表示触发 + /// + public async Task QueryOVPTrippedAsync(CancellationToken ct = default) + { + await SendAsync("VOLT:PROTection:TRIPed?\r\n", ct); + var result = await ReadAsync(ct: ct); + return result == "1"; + } + + /// + /// 查询实际输出电流(单位:A) + /// + public async Task QueryCurrentAsync(CancellationToken ct = default) + { + await SendAsync("MEASure:CURRent?\r\n", ct); + var result = await ReadAsync(ct: ct); + return Convert.ToDouble(result); + } + + /// + /// 查询实际输出电压(单位:V) + /// + public async Task QueryVoltageAsync(CancellationToken ct = default) + { + await SendAsync("MEASure:VOLTage?\r\n", ct); + var result = await ReadAsync(ct: ct); + return Convert.ToDouble(result); + } + + /// + /// 查询实际输出功率(单位:W) + /// + public async Task QueryPowerAsync(CancellationToken ct = default) + { + await SendAsync("MEASure:POWer?\r\n", ct); + var result = await ReadAsync(ct: ct); + return Convert.ToDouble(result); + } + + #endregion + + } +}