修改色彩模式属性为方法

This commit is contained in:
zqm
2025-10-29 08:52:20 +08:00
parent df97526930
commit 56119eeaea
3 changed files with 211 additions and 133 deletions

View File

@@ -226,14 +226,33 @@ namespace JoyD.Windows.CS.Toprie
{ {
get get
{ {
return v8Instance.Color_plate; return GetColorPlate();
} }
set set
{ {
v8Instance.Color_plate = value; SetColorPlate(value);
} }
} }
/// <summary>
/// 获取当前色彩模式
/// </summary>
/// <returns>当前色彩模式的值</returns>
public int GetColorPlate()
{
return v8Instance.GetColorPlate();
}
/// <summary>
/// 设置色彩模式
/// </summary>
/// <param name="value">要设置的色彩模式值</param>
/// <returns>设置是否成功</returns>
public bool SetColorPlate(int value)
{
return v8Instance.SetColorPlate(value);
}
// 镜像模式属性 // 镜像模式属性
public int Mirror_mode public int Mirror_mode
{ {

View File

@@ -2290,20 +2290,60 @@ namespace JoyD.Windows.CS.Toprie
// 已在循环外部计算paletteValue避免重复计算 // 已在循环外部计算paletteValue避免重复计算
// 直接设置色彩模式,不再在每次尝试中读取原始值 // 直接设置色彩模式,不再在每次尝试中读取原始值
_a8Sdk.Color_plate = paletteValue; bool setSuccess = false;
try
{
// 直接调用SetColorPlate方法设置色彩模式
setSuccess = _a8Sdk.SetColorPlate(paletteValue);
// 短暂延迟,确保设置生效 if (setSuccess)
Thread.Sleep(50); {
// 短暂延迟,确保设置生效
Thread.Sleep(50);
// 减少读取验证避免嵌套UDP命令 // 再次读取当前值进行验证
// 信任SDK的设置操作不再额外验证 int currentValue = _a8Sdk.GetColorPlate();
Log($"色彩模式设置成功: {paletteType} (值: {paletteValue})"); setSuccess = (currentValue == paletteValue);
}
}
catch (Exception ex)
{
Log($"色彩模式设置异常: {ex.Message}");
}
// 基于实际结果记录日志和返回
if (setSuccess)
{
Log($"色彩模式设置成功: {paletteType} (值: {paletteValue})");
return true;
}
else
{
Log($"色彩模式设置失败: 实际设置未生效 (尝试 {attempt + 1}/{maxRetries})");
if (attempt < maxRetries - 1)
{
Thread.Sleep(retryDelayMs);
continue;
}
else
{
// 最后一次尝试失败,尝试恢复原始值
if (originalValue != -1 && _a8Sdk != null && _connectionStatus == ConnectionStatus.Connected)
{
try
{
Log($"尝试恢复色彩模式为原始值: {originalValue}");
_a8Sdk.Color_plate = originalValue;
}
catch { }
}
}
}
// 移除图像接收重启逻辑,因为色彩模式不影响图像接收 // 移除图像接收重启逻辑,因为色彩模式不影响图像接收
// 仅保留短暂延迟确保设置生效 // 仅保留短暂延迟确保设置生效
Thread.Sleep(200); // 给设备处理时间 Thread.Sleep(200); // 给设备处理时间
// return true; // 已在验证成功后返回
return true;
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@@ -31,8 +31,8 @@ namespace JoyD.Windows.CS.Toprie
{ {
SHUTTER_CORRECTION = 0, SHUTTER_CORRECTION = 0,
SET_AUTO_SHUTTER = 1, SET_AUTO_SHUTTER = 1,
// 根据热像仪SDK,设置色彩模式的命令类型 // 根据设备实际通信协议,设置色彩模式的命令类型
SET_COLOR_PLATE = 2, // 保持为2因为热像仪SDK中也是使用SET_COLOR_PLATE命令 SET_COLOR_PLATE = 17, // 修正为17以匹配设备实际接受的命令格式
SET_MIRROR_VIDEO = 3, SET_MIRROR_VIDEO = 3,
SET_VIDEO_MODE = 4, SET_VIDEO_MODE = 4,
SET_AREA_POS = 5, SET_AREA_POS = 5,
@@ -514,154 +514,173 @@ namespace JoyD.Windows.CS.Toprie
} }
} }
public int Color_plate /// <summary>
/// 获取当前色彩模式值
/// </summary>
/// <returns>色彩模式值,如果获取失败则返回上次已知值</returns>
public int GetColorPlate()
{ {
get lock (_colorPlateLock)
{ {
lock (_colorPlateLock) try
{ {
try const int maxRetries = 3;
int retryCount = 0;
bool success = false;
int resultValue = _lastKnownColorPlate;
while (retryCount < maxRetries && !success)
{ {
const int maxRetries = 3; retryCount++;
int retryCount = 0; // 使用SDK格式获取色板: ip:param_mode,param_value$
bool success = false; string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.COLOR_PLATE}$";
int resultValue = _lastKnownColorPlate;
while (retryCount < maxRetries && !success) Log($"[色彩模式读取] 开始获取色彩模式值{(retryCount > 1 ? " ( " + retryCount + "/" + maxRetries + ")" : "")},发送命令: {command}");
if (SendCommand(command, out string response))
{ {
retryCount++; Log($"[色彩模式读取] 收到响应: {response}");
// 使用SDK格式获取色板: ip:param_mode,param_value$ try
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.COLOR_PLATE}$";
Log($"[色彩模式读取] 开始获取色彩模式值{(retryCount > 1 ? " ( " + retryCount + "/" + maxRetries + ")" : "")},发送命令: {command}");
if (SendCommand(command, out string response))
{ {
Log($"[色彩模式读取] 收到响应: {response}"); int parsedValue = ParseResponseValue(response);
try
{
int parsedValue = ParseResponseValue(response);
if (response != null && parsedValue != -1) // 确保响应有效且解析成功 if (response != null && parsedValue != -1) // 确保响应有效且解析成功
{ {
Log($"[色彩模式读取] 解析成功,当前值: {parsedValue},上一次值: {_lastKnownColorPlate}"); Log($"[色彩模式读取] 解析成功,当前值: {parsedValue},上一次值: {_lastKnownColorPlate}");
_lastKnownColorPlate = parsedValue; _lastKnownColorPlate = parsedValue;
resultValue = parsedValue; resultValue = parsedValue;
success = true; success = true;
}
else if (retryCount < maxRetries)
{
Log($"[色彩模式读取] 解析失败或响应无效,将重试...");
System.Threading.Thread.Sleep(200); // 短暂延迟后重试
}
else
{
Log($"[色彩模式读取] 解析失败或响应无效,返回上次已知值: {_lastKnownColorPlate}");
}
} }
catch (Exception ex) else if (retryCount < maxRetries)
{ {
if (retryCount < maxRetries) Log($"[色彩模式读取] 解析失败或响应无效,将重试...");
{ System.Threading.Thread.Sleep(200); // 短暂延迟后重试
Log($"[色彩模式读取] 解析异常: {ex.Message},将重试...");
System.Threading.Thread.Sleep(200); // 短暂延迟后重试
}
else
{
Log($"[色彩模式读取] 解析异常: {ex.Message},返回上次已知值: {_lastKnownColorPlate}");
}
}
}
else if (retryCount < maxRetries)
{
Log($"[色彩模式读取] 命令发送失败或超时,将重试...");
System.Threading.Thread.Sleep(200); // 短暂延迟后重试
}
else
{
Log($"[色彩模式读取] 命令发送失败或超时,返回上次已知值: {_lastKnownColorPlate}");
}
}
Log($"成功读取当前色彩模式值: {resultValue}"); // 添加一致的成功日志
return resultValue;
}
catch (Exception ex)
{
Log($"[色彩模式读取异常] {ex.Message},返回上次已知值: {_lastKnownColorPlate}");
return _lastKnownColorPlate;
}
}
}
set
{
lock (_colorPlateLock)
{
try
{
int currentValue = GetColor_plateWithoutLock();
Log($"[色彩模式设置] 开始设置色彩模式值为: {value},当前值: {currentValue}");
// 如果值相同,不需要设置
if (currentValue == value)
{
Log($"[色彩模式设置] 当前色彩模式已为目标值,无需设置");
return;
}
// 使用SDK格式设置色板: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_COLOR_PLATE},{value}$";
Log($"[色彩模式设置] 发送命令: {command}");
bool success = SendCommand(command, out string response);
if (success && response != null)
{
Log($"[色彩模式设置] 收到响应: {response}");
// 验证响应是否包含成功标记
bool responseValid = response.Contains("+RET:") &&
(!response.Contains("error") && !response.Contains("失败"));
if (responseValid)
{
Log($"[色彩模式设置成功] 从 {_lastKnownColorPlate} 变更为 {value}");
_lastKnownColorPlate = value; // 更新最后已知值
// 验证设置是否生效
Log($"[色彩模式设置] 验证设置是否生效...");
int validatedValue = GetColor_plateWithoutLock();
if (validatedValue == value)
{
Log($"[色彩模式设置] 验证成功,当前值确认为: {validatedValue}");
// 移除图像接收重启逻辑,因为色彩模式不影响图像接收
// 仅保留短暂延迟确保设置生效
Log($"[色彩模式设置] 设置成功,不需要重启图像接收");
} }
else else
{ {
Log($"[色彩模式设置] 验证失败,实际值: {validatedValue},期望值: {value}"); Log($"[色彩模式读取] 解析失败或响应无效,返回上次已知值: {_lastKnownColorPlate}");
} }
} }
catch (Exception ex)
{
if (retryCount < maxRetries)
{
Log($"[色彩模式读取] 解析异常: {ex.Message},将重试...");
System.Threading.Thread.Sleep(200); // 短暂延迟后重试
}
else
{
Log($"[色彩模式读取] 解析异常: {ex.Message},返回上次已知值: {_lastKnownColorPlate}");
}
}
}
else if (retryCount < maxRetries)
{
Log($"[色彩模式读取] 命令发送失败或超时,将重试...");
System.Threading.Thread.Sleep(200); // 短暂延迟后重试
}
else
{
Log($"[色彩模式读取] 命令发送失败或超时,返回上次已知值: {_lastKnownColorPlate}");
}
}
Log($"成功读取当前色彩模式值: {resultValue}"); // 添加一致的成功日志
return resultValue;
}
catch (Exception ex)
{
Log($"[色彩模式读取异常] {ex.Message},返回上次已知值: {_lastKnownColorPlate}");
return _lastKnownColorPlate;
}
}
}
/// <summary>
/// 设置色彩模式值
/// </summary>
/// <param name="value">要设置的色彩模式值</param>
/// <returns>设置是否成功</returns>
public bool SetColorPlate(int value)
{
lock (_colorPlateLock)
{
try
{
int currentValue = GetColor_plateWithoutLock();
Log($"[色彩模式设置] 开始设置色彩模式值为: {value},当前值: {currentValue}");
// 如果值相同,不需要设置
if (currentValue == value)
{
Log($"[色彩模式设置] 当前色彩模式已为目标值,无需设置");
return true;
}
// 使用SDK格式设置色板: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_COLOR_PLATE},{value}$";
Log($"[色彩模式设置] 发送命令: {command}");
bool success = SendCommand(command, out string response);
if (success && response != null)
{
Log($"[色彩模式设置] 收到响应: {response}");
// 验证响应是否包含成功标记
bool responseValid = response.Contains("+RET:") &&
(!response.Contains("error") && !response.Contains("失败"));
if (responseValid)
{
Log($"[色彩模式设置成功] 从 {_lastKnownColorPlate} 变更为 {value}");
_lastKnownColorPlate = value; // 更新最后已知值
// 验证设置是否生效
Log($"[色彩模式设置] 验证设置是否生效...");
int validatedValue = GetColor_plateWithoutLock();
if (validatedValue == value)
{
Log($"[色彩模式设置] 验证成功,当前值确认为: {validatedValue}");
// 移除图像接收重启逻辑,因为色彩模式不影响图像接收
// 仅保留短暂延迟确保设置生效
Log($"[色彩模式设置] 设置成功,不需要重启图像接收");
return true;
}
else else
{ {
Log($"[色彩模式设置失败] 响应验证失败: {response}"); Log($"[色彩模式设置] 验证失败,实际值: {validatedValue},期望值: {value}");
return false;
} }
} }
else else
{ {
Log($"[色彩模式设置失败] 命令发送失败或超时响应为null: {success}"); Log($"[色彩模式设置失败] 响应验证失败: {response}");
return false;
} }
} }
catch (Exception ex) else
{ {
Log($"[色彩模式设置异常] {ex.Message}"); Log($"[色彩模式设置失败] 命令发送失败或超时响应为null: {success}");
return false;
} }
} }
catch (Exception ex)
{
Log($"[色彩模式设置异常] {ex.Message}");
return false;
}
} }
} }
// 保留原属性以保持向后兼容性
public int Color_plate
{
get => GetColorPlate();
set => SetColorPlate(value);
}
/// <summary> /// <summary>
/// 不带锁获取色彩模式,避免在设置后验证时出现死锁 /// 不带锁获取色彩模式,避免在设置后验证时出现死锁
/// </summary> /// </summary>