示波器修改:visa连接方式+截图保存

This commit is contained in:
“hsc”
2026-09-23 09:16:14 +08:00
parent 3275283263
commit 81c6386e62
15 changed files with 910 additions and 88 deletions
+55 -2
View File
@@ -68,6 +68,12 @@ namespace UIShare.GlobalVariable
return $"Tcp:{config.TcpConfig.IPAddress}:{config.TcpConfig.Port}";
}
if (string.Equals(config.ConnectionType, "Visa", StringComparison.OrdinalIgnoreCase)
&& config.VisaConfig != null)
{
return $"Visa:{config.VisaConfig.ResourceName}";
}
if (string.Equals(config.ConnectionType, "Serial", StringComparison.OrdinalIgnoreCase)
&& config.SerialPortConfig != null)
{
@@ -129,6 +135,7 @@ namespace UIShare.GlobalVariable
return config.ConnectionType switch
{
"Tcp" => CreateTcpDevice(deviceType, config.TcpConfig)!,
"Visa" => CreateVisaDevice(deviceType, config.VisaConfig)!,
"Serial" => CreateSerialDevice(deviceType, config.SerialPortConfig)!,
_ => null!
};
@@ -379,6 +386,7 @@ namespace UIShare.GlobalVariable
bool ok = conn switch
{
"Tcp" => await ConnectTcpAsync(name, device, ct),
"Visa" => await ConnectVisaAsync(name, device, ct),
"Serial" => await ConnectSerialAsync(name, device, ct),
_ => false
};
@@ -417,6 +425,16 @@ namespace UIShare.GlobalVariable
return false;
}
private static async Task<bool> ConnectVisaAsync(string name, IBaseInterface device, CancellationToken ct)
{
if (device is IVisa visa)
{
return await visa.ConnectAsync(ct);
}
LoggerHelper.Warn($"设备 [{name}] 配置为 Visa 但未实现 IVisa,实际类型为 {device.GetType().Name}。");
return false;
}
private static async Task<bool> ConnectSerialAsync(string name, IBaseInterface device, CancellationToken ct)
{
if (device is not ISerialPort sp)
@@ -533,7 +551,34 @@ namespace UIShare.GlobalVariable
SendTimeout = vm.SendTimeout,
ReceiveTimeout = vm.ReceiveTimeout
};
return Activator.CreateInstance(type, cfg) as IBaseInterface;
try
{
return Activator.CreateInstance(type, cfg) as IBaseInterface;
}
catch (Exception ex)
{
LoggerHelper.Warn($"实例化 Tcp 设备 [{type.Name}] 失败(连接配置与设备构造函数不匹配?):{ex.InnerException?.Message ?? ex.Message}");
return null;
}
}
private static IBaseInterface? CreateVisaDevice(Type type, VisaConfigVM? vm)
{
vm ??= new VisaConfigVM();
var cfg = new VisaConfig
{
ResourceName = vm.ResourceName,
OpenTimeout = vm.OpenTimeout,
IOTimeout = vm.IOTimeout
};
try
{
return Activator.CreateInstance(type, cfg) as IBaseInterface;
}
catch (Exception ex)
{
LoggerHelper.Warn($"实例化 Visa 设备 [{type.Name}] 失败(连接配置与设备构造函数不匹配?):{ex.InnerException?.Message ?? ex.Message}");
return null;
}
}
private static IBaseInterface? CreateSerialDevice(Type type, SerialPortConfigVM? vm)
{
@@ -548,7 +593,15 @@ namespace UIShare.GlobalVariable
ReadTimeout = vm.ReadTimeout,
WriteTimeout = vm.WriteTimeout
};
return Activator.CreateInstance(type, cfg) as IBaseInterface;
try
{
return Activator.CreateInstance(type, cfg) as IBaseInterface;
}
catch (Exception ex)
{
LoggerHelper.Warn($"实例化 Serial 设备 [{type.Name}] 失败(连接配置与设备构造函数不匹配?):{ex.InnerException?.Message ?? ex.Message}");
return null;
}
}
public void Dispose()