色彩模式切换成功

This commit is contained in:
zqm
2025-10-29 11:22:38 +08:00
parent 56119eeaea
commit 406d12e31e
3 changed files with 171 additions and 170 deletions

View File

@@ -1834,6 +1834,9 @@ namespace JoyD.Windows.CS.Toprie
if (data == null || data.Length == 0)
return;
// 记录数据接收时间
Log($"[{DateTime.Now:HH:mm:ss.fff}] [图像接收] 开始处理接收到的数据,大小: {data.Length}字节");
// 更新最后接收数据时间戳
_lastDataReceivedTime = DateTime.Now;
@@ -1890,6 +1893,8 @@ namespace JoyD.Windows.CS.Toprie
{
// 创建并触发事件
Image jpegImage = Image.FromStream(ms);
// 记录图像接收时间
Log($"[{DateTime.Now:HH:mm:ss.fff}] [图像接收] 成功接收到JPEG图像大小: {imageData.Length}字节");
OnImageReceived(new ImageReceivedEventArgs(imageData, _currentImageMode));
return;
}
@@ -1898,11 +1903,15 @@ namespace JoyD.Windows.CS.Toprie
// 对于其他格式或无法确认完整度的情况,直接尝试创建
Image generalImage = Image.FromStream(ms);
// 记录图像接收时间
Log($"[{DateTime.Now:HH:mm:ss.fff}] [图像接收] 成功接收到图像,大小: {imageData.Length}字节");
OnImageReceived(new ImageReceivedEventArgs(imageData, _currentImageMode));
}
}
catch (Exception ex)
{
// 记录异常日志
Log($"[{DateTime.Now:HH:mm:ss.fff}] [图像接收] 处理图像数据异常: {ex.Message}");
Console.WriteLine("处理图像数据异常: " + ex.Message);
// 尝试查找有效的图像起始位置
int validStartPos = FindImageStartPosition(imageData);
@@ -1918,6 +1927,8 @@ namespace JoyD.Windows.CS.Toprie
using (MemoryStream ms = new MemoryStream(validImageData))
{
Image validImage = Image.FromStream(ms);
// 记录图像接收时间
Log($"[{DateTime.Now:HH:mm:ss.fff}] [图像接收] 成功接收到有效图像(二次尝试),大小: {validImageData.Length}字节");
OnImageReceived(new ImageReceivedEventArgs(validImageData, _currentImageMode));
}
}
@@ -3818,16 +3829,14 @@ namespace JoyD.Windows.CS.Toprie
return;
}
// 检查是否最近收到过数据
bool recentlyReceivedData = false;
if (_lastDataReceivedTime != DateTime.MinValue)
{
TimeSpan timeSinceLastData = DateTime.Now - _lastDataReceivedTime;
recentlyReceivedData = timeSinceLastData.TotalMilliseconds < DataReceivedTimeout;
// 检查是否最近收到过数据
bool recentlyReceivedData = _lastDataReceivedTime != DateTime.MinValue &&
(DateTime.Now - _lastDataReceivedTime).TotalMilliseconds < DataReceivedTimeout;
if (recentlyReceivedData)
{
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] HeartbeatCallback() - 最近{timeSinceLastData.TotalMilliseconds:F0}ms内收到过数据跳过心跳检测");
TimeSpan timeSinceLastData = DateTime.Now - _lastDataReceivedTime;
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] HeartbeatCallback() - 最近收到数据({timeSinceLastData.TotalMilliseconds:F0}ms前不进行心跳检测");
// 更新连接状态为正常
if (_connectionStatus != ConnectionStatus.Connected)
{
@@ -3835,7 +3844,6 @@ namespace JoyD.Windows.CS.Toprie
}
return;
}
}
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] HeartbeatCallback() - 长时间未收到数据,执行心跳检测");

View File

