示波器修改:visa连接方式+截图保存
This commit is contained in:
@@ -124,6 +124,11 @@ namespace UIShare.GlobalVariable
|
||||
{
|
||||
alive = await ProbeTcpDeviceAsync(tcpDevice);
|
||||
}
|
||||
// VISA 设备同理:IsConnected 仅表示会话对象存在,主动探活以检测死会话
|
||||
else if (alive && device is Visa visaDevice)
|
||||
{
|
||||
alive = await ProbeVisaDeviceAsync(visaDevice);
|
||||
}
|
||||
|
||||
if (alive)
|
||||
{
|
||||
@@ -176,6 +181,26 @@ namespace UIShare.GlobalVariable
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对 VISA 设备执行轻量级主动探活(SCPI *IDN?)。
|
||||
/// <para>通过 WriteReadAsync 内部获取 _commLock,与监控采样天然串行化;
|
||||
/// 死会话将在 VISA 自身的 IOTimeout 内抛出异常,返回 false 触发重连。</para>
|
||||
/// </summary>
|
||||
/// <returns>true: 探活成功(会话确实存活); false: 探活失败(死会话)</returns>
|
||||
private async Task<bool> ProbeVisaDeviceAsync(Visa visaDevice)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var probeCts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
|
||||
string resp = await visaDevice.WriteReadAsync("*IDN?\n", "\n", probeCts.Token);
|
||||
return !string.IsNullOrWhiteSpace(resp);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重连单个设备。
|
||||
/// <para>
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user