This commit is contained in:
zqm
2025-10-28 13:08:56 +08:00
parent 6da609c10f
commit eb96e4f151
4 changed files with 1469 additions and 384 deletions

View File

@@ -218,11 +218,11 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用UDP通信管理器发送请求
byte[] responseBytes = UdpCommunicationManager.Instance.SendRequest(deviceIp,
Encoding.ASCII.GetBytes(cmd), 18890, 200);
// 使用UDP通信管理器发送请求,获取详细的请求结果
var result = UdpCommunicationManager.Instance.SendRequest(deviceIp,
Encoding.ASCII.GetBytes(cmd), out byte[] responseBytes, 18890, 200);
if (responseBytes != null)
if (result == UdpCommunicationManager.RequestResult.Success && responseBytes != null)
{
response = Encoding.ASCII.GetString(responseBytes);
Console.WriteLine($"UDP命令已发送: {cmd}");
@@ -232,7 +232,25 @@ namespace JoyD.Windows.CS.Toprie
}
else
{
Console.WriteLine($"UDP命令发送后未收到响应或超时: {cmd}");
// 根据不同的结果类型提供更详细的信息
switch (result)
{
case UdpCommunicationManager.RequestResult.Timeout:
Console.WriteLine($"UDP命令 '{cmd}' 发送后超时");
break;
case UdpCommunicationManager.RequestResult.NetworkError:
Console.WriteLine($"UDP命令 '{cmd}' 网络错误");
break;
case UdpCommunicationManager.RequestResult.InvalidResponse:
Console.WriteLine($"UDP命令 '{cmd}' 收到无效响应");
break;
case UdpCommunicationManager.RequestResult.ProcessingError:
Console.WriteLine($"UDP命令 '{cmd}' 处理错误");
break;
default:
Console.WriteLine($"UDP命令 '{cmd}' 发送后未收到有效响应");
break;
}
return false;
}
@@ -464,6 +482,8 @@ namespace JoyD.Windows.CS.Toprie
}
}
private int _lastKnownColorPlate = 0; // 保存最后已知的色板值
public int Color_plate
{
get
@@ -475,14 +495,20 @@ namespace JoyD.Windows.CS.Toprie
if (SendCommand(command, out string response))
{
return ParseResponseValue(response);
int parsedValue = ParseResponseValue(response);
if (parsedValue != 0 || response != null) // 确保至少解析成功或有响应
{
_lastKnownColorPlate = parsedValue;
return parsedValue;
}
}
return 0; // 默认色板
Console.WriteLine("获取色板失败,返回上次已知值");
return _lastKnownColorPlate; // 返回上次已知值
}
catch (Exception ex)
{
Console.WriteLine($"获取色板失败: {ex.Message}");
return 0;
Console.WriteLine($"获取色板异常: {ex.Message},返回上次已知值");
return _lastKnownColorPlate;
}
}
set
@@ -492,18 +518,31 @@ namespace JoyD.Windows.CS.Toprie
// 使用SDK格式设置色板: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_COLOR_PLATE},{value}$";
if (SendCommand(command, out string response))
bool success = SendCommand(command, out string response);
if (success && response != null)
{
// 验证响应
if (response != null)
// 验证响应是否包含成功标记
bool responseValid = response.Contains("+RET:") &&
(!response.Contains("error") || !response.Contains("失败"));
if (responseValid)
{
Console.WriteLine("设置色板成功");
Console.WriteLine($"设置色板成功: {value}");
_lastKnownColorPlate = value; // 更新最后已知值
}
else
{
Console.WriteLine($"设置色板响应验证失败: {response}");
}
}
else
{
Console.WriteLine($"设置色板失败: 命令发送失败或超时");
}
}
catch (Exception ex)
{
Console.WriteLine($"设置色板失败: {ex.Message}");
Console.WriteLine($"设置色板异常: {ex.Message}");
}
}
}