@@ -275,6 +275,52 @@ namespace JoyD.Windows.CS.Toprie
InvalidResponse
}
/// <summary>
/// 使用独立UDP客户端发送请求并接收响应即用即销毁模式
/// </summary>
/// <param name="targetIp">目标IP地址</param>
/// <param name="data">要发送的数据</param>
/// <param name="port">目标端口</param>
/// <param name="timeout">超时时间(毫秒)</param>
/// <returns>响应数据如果超时或出错则返回null</returns>
private byte[] SendRequestWithDisposableUdp(string targetIp, byte[] data, int port, int timeout)
{
using (UdpClient udpClient = new UdpClient())
{
try
{
// 设置超时时间
udpClient.Client.ReceiveTimeout = timeout;
// 发送数据
IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Parse(targetIp), port);
udpClient.Send(data, data.Length, targetEndPoint);
// 接收响应
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] responseData = udpClient.Receive(ref remoteEndPoint);
return responseData;
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.TimedOut)
{
Console.WriteLine($"UDP请求执行超时({timeout}ms)");
}
else
{
Console.WriteLine($"UDP通信错误: {ex.Message}");
}
return null;
}
catch (Exception ex)
{
Console.WriteLine($"发送UDP请求时发生错误: {ex.Message}");
return null;
}
}
}
/// <summary>
/// 同步发送UDP请求
/// </summary>
@@ -289,28 +335,21 @@ namespace JoyD.Windows.CS.Toprie
response = null;
try
{
var task = SendRequestAsync(targetIp, data, port, timeout);
bool completed = task.Wait(timeout);
if (completed)
{
response = task.Result;
// 使用新的即用即销毁UDP方法
response = SendRequestWithDisposableUdp(targetIp, data, port, timeout);
if (response != null)
{
return RequestResult.Success;
}
// 如果响应为null可能是超时或网络错误
// 在SendRequestWithDisposableUdp方法中已经记录了具体错误
return RequestResult.NetworkError;
}
Console.WriteLine($"UDP请求执行超时({timeout}ms)");
return RequestResult.Timeout;
}
catch (TimeoutException)
{
Console.WriteLine($"UDP请求执行超时异常");
return RequestResult.Timeout;
}
catch (Exception ex)
{
Console.WriteLine($"发送UDP请求时发生错误: {ex.Message}");
Console.WriteLine($"处理UDP请求结果时发生错误: {ex.Message}");
return RequestResult.ProcessingError;
}
}

View File

@@ -30,9 +30,9 @@ namespace JoyD.Windows.CS.Toprie
private enum CMD_TYPE
{
SHUTTER_CORRECTION = 0,
SET_AUTO_SHUTTER = 1,
// 根据设备实际通信协议,设置色彩模式的命令类型
SET_COLOR_PLATE = 17, // 修正为17以匹配设备实际接受的命令格式
// 根据设备实际通信协议调整枚举值,避免冲突
SET_COLOR_PLATE = 2, // 修正为2与设备实际接受的命令格式匹配
SET_AUTO_SHUTTER = 1, // 保持为1不要与其他命令冲突
SET_MIRROR_VIDEO = 3,
SET_VIDEO_MODE = 4,
SET_AREA_POS = 5,
@@ -47,7 +47,7 @@ namespace JoyD.Windows.CS.Toprie
SET_EMAIL_SERVER = 14,
SET_TFTP_SERVER = 15,
SET_NETWORK_ETH = 16,
GET_PARAMETER = 17,
GET_PARAMETER = 20, // 修改为20与a8_sdk保持一致
SET_FUSION_DISTANCE = 18,
SET_ENVIR_PARAM = 19,
SET_ALARM_PARAM = 20,
@@ -144,25 +144,7 @@ namespace JoyD.Windows.CS.Toprie
Disconnect();
}
// 连接设备
private bool Connect()
{
try
{
// 注意设备使用UDP协议通信不需要建立TCP连接
// 直接设置连接状态为true表示准备好进行UDP通信
// 这里仍然保持isConnected标志以兼容现有代码逻辑
isConnected = true;
Console.WriteLine($"UDP通信准备就绪目标设备 {deviceIp}:18890");
return true;
}
catch (Exception ex)
{
Console.WriteLine($"初始化UDP通信失败: {ex.Message}");
isConnected = false;
return false;
}
}
// 断开连接UDP模式下
private void Disconnect()
@@ -224,7 +206,7 @@ namespace JoyD.Windows.CS.Toprie
{
// 使用UDP通信管理器发送请求获取详细的请求结果
var result = UdpCommunicationManager.Instance.SendRequest(deviceIp,
Encoding.ASCII.GetBytes(cmd), out byte[] responseBytes, 18890, 200);
Encoding.ASCII.GetBytes(cmd), out byte[] responseBytes, 18890, 500);
if (result == UdpCommunicationManager.RequestResult.Success && responseBytes != null)
{
@@ -270,7 +252,7 @@ namespace JoyD.Windows.CS.Toprie
private int ParseResponseValue(string response)
{
if (string.IsNullOrEmpty(response) || !response.StartsWith("+RET:"))
return 0;
return -1; // 返回-1表示解析失败
try
{
@@ -283,7 +265,7 @@ namespace JoyD.Windows.CS.Toprie
{
Console.WriteLine($"解析响应失败: {ex.Message}");
}
return 0;
return -1; // 返回-1表示解析失败
}
// SDK核心功能实现 - 不使用DllImport
@@ -533,7 +515,7 @@ namespace JoyD.Windows.CS.Toprie
{
retryCount++;
// 使用SDK格式获取色板: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.COLOR_PLATE}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.COLOR_PLATE}$";
Log($"[色彩模式读取] 开始获取色彩模式值{(retryCount > 1 ? " ( " + retryCount + "/" + maxRetries + ")" : "")},发送命令: {command}");
@@ -544,7 +526,7 @@ namespace JoyD.Windows.CS.Toprie
{
int parsedValue = ParseResponseValue(response);
if (response != null && parsedValue != -1) // 确保响应有效且解析成功
if (response != null && parsedValue != -1) // 确保响应有效且解析成功现在parsedValue为-1表示解析失败
{
Log($"[色彩模式读取] 解析成功,当前值: {parsedValue},上一次值: {_lastKnownColorPlate}");
_lastKnownColorPlate = parsedValue;
@@ -558,7 +540,8 @@ namespace JoyD.Windows.CS.Toprie
}
else
{
Log($"[色彩模式读取] 解析失败或响应无效,返回上次已知值: {_lastKnownColorPlate}");
Log($"[色彩模式读取] 解析失败或响应无效,返回-1表示获取失败");
resultValue = -1; // 失败时返回-1而不是上次已知值
}
}
catch (Exception ex)
@@ -570,7 +553,8 @@ namespace JoyD.Windows.CS.Toprie
}
else
{
Log($"[色彩模式读取] 解析异常: {ex.Message},返回上次已知值: {_lastKnownColorPlate}");
Log($"[色彩模式读取] 解析异常: {ex.Message},返回-1表示获取失败");
resultValue = -1; // 失败时返回-1而不是上次已知值
}
}
}
@@ -581,17 +565,25 @@ namespace JoyD.Windows.CS.Toprie
}
else
{
Log($"[色彩模式读取] 命令发送失败或超时,返回上次已知值: {_lastKnownColorPlate}");
Log($"[色彩模式读取] 命令发送失败或超时,返回-1表示获取失败");
resultValue = -1; // 失败时返回-1而不是上次已知值
}
}
Log($"成功读取当前色彩模式值: {resultValue}"); // 添加一致的成功日志
// 根据是否成功获取来记录不同的日志
if (resultValue != -1)
{
Log($"成功读取当前色彩模式值: {resultValue}");
}
else
{
Log($"获取色彩模式值失败");
}
return resultValue;
}
catch (Exception ex)
{
Log($"[色彩模式读取异常] {ex.Message},返回上次已知值: {_lastKnownColorPlate}");
return _lastKnownColorPlate;
{ Log($"[色彩模式读取异常] {ex.Message},返回-1表示获取失败");
return -1; // 异常时返回-1而不是上次已知值
}
}
}
@@ -607,18 +599,25 @@ namespace JoyD.Windows.CS.Toprie
{
try
{
// 首先尝试获取当前值
int currentValue = GetColor_plateWithoutLock();
Log($"[色彩模式设置] 开始设置色彩模式值为: {value},当前值: {currentValue}");
Log($"[色彩模式设置] 开始设置色彩模式值为: {value}尝试获取当前值: {currentValue}");
// 如果值相同,不需要设置
if (currentValue == value)
// 只有在成功获取到当前值currentValue != -1且与目标值相同时才跳过设置
// 如果获取失败(currentValue == -1仍然执行设置操作避免因获取失败而跳过设置
if (currentValue != -1 && currentValue == value)
{
Log($"[色彩模式设置] 当前色彩模式已为目标值,无需设置");
return true;
}
// 如果获取失败,记录日志说明将继续执行设置
else if (currentValue == -1)
{
Log($"[色彩模式设置] 获取当前值失败,将执行设置操作");
}
// 使用SDK格式设置色板: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_COLOR_PLATE},{value}$";
// 使用SDK格式设置色板: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_COLOR_PLATE},{value}$";
Log($"[色彩模式设置] 发送命令: {command}");
@@ -688,40 +687,28 @@ namespace JoyD.Windows.CS.Toprie
{
try
{
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.COLOR_PLATE}$";
// 使用SDK格式获取色彩模式: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.COLOR_PLATE}$";
if (SendCommand(command, out string response))
{
return ParseResponseValue(response);
int result = ParseResponseValue(response);
// 如果解析结果不是特殊值-1表示解析成功
if (result != -1)
{
return result;
}
}
}
catch (Exception ex)
{
Log($"[GetColor_plateWithoutLock异常] {ex.Message}");
}
return _lastKnownColorPlate;
// 返回特殊值-1表示获取失败而不是返回_lastKnownColorPlate
// 这样SetColorPlate方法可以明确判断是否获取到了真实值
return -1;
}
/// <summary>
/// 安全地重启图像接收,避免重复停止和启动
/// </summary>
private void SafeRestartImageReceiving()
{
try
{
// 注意V8类不直接管理图像接收和连接检查
// 这个方法被Color_plate属性调用主要是为了在设置色彩模式后提供稳定的过渡时间
Log("执行色彩模式变更后的稳定处理");
// 添加短暂延迟以确保设备有时间处理色彩模式变更
System.Threading.Thread.Sleep(300);
Log("色彩模式变更后的稳定处理完成");
}
catch (Exception ex)
{
Log($"色彩模式变更后稳定处理过程中发生异常: {ex.Message}");
}
}
public int Mirror_mode
{
@@ -729,8 +716,8 @@ namespace JoyD.Windows.CS.Toprie
{
try
{
// 使用SDK格式获取镜像模式: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.MIRROR_MODE}$";
// 使用SDK格式获取镜像模式: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.MIRROR_MODE}$";
if (SendCommand(command, out string response))
{
@@ -748,8 +735,8 @@ namespace JoyD.Windows.CS.Toprie
{
try
{
// 使用SDK格式设置镜像模式: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_MIRROR_VIDEO},{value}$";
// 使用SDK格式设置镜像模式: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_MIRROR_VIDEO},{value}$";
if (SendCommand(command, out string response))
{
@@ -773,8 +760,8 @@ namespace JoyD.Windows.CS.Toprie
{
try
{
// 使用SDK格式获取视频模式: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.VIDEO_MODE}$";
// 使用SDK格式获取视频模式: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.VIDEO_MODE}$";
if (SendCommand(command, out string response))
{
@@ -792,8 +779,8 @@ namespace JoyD.Windows.CS.Toprie
{
try
{
// 使用SDK格式设置视频模式: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_VIDEO_MODE},{value}$";
// 使用SDK格式设置视频模式: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_VIDEO_MODE},{value}$";
if (SendCommand(command, out string response))
{
@@ -915,8 +902,8 @@ namespace JoyD.Windows.CS.Toprie
{
try
{
// 使用SDK格式设置线位置: ip:param_mode,enable,sta_x,sta_y,end_x,end_y$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_LINE_POS},{value.enable},{value.sta_x},{value.sta_y},{value.end_x},{value.end_y}$";
// 使用SDK格式设置线位置: +CMD:param_mode,enable,sta_x,sta_y,end_x,end_y$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_LINE_POS},{value.enable},{value.sta_x},{value.sta_y},{value.end_x},{value.end_y}$";
SendCommand(command, out _);
}
@@ -929,8 +916,8 @@ namespace JoyD.Windows.CS.Toprie
{
try
{
// 使用SDK格式获取线位置: ip:param_mode,0$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_LINE_POS},0$";
// 使用SDK格式获取线位置: +CMD:param_mode,0$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_LINE_POS},0$";
if (SendCommand(command, out string response))
{
@@ -1195,7 +1182,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用SDK格式获取Y缩放: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.ISP_Y_SCALE}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.ISP_Y_SCALE}$";
if (SendCommand(command, out string response))
{
@@ -1217,8 +1204,8 @@ namespace JoyD.Windows.CS.Toprie
{
try
{
// 使用SDK格式设置LED状态: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_LED},{value}$";
// 使用SDK格式设置LED状态: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_LED},{value}$";
if (SendCommand(command, out string response))
{
@@ -1360,9 +1347,9 @@ namespace JoyD.Windows.CS.Toprie
{
try
{
// 使用SDK格式设置网络参数: ip:param_mode,param_value$
// 使用SDK格式设置网络参数: +CMD:param_mode,param_value$
string paramsStr = $"{value.enable}";
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_NETWORK_ETH},{paramsStr}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_NETWORK_ETH},{paramsStr}$";
if (SendCommand(command, out string response))
{
@@ -1383,7 +1370,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用GET_PARAMETER命令获取网络参数
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER}:{(int)PARAM_TYPE.NETWORK_ETH_CONFIG}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.NETWORK_ETH_CONFIG}$";
if (SendCommand(command, out string response))
{
@@ -1431,7 +1418,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用GET_PARAMETER命令获取融合距离 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.FUSION_DISTANCE}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.FUSION_DISTANCE}$";
if (SendCommand(command, out string response))
{
@@ -1454,7 +1441,7 @@ namespace JoyD.Windows.CS.Toprie
// 使用SET_ENVIR_PARAM命令设置环境参数 - SDK格式
// 构建参数字符串,包含所有环境参数
string paramsStr = $"{data.method},{data.num},{data.emissivity},{data.airTemp},{data.targetTemp},{data.atmosTrans},{data.distance},{data.infraredTemp},{data.infraredRadia}";
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_ENVIR_PARAM},{paramsStr}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_ENVIR_PARAM},{paramsStr}$";
if (SendCommand(command, out string response))
{
@@ -1476,7 +1463,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用GET_AREA_ENVIR_PARAM命令获取区域环境参数 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_AREA_ENVIR_PARAM},{index}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_AREA_ENVIR_PARAM},{index}$";
if (SendCommand(command, out string response))
{
@@ -1500,7 +1487,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用GET_SPOT_ENVIR_PARAM命令获取点环境参数 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_SPOT_ENVIR_PARAM},{index}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_SPOT_ENVIR_PARAM},{index}$";
if (SendCommand(command, out string response))
{
@@ -1524,7 +1511,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用GET_LINE_ENVIR_PARAM命令获取线环境参数 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_LINE_ENVIR_PARAM}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_LINE_ENVIR_PARAM}$";
if (SendCommand(command, out string response))
{
@@ -1548,7 +1535,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用GET_GLOBAL_ENVIR_PARAM命令获取全局环境参数 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_GLOBAL_ENVIR_PARAM}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_GLOBAL_ENVIR_PARAM}$";
if (SendCommand(command, out string response))
{
@@ -1572,7 +1559,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用SET_ALARM_PARAM命令设置报警参数 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_ALARM_PARAM},{data.method},{data.num},{data.active},{data.condition},{data.captrue},{data.disableCalib},{data.email},{data.digital},{data.ftp},{data.threshold},{data.hysteresis},{data.thresholeTime}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_ALARM_PARAM},{data.method},{data.num},{data.active},{data.condition},{data.captrue},{data.disableCalib},{data.email},{data.digital},{data.ftp},{data.threshold},{data.hysteresis},{data.thresholeTime}$";
if (SendCommand(command, out string response))
{
@@ -1870,44 +1857,21 @@ namespace JoyD.Windows.CS.Toprie
}
}
// 使用UDP协议发送心跳命令与SDK保持一致
// 使用SendCommand方法发送心跳命令与SDK保持一致
public int Heartbeat()
{
// 使用UDP通信管理器进行心跳检测防止异步数据相互影响
try
{
// 根据SDK实际实现心跳命令格式是 +CMD:24$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.HEARTBEAT}$";
byte[] commandBytes = Encoding.ASCII.GetBytes(command);
// SDK实现中会尝试3次心跳这里也采用相同的策略
for (int retry = 0; retry < 3; retry++)
{
Console.WriteLine($"心跳检测...(尝试 {retry + 1}/3)");
Console.WriteLine($"心跳命令: {command}");
Console.WriteLine($"目标IP: {deviceIp}:18890");
try
if (SendCommand(command, out string response))
{
// 使用UDP通信管理器发送心跳请求设置500ms超时
byte[] responseBytes = UdpCommunicationManager.Instance.SendRequest(deviceIp,
commandBytes, 18890, 500);
Console.WriteLine("UDP心跳命令已发送等待响应...");
if (responseBytes != null)
{
string response = Encoding.ASCII.GetString(responseBytes);
Console.WriteLine($"收到UDP心跳响应: {response}");
// 不使用LINQ的Select方法避免缺少System.Linq命名空间的问题
string[] asciiValues = new string[response.Length];
for (int i = 0; i < response.Length; i++)
{
asciiValues[i] = ((int)response[i]).ToString();
}
Console.WriteLine($"响应长度: {response.Length} 字节响应ASCII码: {string.Join(",", asciiValues)}");
// SDK要求响应中必须包含 ":ok" 字符串才算成功
if (!string.IsNullOrEmpty(response) && response.Contains(":ok"))
{
@@ -1919,15 +1883,6 @@ namespace JoyD.Windows.CS.Toprie
Console.WriteLine($"心跳响应不包含':ok',验证失败。收到的响应: '{response}'");
}
}
else
{
Console.WriteLine("UDP心跳未收到响应或超时");
}
}
catch (Exception ex)
{
Console.WriteLine($"UDP心跳异常: {ex.Message}");
}
// 如果不是最后一次尝试,短暂延迟后重试
if (retry < 2)
@@ -1943,7 +1898,6 @@ namespace JoyD.Windows.CS.Toprie
catch (Exception ex)
{
Console.WriteLine($"心跳检测异常: {ex.Message}");
Console.WriteLine($"异常堆栈: {ex.StackTrace}");
return -1;
}
}
@@ -1953,7 +1907,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用SET_UART命令设置串口参数 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_UART},{nSpeed},{nBits},{(int)nEvent},{nStop}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_UART},{nSpeed},{nBits},{(int)nEvent},{nStop}$";
if (SendCommand(command, out string response))
{
@@ -2014,7 +1968,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用SET_DEVICE_NAME命令设置设备名称 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_DEVICE_NAME},{data}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_DEVICE_NAME},{data}$";
if (SendCommand(command, out string response))
{
@@ -2036,7 +1990,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用GET_PARAMETER命令获取设备名称 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.DEVICE_NA}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.DEVICE_NA}$";
if (SendCommand(command, out string response))
{
@@ -2105,7 +2059,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用SET_TEMP_FRAME命令设置温度帧 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_TEMP_FRAME},{(byte)value}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_TEMP_FRAME},{(byte)value}$";
SendCommand(command, out _);
}
@@ -2119,7 +2073,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用GET_TEMP_FRAME命令获取温度帧 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_TEMP_FRAME}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_TEMP_FRAME}$";
if (SendCommand(command, out string response))
{
@@ -2241,7 +2195,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用SET_TIME命令设置系统时间 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_TIME},{data.year},{data.month},{data.day},{data.hour},{data.minute},{data.second}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_TIME},{data.year},{data.month},{data.day},{data.hour},{data.minute},{data.second}$";
if (SendCommand(command, out string response))
{
@@ -2264,7 +2218,7 @@ namespace JoyD.Windows.CS.Toprie
try
{
// 使用SET_IMAGE_MODE命令设置图像模式 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_IMAGE_MODE},{mode}$";
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_IMAGE_MODE},{mode}$";
if (SendCommand(command, out string response))
{