Files
JoyD/Windows/CS/Framework4.0/Toprie/Toprie/V8.cs

2439 lines
95 KiB
C#
Raw Normal View History

2025-10-24 14:40:54 +08:00
using System;
using System.Collections.Generic;
using System.Text;
2025-10-28 10:29:11 +08:00
using System.Net.Sockets;
using System.Net;
2025-10-24 14:40:54 +08:00
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
2025-10-28 10:29:11 +08:00
// 导入UDP通信管理器
using JoyD.Windows.CS.Toprie;
2025-10-24 14:40:54 +08:00
namespace JoyD.Windows.CS.Toprie
{
public class V8
{
2025-10-27 10:39:33 +08:00
// 结构体引用已移至SharedStructures类
2025-10-24 14:40:54 +08:00
// 常量定义
2025-10-27 15:36:18 +08:00
private const int SDK_PORT = 8080;
2025-10-24 14:40:54 +08:00
private const int BUFFER_SIZE = 4096;
private const int TIMEOUT = 3000;
2025-10-27 13:18:15 +08:00
private const string CMD_HEAD = "+CMD";
private const int DISCOVERY_UDP_PORT = 18889;
private const int SEARCH_DEVICE_MAX_NUM = 99;
2025-10-28 16:43:18 +08:00
// 日志文件路径
private static readonly string LogFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log.txt");
2025-10-27 13:18:15 +08:00
// 命令类型枚举
private enum CMD_TYPE
{
SHUTTER_CORRECTION = 0,
2025-10-29 11:22:38 +08:00
// 根据设备实际通信协议调整枚举值,避免冲突
SET_COLOR_PLATE = 2, // 修正为2与设备实际接受的命令格式匹配
SET_AUTO_SHUTTER = 1, // 保持为1不要与其他命令冲突
2025-10-27 13:18:15 +08:00
SET_MIRROR_VIDEO = 3,
SET_VIDEO_MODE = 4,
2025-10-29 13:13:31 +08:00
GET_VIDEO_MODE = 28,
2025-10-27 13:18:15 +08:00
SET_AREA_POS = 5,
SET_SPOT_POS = 6,
SET_LINE_POS = 7,
SET_TEMP_RANGE = 8,
SET_ISP_X_OFFSET = 9,
SET_ISP_Y_OFFSET = 10,
SET_ISP_X_SCALE = 11,
SET_ISP_Y_SCALE = 12,
SET_LED = 13,
SET_EMAIL_SERVER = 14,
SET_TFTP_SERVER = 15,
SET_NETWORK_ETH = 16,
2025-10-29 11:22:38 +08:00
GET_PARAMETER = 20, // 修改为20与a8_sdk保持一致
2025-10-28 17:17:05 +08:00
SET_FUSION_DISTANCE = 18,
SET_ENVIR_PARAM = 19,
SET_ALARM_PARAM = 20,
2025-10-27 13:18:15 +08:00
POWER_REBOOT = 21,
PARAM_RECOVER = 22,
UPDATER = 23,
HEARTBEAT = 24,
SET_UART_PORT = 25,
UART_COMMAND = 26,
AUTOFOCUS = 27,
SET_DEVICE_NAME = 28,
SET_TEMP_FRAME = 29,
SET_ALARM_OUT = 30,
SET_ALARM = 31,
GET_ALARM = 32,
GET_COMP_TEMP = 33,
SET_TIME = 34,
SET_IMAGE_MODE = 35,
GET_IMAGE_DATA = 36,
GET_ALL_TEMP = 37,
UPDATE = 38,
SET_UART = 39,
SEND_UART_COMMAND = 40,
GET_DETECT_NUMBER = 41,
GET_TEMP_FRAME = 42,
GET_GLOBAL_ALARM_PARAM = 43,
GET_LINE_ALARM_PARAM = 44,
GET_AREA_TEMP = 45,
GET_SPOT_TEMP = 46,
GET_LINE_TEMP = 47,
GET_GLOBAL_TEMP = 48,
GET_AREA_ENVIR_PARAM = 49,
GET_SPOT_ENVIR_PARAM = 50,
GET_LINE_ENVIR_PARAM = 51,
GET_GLOBAL_ENVIR_PARAM = 52,
GET_AREA_ALARM_PARAM = 53,
GET_SPOT_ALARM_PARAM = 54,
GET_AREA_POS = 55,
GET_SPOT_POS = 56,
GET_LINE_POS = 57,
GET_ALL_POS = 58
}
// 参数类型枚举
private enum PARAM_TYPE
{
AUTO_SHUTTER_TIME = 0,
COLOR_PLATE = 1,
MIRROR_MODE = 2,
VIDEO_MODE = 3,
AREA_POS = 4,
SPOT_POS = 5,
LINE_POS = 6,
TEMP_RANGE = 7,
ISP_X_OFFSET = 8,
ISP_Y_OFFSET = 9,
ISP_X_SCALE = 10,
ISP_Y_SCALE = 11,
LED_PARAM = 12,
EMAIL_SERVER = 13,
TFTP_SERVER = 14,
NETWORK_ETH = 15,
FUSION_DISTANCE = 16,
ENVIR_PARAM = 17,
ALARM_PARAM = 18,
TEMP_DATA = 19,
ALL_POS = 20,
DEVICE_NA = 21,
DETECT_NO = 22,
TEMP_FRAME = 23,
ALARM_IN = 24,
COMP_TEMP = 25,
LED_STATUS = 26,
EMAIL_SERVER_CONFIG = 27,
TFTP_SERVER_CONFIG = 28,
NETWORK_ETH_CONFIG = 29
}
2025-10-24 14:40:54 +08:00
// 私有字段
private readonly string deviceIp;
private Socket socket = null;
2025-10-24 14:40:54 +08:00
private static bool isSdkInitialized = false;
private static readonly Dictionary<string, V8> deviceInstances = new Dictionary<string, V8>();
2025-10-24 14:40:54 +08:00
public V8(string ip)
{
deviceIp = ip;
}
~V8()
{
Disconnect();
}
2025-10-29 11:22:38 +08:00
2025-10-24 14:40:54 +08:00
2025-10-27 16:18:24 +08:00
// 断开连接UDP模式下
2025-10-24 14:40:54 +08:00
private void Disconnect()
{
try
{
2025-10-27 16:18:24 +08:00
// 注意在UDP模式下不需要关闭socket连接
// 只需重置连接状态标志
// 仍然清理socket资源以防万一
2025-10-24 14:40:54 +08:00
if (socket != null)
{
socket.Close();
socket = null;
}
Console.WriteLine("UDP通信状态已重置");
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
2025-10-27 16:18:24 +08:00
Console.WriteLine($"重置UDP通信状态失败: {ex.Message}");
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 13:18:15 +08:00
// 发送命令并接收响应(字节数组版本)
2025-10-24 14:40:54 +08:00
private bool SendCommand(byte[] command, out byte[] response, int responseLength = 0)
{
response = null;
try
{
2025-10-28 10:29:11 +08:00
// 使用UDP通信管理器发送请求
response = UdpCommunicationManager.Instance.SendRequest(deviceIp, command, 18890, 200);
if (response != null)
2025-10-24 14:40:54 +08:00
{
2025-10-27 16:18:24 +08:00
Console.WriteLine($"UDP命令已发送到 {deviceIp}:18890");
Console.WriteLine($"收到UDP命令响应长度: {response.Length}");
2025-10-24 14:40:54 +08:00
return true;
}
2025-10-28 10:29:11 +08:00
else
{
Console.WriteLine($"UDP命令发送后未收到响应或超时");
return false;
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
2025-10-27 16:18:24 +08:00
Console.WriteLine($"UDP发送命令失败: {ex.Message}");
2025-10-24 14:40:54 +08:00
return false;
}
}
2025-10-27 13:18:15 +08:00
// 发送命令并接收响应(字符串版本)
private bool SendCommand(string cmd, out string response)
{
response = null;
try
{
2025-10-28 13:08:56 +08:00
// 使用UDP通信管理器发送请求获取详细的请求结果
var result = UdpCommunicationManager.Instance.SendRequest(deviceIp,
2025-10-29 11:22:38 +08:00
Encoding.ASCII.GetBytes(cmd), out byte[] responseBytes, 18890, 500);
2025-10-28 10:29:11 +08:00
2025-10-28 13:08:56 +08:00
if (result == UdpCommunicationManager.RequestResult.Success && responseBytes != null)
2025-10-27 13:18:15 +08:00
{
2025-10-28 10:29:11 +08:00
response = Encoding.ASCII.GetString(responseBytes);
2025-10-27 16:18:24 +08:00
Console.WriteLine($"UDP命令已发送: {cmd}");
Console.WriteLine($"目标IP: {deviceIp}:18890");
Console.WriteLine($"收到UDP命令响应: {response}");
2025-10-27 13:18:15 +08:00
return true;
}
2025-10-28 10:29:11 +08:00
else
{
2025-10-28 13:08:56 +08:00
// 根据不同的结果类型提供更详细的信息
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;
}
2025-10-28 10:29:11 +08:00
return false;
}
2025-10-27 16:18:24 +08:00
2025-10-27 13:18:15 +08:00
}
catch (Exception ex)
{
2025-10-27 16:18:24 +08:00
Console.WriteLine($"UDP发送命令失败: {ex.Message}");
2025-10-27 13:18:15 +08:00
return false;
}
}
// 解析响应数据
private int ParseResponseValue(string response)
{
2025-10-29 12:55:14 +08:00
if (string.IsNullOrEmpty(response))
2025-10-29 11:22:38 +08:00
return -1; // 返回-1表示解析失败
2025-10-27 13:18:15 +08:00
2025-10-29 12:55:14 +08:00
// 支持两种响应格式: +RET: 和 +RSP:
int startIndex = -1;
if (response.StartsWith("+RET:"))
startIndex = 5;
else if (response.StartsWith("+RSP:"))
startIndex = 5;
if (startIndex == -1)
return -1;
2025-10-27 13:18:15 +08:00
try
{
2025-10-28 17:17:05 +08:00
// 从第5个字符开始解析数值与热像仪SDK保持一致
2025-10-29 12:55:14 +08:00
string valueStr = response.Substring(startIndex);
// 移除结尾可能存在的$符号
if (valueStr.EndsWith("$"))
valueStr = valueStr.Substring(0, valueStr.Length - 1);
2025-10-28 17:17:05 +08:00
return int.Parse(valueStr);
2025-10-27 13:18:15 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"解析响应失败: {ex.Message}");
}
2025-10-29 11:22:38 +08:00
return -1; // 返回-1表示解析失败
2025-10-27 13:18:15 +08:00
}
2025-10-24 14:40:54 +08:00
// SDK核心功能实现 - 不使用DllImport
public static int SDK_initialize()
{
try
{
if (isSdkInitialized)
return 0; // 已初始化
// 初始化SDK资源
deviceInstances.Clear();
isSdkInitialized = true;
Console.WriteLine("SDK初始化成功");
return 0;
}
catch (Exception ex)
{
Console.WriteLine($"SDK初始化失败: {ex.Message}");
return -1;
}
}
public static int SDK_destroy()
{
try
{
if (!isSdkInitialized)
return 0; // 未初始化
// 清理所有设备连接
foreach (var instance in deviceInstances.Values)
{
instance.Disconnect();
}
deviceInstances.Clear();
isSdkInitialized = false;
Console.WriteLine("SDK销毁成功");
return 0;
}
catch (Exception ex)
{
Console.WriteLine($"SDK销毁失败: {ex.Message}");
return -1;
}
}
public static string SDK_serch_device(int list_len)
{
try
{
if (!isSdkInitialized)
{
Console.WriteLine("SDK未初始化");
return string.Empty;
}
2025-10-27 13:18:15 +08:00
const int DISCOVERY_UDP_PORT = 18889;
const int SEARCH_DEVICE_MAX_NUM = 99;
const int TIMEOUT_MS = 200; // 200毫秒超时
const string DISCOVERY_CMD = "CMD_DISCOVER";
2025-10-24 14:40:54 +08:00
StringBuilder deviceList = new StringBuilder();
2025-10-27 13:18:15 +08:00
List<string> discoveredDevices = new List<string>();
// 创建UDP socket
using (Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
// 设置为广播模式
udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
// 设置接收超时
udpSocket.ReceiveTimeout = TIMEOUT_MS;
// 发送广播命令
byte[] sendData = Encoding.ASCII.GetBytes(DISCOVERY_CMD);
IPEndPoint broadcastEndPoint = new IPEndPoint(IPAddress.Broadcast, DISCOVERY_UDP_PORT);
udpSocket.SendTo(sendData, broadcastEndPoint);
Console.WriteLine("发送设备发现广播命令: CMD_DISCOVER");
// 接收设备回复最多接收99个设备
int loopCount = SEARCH_DEVICE_MAX_NUM;
byte[] receiveBuffer = new byte[1024];
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
try
{
while (loopCount > 0)
{
int receiveLength = udpSocket.ReceiveFrom(receiveBuffer, ref remoteEndPoint);
if (receiveLength > 0)
{
string deviceIp = ((IPEndPoint)remoteEndPoint).Address.ToString();
if (!discoveredDevices.Contains(deviceIp))
{
discoveredDevices.Add(deviceIp);
Console.WriteLine($"发现设备: {deviceIp}");
}
}
loopCount--;
}
}
catch (SocketException ex)
{
// 超时异常是正常的,继续处理已发现的设备
if (ex.SocketErrorCode != SocketError.TimedOut)
{
Console.WriteLine($"接收设备回复异常: {ex.Message}");
}
}
}
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
// 将发现的设备IP用分号连接
for (int i = 0; i < discoveredDevices.Count; i++)
{
deviceList.Append(discoveredDevices[i]);
if (i < discoveredDevices.Count - 1)
{
deviceList.Append(';');
}
}
2025-10-24 14:40:54 +08:00
return deviceList.ToString();
}
catch (Exception ex)
{
Console.WriteLine($"搜索设备失败: {ex.Message}");
return string.Empty;
}
}
public void Shutter_correction()
{
try
{
2025-10-27 13:18:15 +08:00
// 根据SDK格式构建快门校正命令: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SHUTTER_CORRECTION},0$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
Console.WriteLine($"快门校正命令已发送,响应: {response}");
2025-10-24 14:40:54 +08:00
}
else
{
throw new Exception("快门校正失败");
}
}
catch (Exception ex)
{
Console.WriteLine($"快门校正失败: {ex.Message}");
}
}
public int Shutter_times
{
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式获取快门校正时间: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.AUTO_SHUTTER_TIME}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 根据SDK响应格式解析
return ParseResponseValue(response);
2025-10-24 14:40:54 +08:00
}
2025-10-27 13:18:15 +08:00
return 60; // 默认值
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取快门时间失败: {ex.Message}");
2025-10-27 13:18:15 +08:00
return 60; // 默认值
2025-10-24 14:40:54 +08:00
}
}
set
2025-10-28 16:43:18 +08:00
{;
2025-10-24 14:40:54 +08:00
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式设置快门校正时间: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_AUTO_SHUTTER},{value}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应
if (response != null)
{
Console.WriteLine("设置快门时间成功");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置快门时间失败: {ex.Message}");
}
}
}
2025-10-28 13:08:56 +08:00
private int _lastKnownColorPlate = 0; // 保存最后已知的色板值
2025-10-28 16:43:18 +08:00
private readonly object _colorPlateLock = new object(); // 线程锁,确保日志记录的线程安全
/// <summary>
/// 静态日志写入方法
/// </summary>
/// <param name="message">日志消息</param>
public static void Log(string message)
{
try
{
// 确保日志目录存在
string logDirectory = Path.GetDirectoryName(LogFilePath);
if (!Directory.Exists(logDirectory))
{
Directory.CreateDirectory(logDirectory);
}
// 写入日志,不添加额外时间戳(因为原始消息已包含时间戳)
File.AppendAllText(LogFilePath, message + Environment.NewLine);
// 同时输出到控制台
Console.WriteLine(message);
}
catch (Exception ex)
{
// 如果日志写入失败,至少输出到控制台
Console.WriteLine($"[日志写入失败] {ex.Message}");
}
}
2025-10-28 13:08:56 +08:00
2025-10-29 08:52:20 +08:00
/// <summary>
/// 获取当前色彩模式值
/// </summary>
/// <returns>色彩模式值,如果获取失败则返回上次已知值</returns>
public int GetColorPlate()
2025-10-24 14:40:54 +08:00
{
2025-10-29 08:52:20 +08:00
lock (_colorPlateLock)
2025-10-24 14:40:54 +08:00
{
2025-10-29 08:52:20 +08:00
try
2025-10-24 14:40:54 +08:00
{
2025-10-29 08:52:20 +08:00
const int maxRetries = 3;
int retryCount = 0;
bool success = false;
int resultValue = _lastKnownColorPlate;
while (retryCount < maxRetries && !success)
2025-10-24 14:40:54 +08:00
{
2025-10-29 08:52:20 +08:00
retryCount++;
// 使用SDK格式获取色板: ip:param_mode,param_value$
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.COLOR_PLATE}$";
2025-10-28 16:43:18 +08:00
2025-10-29 08:52:20 +08:00
Log($"[色彩模式读取] 开始获取色彩模式值{(retryCount > 1 ? " ( " + retryCount + "/" + maxRetries + ")" : "")},发送命令: {command}");
if (SendCommand(command, out string response))
2025-10-28 13:08:56 +08:00
{
2025-10-29 08:52:20 +08:00
Log($"[色彩模式读取] 收到响应: {response}");
try
2025-10-28 16:43:18 +08:00
{
2025-10-29 08:52:20 +08:00
int parsedValue = ParseResponseValue(response);
2025-10-28 16:43:18 +08:00
2025-10-29 11:22:38 +08:00
if (response != null && parsedValue != -1) // 确保响应有效且解析成功现在parsedValue为-1表示解析失败
2025-10-28 16:43:18 +08:00
{
2025-10-29 08:52:20 +08:00
Log($"[色彩模式读取] 解析成功,当前值: {parsedValue},上一次值: {_lastKnownColorPlate}");
_lastKnownColorPlate = parsedValue;
resultValue = parsedValue;
success = true;
}
else if (retryCount < maxRetries)
{
Log($"[色彩模式读取] 解析失败或响应无效,将重试...");
System.Threading.Thread.Sleep(200); // 短暂延迟后重试
2025-10-28 16:43:18 +08:00
}
else
{
2025-10-29 11:22:38 +08:00
Log($"[色彩模式读取] 解析失败或响应无效,返回-1表示获取失败");
resultValue = -1; // 失败时返回-1而不是上次已知值
2025-10-28 16:43:18 +08:00
}
}
2025-10-29 08:52:20 +08:00
catch (Exception ex)
{
if (retryCount < maxRetries)
{
Log($"[色彩模式读取] 解析异常: {ex.Message},将重试...");
System.Threading.Thread.Sleep(200); // 短暂延迟后重试
}
else
{
2025-10-29 11:22:38 +08:00
Log($"[色彩模式读取] 解析异常: {ex.Message},返回-1表示获取失败");
resultValue = -1; // 失败时返回-1而不是上次已知值
2025-10-29 08:52:20 +08:00
}
}
}
else if (retryCount < maxRetries)
{
Log($"[色彩模式读取] 命令发送失败或超时,将重试...");
System.Threading.Thread.Sleep(200); // 短暂延迟后重试
}
else
{
2025-10-29 11:22:38 +08:00
Log($"[色彩模式读取] 命令发送失败或超时,返回-1表示获取失败");
resultValue = -1; // 失败时返回-1而不是上次已知值
2025-10-29 08:52:20 +08:00
}
}
2025-10-29 11:22:38 +08:00
// 根据是否成功获取来记录不同的日志
if (resultValue != -1)
{
Log($"成功读取当前色彩模式值: {resultValue}");
}
else
{
Log($"获取色彩模式值失败");
}
2025-10-29 08:52:20 +08:00
return resultValue;
}
catch (Exception ex)
2025-10-29 11:22:38 +08:00
{ Log($"[色彩模式读取异常] {ex.Message},返回-1表示获取失败");
return -1; // 异常时返回-1而不是上次已知值
}
2025-10-29 08:52:20 +08:00
}
}
/// <summary>
/// 设置色彩模式值
/// </summary>
/// <param name="value">要设置的色彩模式值</param>
/// <returns>设置是否成功</returns>
public bool SetColorPlate(int value)
{
lock (_colorPlateLock)
{
try
{
2025-10-29 11:22:38 +08:00
// 首先尝试获取当前值
2025-10-29 08:52:20 +08:00
int currentValue = GetColor_plateWithoutLock();
2025-10-29 11:22:38 +08:00
Log($"[色彩模式设置] 开始设置色彩模式值为: {value},尝试获取当前值: {currentValue}");
2025-10-29 08:52:20 +08:00
2025-10-29 11:22:38 +08:00
// 只有在成功获取到当前值currentValue != -1且与目标值相同时才跳过设置
// 如果获取失败currentValue == -1仍然执行设置操作避免因获取失败而跳过设置
if (currentValue != -1 && currentValue == value)
2025-10-29 08:52:20 +08:00
{
Log($"[色彩模式设置] 当前色彩模式已为目标值,无需设置");
return true;
}
2025-10-29 11:22:38 +08:00
// 如果获取失败,记录日志说明将继续执行设置
else if (currentValue == -1)
{
Log($"[色彩模式设置] 获取当前值失败,将执行设置操作");
}
2025-10-29 08:52:20 +08:00
2025-10-29 11:22:38 +08:00
// 使用SDK格式设置色板: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_COLOR_PLATE},{value}$";
2025-10-29 08:52:20 +08:00
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;
}
2025-10-28 16:43:18 +08:00
else
{
2025-10-29 08:52:20 +08:00
Log($"[色彩模式设置] 验证失败,实际值: {validatedValue},期望值: {value}");
return false;
2025-10-28 16:43:18 +08:00
}
2025-10-27 13:18:15 +08:00
}
2025-10-28 13:08:56 +08:00
else
{
2025-10-29 08:52:20 +08:00
Log($"[色彩模式设置失败] 响应验证失败: {response}");
return false;
2025-10-28 13:08:56 +08:00
}
}
2025-10-29 08:52:20 +08:00
else
2025-10-28 13:08:56 +08:00
{
2025-10-29 08:52:20 +08:00
Log($"[色彩模式设置失败] 命令发送失败或超时响应为null: {success}");
return false;
2025-10-27 13:18:15 +08:00
}
2025-10-24 14:40:54 +08:00
}
2025-10-29 08:52:20 +08:00
catch (Exception ex)
{
Log($"[色彩模式设置异常] {ex.Message}");
return false;
}
2025-10-28 16:43:18 +08:00
}
}
2025-10-29 08:52:20 +08:00
// 保留原属性以保持向后兼容性
public int Color_plate
{
get => GetColorPlate();
set => SetColorPlate(value);
}
2025-10-28 16:43:18 +08:00
/// <summary>
/// 不带锁获取色彩模式,避免在设置后验证时出现死锁
/// </summary>
private int GetColor_plateWithoutLock()
{
try
{
2025-10-29 11:22:38 +08:00
// 使用SDK格式获取色彩模式: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.COLOR_PLATE}$";
2025-10-28 16:43:18 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-29 11:22:38 +08:00
int result = ParseResponseValue(response);
// 如果解析结果不是特殊值-1表示解析成功
if (result != -1)
{
return result;
}
2025-10-24 14:40:54 +08:00
}
}
2025-10-28 16:43:18 +08:00
catch (Exception ex)
{
Log($"[GetColor_plateWithoutLock异常] {ex.Message}");
}
2025-10-29 11:22:38 +08:00
// 返回特殊值-1表示获取失败而不是返回_lastKnownColorPlate
// 这样SetColorPlate方法可以明确判断是否获取到了真实值
return -1;
2025-10-28 16:43:18 +08:00
}
2025-10-29 11:22:38 +08:00
2025-10-24 14:40:54 +08:00
public int Mirror_mode
{
get
{
try
{
2025-10-29 11:22:38 +08:00
// 使用SDK格式获取镜像模式: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.MIRROR_MODE}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
return ParseResponseValue(response);
2025-10-24 14:40:54 +08:00
}
2025-10-27 13:18:15 +08:00
return 0; // 默认不镜像
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取镜像模式失败: {ex.Message}");
return 0;
}
}
set
{
try
{
2025-10-29 11:22:38 +08:00
// 使用SDK格式设置镜像模式: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_MIRROR_VIDEO},{value}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应
if (response != null)
{
Console.WriteLine("设置镜像模式成功");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置镜像模式失败: {ex.Message}");
}
}
}
2025-10-29 13:47:51 +08:00
private int _lastKnownVideoMode = 0; // 保存最后已知的视频模式值
private readonly object _videoModeLock = new object(); // 线程锁,确保线程安全
/// <summary>
/// 获取当前视频模式值(内部方法)
/// </summary>
/// <returns>视频模式值,如果获取失败则返回上次已知值</returns>
private int GetVideoModeInternal()
2025-10-24 14:40:54 +08:00
{
2025-10-29 13:47:51 +08:00
lock (_videoModeLock)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-29 13:47:51 +08:00
const int maxRetries = 3;
int retryCount = 0;
bool success = false;
int resultValue = _lastKnownVideoMode;
2025-10-24 14:40:54 +08:00
2025-10-29 13:47:51 +08:00
while (retryCount < maxRetries && !success)
2025-10-24 14:40:54 +08:00
{
2025-10-29 13:47:51 +08:00
retryCount++;
// 使用SDK格式获取视频模式: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.VIDEO_MODE}$";
Log($"[视频模式读取] 开始获取视频模式值{(retryCount > 1 ? " ( " + retryCount + "/" + maxRetries + ")" : "")},发送命令: {command}");
if (SendCommand(command, out string response))
{
Log($"[视频模式读取] 收到响应: {response}");
try
{
int parsedValue = ParseResponseValue(response);
if (response != null && parsedValue != -1) // 确保响应有效且解析成功
{
Log($"[视频模式读取] 解析成功,当前值: {parsedValue},上一次值: {_lastKnownVideoMode}");
_lastKnownVideoMode = parsedValue;
resultValue = parsedValue;
success = true;
}
else if (retryCount < maxRetries)
{
Log($"[视频模式读取] 解析失败或响应无效,将重试...");
System.Threading.Thread.Sleep(200); // 短暂延迟后重试
}
else
{
Log($"[视频模式读取] 解析失败或响应无效,返回-1表示获取失败");
resultValue = -1; // 失败时返回-1而不是上次已知值
}
}
catch (Exception ex)
{
if (retryCount < maxRetries)
{
Log($"[视频模式读取] 解析异常: {ex.Message},将重试...");
System.Threading.Thread.Sleep(200); // 短暂延迟后重试
}
else
{
Log($"[视频模式读取] 解析异常: {ex.Message},重试次数已达上限");
resultValue = -1;
}
}
}
else if (retryCount < maxRetries)
{
Log($"[视频模式读取] 发送命令失败,将重试...");
System.Threading.Thread.Sleep(200); // 短暂延迟后重试
}
else
{
Log($"[视频模式读取] 发送命令失败,重试次数已达上限");
resultValue = -1;
}
2025-10-24 14:40:54 +08:00
}
2025-10-29 13:47:51 +08:00
return resultValue;
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
2025-10-29 13:47:51 +08:00
Log($"[视频模式读取] 发生异常: {ex.Message}");
return -1;
2025-10-24 14:40:54 +08:00
}
}
2025-10-29 13:47:51 +08:00
}
/// <summary>
/// 设置视频模式值(内部方法)
/// </summary>
/// <param name="value">要设置的视频模式值</param>
/// <returns>设置是否成功</returns>
private bool SetVideoModeInternal(int value)
{
lock (_videoModeLock)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-29 13:47:51 +08:00
// 首先尝试获取当前值
int currentValue = GetVideoModeWithoutLock();
Log($"[视频模式设置] 开始设置视频模式值为: {value},尝试获取当前值: {currentValue}");
// 只有在成功获取到当前值currentValue != -1且与目标值相同时才跳过设置
// 如果获取失败currentValue == -1仍然执行设置操作避免因获取失败而跳过设置
if (currentValue != -1 && currentValue == value)
{
Log($"[视频模式设置] 当前视频模式已为目标值,无需设置");
return true;
}
// 如果获取失败,记录日志说明将继续执行设置
else if (currentValue == -1)
{
Log($"[视频模式设置] 获取当前值失败,将执行设置操作");
}
2025-10-29 11:22:38 +08:00
// 使用SDK格式设置视频模式: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_VIDEO_MODE},{value}$";
2025-10-24 14:40:54 +08:00
2025-10-29 13:47:51 +08:00
Log($"[视频模式设置] 发送命令: {command}");
bool success = SendCommand(command, out string response);
if (success && response != null)
2025-10-27 13:18:15 +08:00
{
2025-10-29 13:47:51 +08:00
Log($"[视频模式设置] 收到响应: {response}");
// 验证响应是否包含成功标记
bool responseValid = response.Contains("+RET:") &&
(!response.Contains("error") && !response.Contains("失败"));
if (responseValid)
2025-10-27 13:18:15 +08:00
{
2025-10-29 13:47:51 +08:00
Log($"[视频模式设置成功] 从 {_lastKnownVideoMode} 变更为 {value}");
_lastKnownVideoMode = value; // 更新最后已知值
// 验证设置是否生效
Log($"[视频模式设置] 验证设置是否生效...");
int validatedValue = GetVideoModeWithoutLock();
if (validatedValue == value)
{
Log($"[视频模式设置] 验证成功,当前值确认为: {validatedValue}");
return true;
}
else
{
Log($"[视频模式设置] 验证失败,当前值: {validatedValue},目标值: {value}");
return false;
}
2025-10-27 13:18:15 +08:00
}
2025-10-29 13:47:51 +08:00
else
{
Log($"[视频模式设置] 响应无效或失败: {response}");
return false;
}
}
else
{
Log($"[视频模式设置] 发送命令失败或未收到响应");
return false;
2025-10-27 13:18:15 +08:00
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
2025-10-29 13:47:51 +08:00
Log($"[视频模式设置] 发生异常: {ex.Message}");
return false;
2025-10-24 14:40:54 +08:00
}
}
}
2025-10-29 13:47:51 +08:00
/// <summary>
/// 不带锁获取视频模式,避免在设置后验证时出现死锁
/// </summary>
private int GetVideoModeWithoutLock()
{
try
{
// 使用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))
{
int result = ParseResponseValue(response);
// 如果解析结果不是特殊值-1表示解析成功
if (result != -1)
{
return result;
}
}
}
catch (Exception ex)
{
Log($"[GetVideoModeWithoutLock异常] {ex.Message}");
}
// 返回特殊值-1表示获取失败而不是返回_lastKnownVideoMode
// 这样SetVideoModeInternal方法可以明确判断是否获取到了真实值
return -1;
}
// 保留原属性以保持向后兼容性
public int Video_mode
{
get => GetVideoModeInternal();
set => SetVideoModeInternal(value);
}
2025-10-24 14:40:54 +08:00
2025-10-27 10:39:33 +08:00
public void Set_area_pos(int index, SharedStructures.AreaPos area_data)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式设置区域位置: ip:param_mode,index,enable,x,y,width$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_AREA_POS},{index},{area_data.enable},{area_data.x},{area_data.y},{area_data.width}$";
2025-10-24 14:40:54 +08:00
SendCommand(command, out _);
}
catch (Exception ex)
{
Console.WriteLine($"设置区域位置失败: {ex.Message}");
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.AreaPos Get_area_pos(int index)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式获取区域位置: ip:param_mode,index$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_AREA_POS},{index}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
// 解析响应数据
2025-10-27 10:39:33 +08:00
SharedStructures.AreaPos area_data = new SharedStructures.AreaPos();
2025-10-27 13:18:15 +08:00
if (!string.IsNullOrEmpty(response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 移除结束符$并按逗号分割数据
string[] values = response.TrimEnd('$').Split(',');
if (values.Length >= 5)
{
area_data.enable = int.Parse(values[1]);
area_data.x = int.Parse(values[2]);
area_data.y = int.Parse(values[3]);
area_data.width = int.Parse(values[4]);
}
2025-10-24 14:40:54 +08:00
}
return area_data;
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.AreaPos();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取区域位置失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.AreaPos();
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public void Set_spot_pos(int index, SharedStructures.SpotPos spot_data)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式设置点位置: ip:param_mode,index,enable,x,y$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_SPOT_POS},{index},{spot_data.enable},{spot_data.x},{spot_data.y}$";
2025-10-24 14:40:54 +08:00
SendCommand(command, out _);
}
catch (Exception ex)
{
Console.WriteLine($"设置点位置失败: {ex.Message}");
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.SpotPos Get_spot_pos(int index)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式获取点位置: ip:param_mode,index$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_SPOT_POS},{index}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 10:39:33 +08:00
SharedStructures.SpotPos spot_data = new SharedStructures.SpotPos();
2025-10-27 13:18:15 +08:00
if (!string.IsNullOrEmpty(response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 移除结束符$并按逗号分割数据
string[] values = response.TrimEnd('$').Split(',');
if (values.Length >= 4)
{
spot_data.enable = int.Parse(values[1]);
spot_data.x = int.Parse(values[2]);
spot_data.y = int.Parse(values[3]);
}
2025-10-24 14:40:54 +08:00
}
return spot_data;
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.SpotPos();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取点位置失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.SpotPos();
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.LinePos Line_pos
2025-10-24 14:40:54 +08:00
{
set
{
try
{
2025-10-29 11:22:38 +08:00
// 使用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}$";
2025-10-24 14:40:54 +08:00
SendCommand(command, out _);
}
catch (Exception ex)
{
Console.WriteLine($"设置线位置失败: {ex.Message}");
}
}
get
{
try
{
2025-10-29 11:22:38 +08:00
// 使用SDK格式获取线位置: +CMD:param_mode,0$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_LINE_POS},0$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 10:39:33 +08:00
SharedStructures.LinePos line_data = new SharedStructures.LinePos();
2025-10-27 13:18:15 +08:00
if (!string.IsNullOrEmpty(response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 移除结束符$并按逗号分割数据
string[] values = response.TrimEnd('$').Split(',');
if (values.Length >= 6)
{
line_data.enable = int.Parse(values[1]);
line_data.sta_x = int.Parse(values[2]);
line_data.sta_y = int.Parse(values[3]);
line_data.end_x = int.Parse(values[4]);
line_data.end_y = int.Parse(values[5]);
}
2025-10-24 14:40:54 +08:00
}
return line_data;
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.LinePos();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取线位置失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.LinePos();
2025-10-24 14:40:54 +08:00
}
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.ImagePos All_pos
2025-10-24 14:40:54 +08:00
{
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式获取所有位置: ip:param_mode,0$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_ALL_POS},0$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
// 创建默认的ImagePos对象
2025-10-27 10:39:33 +08:00
SharedStructures.ImagePos data = new SharedStructures.ImagePos();
data.area = new SharedStructures.AreaPos[6];
data.spot = new SharedStructures.SpotPos[6];
2025-10-24 14:40:54 +08:00
// 这里应该解析完整的响应数据
// 简化实现,返回默认值
return data;
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.ImagePos();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取所有位置失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.ImagePos();
2025-10-24 14:40:54 +08:00
}
}
}
public int Temp_range
{
set
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式设置温度范围: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_TEMP_RANGE},{value}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应
if (response != null)
{
Console.WriteLine("设置温度范围成功");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置温度范围失败: {ex.Message}");
}
}
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式获取温度范围: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.TEMP_RANGE}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
return ParseResponseValue(response);
2025-10-24 14:40:54 +08:00
}
2025-10-27 13:18:15 +08:00
return 0; // 默认范围
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取温度范围失败: {ex.Message}");
return 0;
}
}
}
public int Video_isp_x_offset
{
set
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式设置X偏移: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_ISP_X_OFFSET},{value}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应
if (response != null)
{
Console.WriteLine("设置X偏移成功");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置X偏移失败: {ex.Message}");
}
}
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式获取X偏移: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.ISP_X_OFFSET}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
return ParseResponseValue(response);
2025-10-24 14:40:54 +08:00
}
return 0;
}
catch (Exception ex)
{
Console.WriteLine($"获取X偏移失败: {ex.Message}");
return 0;
}
}
}
public int Video_isp_y_offset
{
2025-10-27 13:18:15 +08:00
set
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式设置Y偏移: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_ISP_Y_OFFSET},{value}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应
if (response != null)
{
Console.WriteLine("设置Y偏移成功");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置Y偏移失败: {ex.Message}");
}
}
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式获取Y偏移: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.ISP_Y_OFFSET}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
return ParseResponseValue(response);
2025-10-24 14:40:54 +08:00
}
return 0;
}
catch (Exception ex)
{
Console.WriteLine($"获取Y偏移失败: {ex.Message}");
return 0;
}
}
}
public int Video_isp_x_scale
{
set
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式设置X缩放: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_ISP_X_SCALE},{value}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应
if (response != null)
{
Console.WriteLine("设置X缩放成功");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置X缩放失败: {ex.Message}");
}
}
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式获取X缩放: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.ISP_X_SCALE}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
return ParseResponseValue(response);
2025-10-24 14:40:54 +08:00
}
2025-10-27 13:18:15 +08:00
return 100; // 默认100%
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取X缩放失败: {ex.Message}");
return 100;
}
}
}
public int Video_isp_y_scale
{
set
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式设置Y缩放: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_ISP_Y_SCALE},{value}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应
if (response != null)
{
Console.WriteLine("设置Y缩放成功");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置Y缩放失败: {ex.Message}");
}
}
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式获取Y缩放: ip:param_mode,param_value$
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.ISP_Y_SCALE}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
return ParseResponseValue(response);
2025-10-24 14:40:54 +08:00
}
2025-10-27 13:18:15 +08:00
return 100; // 默认100%
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取Y缩放失败: {ex.Message}");
return 100;
}
}
}
public int Set_led
{
set
{
try
{
2025-10-29 11:22:38 +08:00
// 使用SDK格式设置LED状态: +CMD:param_mode,param_value$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_LED},{value}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应
if (response != null)
{
Console.WriteLine("设置LED状态成功");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置LED失败: {ex.Message}");
}
}
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式获取LED状态: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.LED_STATUS}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
return ParseResponseValue(response);
2025-10-24 14:40:54 +08:00
}
2025-10-27 13:18:15 +08:00
return 0; // 默认关闭
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取LED状态失败: {ex.Message}");
return 0;
}
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.EmailServer Email_server
2025-10-24 14:40:54 +08:00
{
set
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式设置邮件服务器配置: ip:param_mode,param_value$
// 构建参数字符串包含enable标志
string paramsStr = $"{value.enable}";
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_EMAIL_SERVER},{paramsStr}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应
if (response != null)
{
Console.WriteLine("设置邮件服务器成功");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置邮件服务器失败: {ex.Message}");
}
}
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式获取邮件服务器配置: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.EMAIL_SERVER_CONFIG}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建EmailServer对象
SharedStructures.EmailServer emailServer = new SharedStructures.EmailServer();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return emailServer;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.EmailServer();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取邮件服务器失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.EmailServer();
2025-10-24 14:40:54 +08:00
}
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.TftpServer Tftp_server
2025-10-24 14:40:54 +08:00
{
set
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式设置TFTP服务器配置: ip:param_mode,param_value$
string paramsStr = $"{value.enable}";
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_TFTP_SERVER},{paramsStr}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应
if (response != null)
{
Console.WriteLine("设置TFTP服务器成功");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置TFTP服务器失败: {ex.Message}");
}
}
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SDK格式获取TFTP服务器配置: ip:param_mode,param_value$
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.TFTP_SERVER_CONFIG}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建TftpServer对象
SharedStructures.TftpServer tftpServer = new SharedStructures.TftpServer();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return tftpServer;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.TftpServer();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取TFTP服务器失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.TftpServer();
2025-10-24 14:40:54 +08:00
}
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.NetworkEth Network_eth
2025-10-24 14:40:54 +08:00
{
set
{
try
{
2025-10-29 11:22:38 +08:00
// 使用SDK格式设置网络参数: +CMD:param_mode,param_value$
2025-10-27 13:18:15 +08:00
string paramsStr = $"{value.enable}";
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_NETWORK_ETH},{paramsStr}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应
if (response != null)
{
Console.WriteLine("设置网络参数成功");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置网络参数失败: {ex.Message}");
}
}
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_PARAMETER命令获取网络参数
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.NETWORK_ETH_CONFIG}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建NetworkEth对象
SharedStructures.NetworkEth networkEth = new SharedStructures.NetworkEth();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return networkEth;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.NetworkEth();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取网络参数失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.NetworkEth();
2025-10-24 14:40:54 +08:00
}
}
}
public int Fusion_distance
{
set
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SET_FUSION_DISTANCE命令设置融合距离 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_FUSION_DISTANCE},{value}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应
if (response != null)
{
Console.WriteLine("设置融合距离成功");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置融合距离失败: {ex.Message}");
}
}
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_PARAMETER命令获取融合距离 - SDK格式
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.FUSION_DISTANCE}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
return ParseResponseValue(response);
2025-10-24 14:40:54 +08:00
}
2025-10-27 13:18:15 +08:00
return 1000; // 默认1米
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取融合距离失败: {ex.Message}");
return 1000;
}
}
}
2025-10-27 10:39:33 +08:00
public void Set_envir_param(SharedStructures.EnvirParam data)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SET_ENVIR_PARAM命令设置环境参数 - SDK格式
// 构建参数字符串,包含所有环境参数
string paramsStr = $"{data.method},{data.num},{data.emissivity},{data.airTemp},{data.targetTemp},{data.atmosTrans},{data.distance},{data.infraredTemp},{data.infraredRadia}";
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_ENVIR_PARAM},{paramsStr}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应
if (response != null)
{
Console.WriteLine("设置环境参数成功");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置环境参数失败: {ex.Message}");
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.EnvirParam Get_area_envir_param(int index)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_AREA_ENVIR_PARAM命令获取区域环境参数 - SDK格式
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_AREA_ENVIR_PARAM},{index}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建EnvirParam对象
SharedStructures.EnvirParam envirParam = new SharedStructures.EnvirParam();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return envirParam;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.EnvirParam();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取区域环境参数失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.EnvirParam();
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.EnvirParam Get_spot_envir_param(int index)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_SPOT_ENVIR_PARAM命令获取点环境参数 - SDK格式
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_SPOT_ENVIR_PARAM},{index}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建EnvirParam对象
SharedStructures.EnvirParam envirParam = new SharedStructures.EnvirParam();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return envirParam;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.EnvirParam();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取点环境参数失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.EnvirParam();
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.EnvirParam Get_line_envir_param()
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_LINE_ENVIR_PARAM命令获取线环境参数 - SDK格式
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_LINE_ENVIR_PARAM}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建EnvirParam对象
SharedStructures.EnvirParam envirParam = new SharedStructures.EnvirParam();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return envirParam;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.EnvirParam();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取线环境参数失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.EnvirParam();
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.EnvirParam Get_globa_envir_param()
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_GLOBAL_ENVIR_PARAM命令获取全局环境参数 - SDK格式
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_GLOBAL_ENVIR_PARAM}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建EnvirParam对象
SharedStructures.EnvirParam envirParam = new SharedStructures.EnvirParam();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return envirParam;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.EnvirParam();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取全局环境参数失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.EnvirParam();
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public void Set_alarm_param(SharedStructures.AlarmParam data)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SET_ALARM_PARAM命令设置报警参数 - SDK格式
2025-10-29 11:22:38 +08:00
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}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应是否成功
if (response != null)
{
// 命令执行成功
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置报警参数失败: {ex.Message}");
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.AlarmParam Get_area_alarm_param(int index)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_AREA_ALARM_PARAM命令获取区域报警参数 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_AREA_ALARM_PARAM},{index}$";
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建AlarmParam对象
SharedStructures.AlarmParam alarmParam = new SharedStructures.AlarmParam();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return alarmParam;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.AlarmParam();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取区域报警参数失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.AlarmParam();
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.AlarmParam Get_spot_alarm_param(int index)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_SPOT_ALARM_PARAM命令获取点报警参数 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_SPOT_ALARM_PARAM},{index}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建AlarmParam对象
SharedStructures.AlarmParam alarmParam = new SharedStructures.AlarmParam();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return alarmParam;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.AlarmParam();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取点报警参数失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.AlarmParam();
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.AlarmParam Get_globa_alarm_param()
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_GLOBAL_ALARM_PARAM命令获取全局报警参数 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_GLOBAL_ALARM_PARAM}$";
2025-10-27 10:39:33 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-27 10:39:33 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建AlarmParam对象
SharedStructures.AlarmParam alarmParam = new SharedStructures.AlarmParam();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return alarmParam;
2025-10-27 10:39:33 +08:00
}
return new SharedStructures.AlarmParam();
}
catch (Exception ex)
{
Console.WriteLine($"获取全局报警参数失败: {ex.Message}");
return new SharedStructures.AlarmParam();
}
}
public SharedStructures.AlarmParam Get_line_alarm_param()
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_LINE_ALARM_PARAM命令获取线报警参数 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_LINE_ALARM_PARAM}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建AlarmParam对象
SharedStructures.AlarmParam alarmParam = new SharedStructures.AlarmParam();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return alarmParam;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.AlarmParam();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取线报警参数失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.AlarmParam();
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.AreaTemp Get_area_temp(int index)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_AREA_TEMP命令获取区域温度 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_AREA_TEMP},{index}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建AreaTemp对象
SharedStructures.AreaTemp areaTemp = new SharedStructures.AreaTemp();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return areaTemp;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.AreaTemp();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取区域温度失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.AreaTemp();
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.SpotTemp Get_spot_temp(int index)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_SPOT_TEMP命令获取点温度 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_SPOT_TEMP},{index}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建SpotTemp对象
SharedStructures.SpotTemp spotTemp = new SharedStructures.SpotTemp();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return spotTemp;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.SpotTemp();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取点温度失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.SpotTemp();
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.LineTemp Get_line_temp()
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_LINE_TEMP命令获取线温度 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_LINE_TEMP}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建LineTemp对象
SharedStructures.LineTemp lineTemp = new SharedStructures.LineTemp();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return lineTemp;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.LineTemp();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取线温度失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.LineTemp();
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.GlobaTemp Get_globa_temp()
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_GLOBAL_TEMP命令获取全局温度 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_GLOBAL_TEMP}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建GlobaTemp对象
SharedStructures.GlobaTemp globaTemp = new SharedStructures.GlobaTemp();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return globaTemp;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.GlobaTemp();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取全局温度失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.GlobaTemp();
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public SharedStructures.ImageTemp Get_all_temp()
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_ALL_TEMP命令获取所有温度数据 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_ALL_TEMP}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应并创建ImageTemp对象
SharedStructures.ImageTemp imageTemp = new SharedStructures.ImageTemp();
// 这里可以根据实际响应格式进行解析
// 目前返回默认对象
return imageTemp;
2025-10-24 14:40:54 +08:00
}
2025-10-27 10:39:33 +08:00
return new SharedStructures.ImageTemp();
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"获取所有温度数据失败: {ex.Message}");
2025-10-27 10:39:33 +08:00
return new SharedStructures.ImageTemp();
2025-10-24 14:40:54 +08:00
}
}
public void Power_reboot()
{
try
{
2025-10-27 13:18:15 +08:00
// 使用POWER_REBOOT命令重启设备 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.POWER_REBOOT}$";
if (SendCommand(command, out string response))
{
// 验证响应是否成功
if (response != null)
{
Console.WriteLine("设备重启命令已发送");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设备重启失败: {ex.Message}");
}
}
public void Param_recover()
{
try
{
2025-10-27 13:18:15 +08:00
// 使用PARAM_RECOVER命令恢复参数 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.PARAM_RECOVER}$";
if (SendCommand(command, out string response))
{
// 验证响应是否成功
if (response != null)
{
Console.WriteLine("参数恢复命令已发送");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"参数恢复失败: {ex.Message}");
}
}
public void Update()
{
try
{
2025-10-27 13:18:15 +08:00
// 使用UPDATE命令更新设备 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.UPDATE}$";
if (SendCommand(command, out string response))
{
// 验证响应是否成功
if (response != null)
{
Console.WriteLine("设备更新命令已发送");
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设备更新失败: {ex.Message}");
}
}
2025-10-29 11:22:38 +08:00
// 使用SendCommand方法发送心跳命令与SDK保持一致
2025-10-24 14:40:54 +08:00
public int Heartbeat()
{
try
{
2025-10-27 15:36:18 +08:00
// 根据SDK实际实现心跳命令格式是 +CMD:24$
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.HEARTBEAT}$";
2025-10-24 14:40:54 +08:00
2025-10-27 15:36:18 +08:00
// SDK实现中会尝试3次心跳这里也采用相同的策略
for (int retry = 0; retry < 3; retry++)
2025-10-24 14:40:54 +08:00
{
2025-10-27 15:36:18 +08:00
Console.WriteLine($"心跳检测...(尝试 {retry + 1}/3)");
2025-10-29 11:22:38 +08:00
if (SendCommand(command, out string response))
2025-10-27 13:18:15 +08:00
{
2025-10-29 11:22:38 +08:00
// SDK要求响应中必须包含 ":ok" 字符串才算成功
if (!string.IsNullOrEmpty(response) && response.Contains(":ok"))
2025-10-27 15:36:18 +08:00
{
2025-10-29 11:22:38 +08:00
Console.WriteLine("心跳成功: 响应包含':ok'");
return 1; // 返回1表示成功与DeviceManager中的heartbeatResult > 0判断一致
2025-10-27 15:36:18 +08:00
}
2025-10-28 10:29:11 +08:00
else
2025-10-27 15:36:18 +08:00
{
2025-10-29 11:22:38 +08:00
Console.WriteLine($"心跳响应不包含':ok',验证失败。收到的响应: '{response}'");
2025-10-27 15:36:18 +08:00
}
}
// 如果不是最后一次尝试,短暂延迟后重试
if (retry < 2)
{
Thread.Sleep(100);
2025-10-27 13:18:15 +08:00
}
2025-10-24 14:40:54 +08:00
}
2025-10-27 15:36:18 +08:00
2025-10-27 16:18:24 +08:00
// 所有重试都失败了
Console.WriteLine("三次心跳检测均失败,连接未建立");
2025-10-24 14:40:54 +08:00
return -1;
}
catch (Exception ex)
{
Console.WriteLine($"心跳检测异常: {ex.Message}");
return -1;
}
}
public void Set_uart(int nSpeed, int nBits, char nEvent, int nStop)
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SET_UART命令设置串口参数 - SDK格式
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_UART},{nSpeed},{nBits},{(int)nEvent},{nStop}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应是否成功
if (response != null)
{
// 命令执行成功
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置串口失败: {ex.Message}");
}
}
public void Send_uart_command(byte[] cmd)
{
try
{
2025-10-27 13:18:15 +08:00
// 将byte数组转换为十六进制字符串格式
string hexData = BitConverter.ToString(cmd).Replace("-", "");
// 使用SEND_UART_COMMAND命令发送串口命令 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.SEND_UART_COMMAND},{hexData}$";
2025-10-24 14:40:54 +08:00
SendCommand(command, out _);
}
catch (Exception ex)
{
Console.WriteLine($"发送串口命令失败: {ex.Message}");
}
}
2025-10-27 10:39:33 +08:00
public void Autofocus(SharedStructures.FocusParam data)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用AUTOFOCUS命令设置自动对焦参数 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.AUTOFOCUS},{data.method},{data.x},{data.y},{data.width},{data.height}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应是否成功
if (response != null)
{
// 命令执行成功
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"自动对焦失败: {ex.Message}");
}
}
public void Set_device_name(string data)
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SET_DEVICE_NAME命令设置设备名称 - SDK格式
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_DEVICE_NAME},{data}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应是否成功
if (response != null)
{
// 命令执行成功
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置设备名称失败: {ex.Message}");
}
}
public string Get_device_name()
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_PARAMETER命令获取设备名称 - SDK格式
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_PARAMETER},{(int)PARAM_TYPE.DEVICE_NA}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应中的设备名称
// 格式: +RET:1:设备名称$ 或 +RET:设备名称$
if (response != null && response.StartsWith("+RET:"))
{
int paramTypeEndPos = response.IndexOf(':', 5);
int nameStartPos = (paramTypeEndPos > 0) ? paramTypeEndPos + 1 : 5;
int endPos = response.IndexOf('$', nameStartPos);
if (endPos > nameStartPos)
{
return response.Substring(nameStartPos, endPos - nameStartPos);
}
}
2025-10-24 14:40:54 +08:00
}
return string.Empty;
}
catch (Exception ex)
{
Console.WriteLine($"获取设备名称失败: {ex.Message}");
return string.Empty;
}
}
public byte[] Get_detect_number()
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_DETECT_NUMBER命令获取检测编号 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_DETECT_NUMBER}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应,返回检测编号数据
// 响应格式: +RET:0:数据内容$ 或 +RET:数据内容$
if (response != null && response.StartsWith("+RET:"))
{
// 解析响应中的数据部分
int startPos = response.IndexOf(':', 5) + 1;
int endPos = response.IndexOf('$', startPos);
if (endPos > startPos)
{
string dataStr = response.Substring(startPos, endPos - startPos);
// 将十六进制字符串转换为字节数组
// 注意:这里需要根据实际响应格式进行适当的转换
return Encoding.ASCII.GetBytes(dataStr);
}
}
2025-10-24 14:40:54 +08:00
}
return new byte[0];
}
catch (Exception ex)
{
Console.WriteLine($"获取检测编号失败: {ex.Message}");
return new byte[0];
}
}
public char Temp_frame
{
set
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SET_TEMP_FRAME命令设置温度帧 - SDK格式
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_TEMP_FRAME},{(byte)value}$";
2025-10-27 13:18:15 +08:00
2025-10-24 14:40:54 +08:00
SendCommand(command, out _);
}
catch (Exception ex)
{
Console.WriteLine($"设置温度帧失败: {ex.Message}");
}
}
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_TEMP_FRAME命令获取温度帧 - SDK格式
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.GET_TEMP_FRAME}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应中的温度帧值
if (response != null && response.StartsWith("+RET:"))
{
int valueStartPos = response.IndexOf(':', 5) + 1;
int endPos = response.IndexOf('$', valueStartPos);
if (endPos > valueStartPos)
{
string valueStr = response.Substring(valueStartPos, endPos - valueStartPos);
if (byte.TryParse(valueStr, out byte result))
{
return (char)result;
}
}
}
2025-10-24 14:40:54 +08:00
}
return (char)0;
}
catch (Exception ex)
{
Console.WriteLine($"获取温度帧失败: {ex.Message}");
return (char)0;
}
}
}
public char Alarm
{
set
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SET_ALARM命令设置报警输出 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.SET_ALARM},{(byte)value}$";
2025-10-24 14:40:54 +08:00
SendCommand(command, out _);
}
catch (Exception ex)
{
Console.WriteLine($"设置报警输出失败: {ex.Message}");
}
}
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_ALARM命令获取报警输出状态 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_ALARM}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应中的报警状态值
if (response != null && response.StartsWith("+RET:"))
{
int valueStartPos = response.IndexOf(':', 5) + 1;
int endPos = response.IndexOf('$', valueStartPos);
if (endPos > valueStartPos)
{
string valueStr = response.Substring(valueStartPos, endPos - valueStartPos);
if (byte.TryParse(valueStr, out byte result))
{
return (char)result;
}
}
}
2025-10-24 14:40:54 +08:00
}
return (char)0;
}
catch (Exception ex)
{
Console.WriteLine($"获取报警输入失败: {ex.Message}");
return (char)0;
}
}
}
public int Comp_temp
{
get
{
try
{
2025-10-27 13:18:15 +08:00
// 使用GET_COMP_TEMP命令获取补偿温度 - SDK格式
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_COMP_TEMP}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-24 14:40:54 +08:00
{
2025-10-27 13:18:15 +08:00
// 解析响应中的补偿温度值
if (response != null && response.StartsWith("+RET:"))
{
int valueStartPos = response.IndexOf(':', 5) + 1;
int endPos = response.IndexOf('$', valueStartPos);
if (endPos > valueStartPos)
{
string valueStr = response.Substring(valueStartPos, endPos - valueStartPos);
if (int.TryParse(valueStr, out int result))
{
return result;
}
}
}
2025-10-24 14:40:54 +08:00
}
}
catch (Exception ex)
{
Console.WriteLine($"获取补偿温度失败: {ex.Message}");
}
2025-10-27 13:18:15 +08:00
return 2500;
2025-10-24 14:40:54 +08:00
}
}
2025-10-27 10:39:33 +08:00
public void Set_time(SharedStructures.TimeParam data)
2025-10-24 14:40:54 +08:00
{
try
{
2025-10-27 13:18:15 +08:00
// 使用SET_TIME命令设置系统时间 - SDK格式
2025-10-29 11:22:38 +08:00
string command = $"{CMD_HEAD}:{(int)CMD_TYPE.SET_TIME},{data.year},{data.month},{data.day},{data.hour},{data.minute},{data.second}$";
2025-10-24 14:40:54 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
{
// 验证响应是否成功
if (response != null)
{
// 命令执行成功
}
}
2025-10-24 14:40:54 +08:00
}
catch (Exception ex)
{
Console.WriteLine($"设置系统时间失败: {ex.Message}");
}
}
2025-10-29 13:13:31 +08:00
// 获取视频模式
public int GetVideoMode()
{
2025-10-29 13:47:51 +08:00
return GetVideoModeInternal();
2025-10-29 13:13:31 +08:00
}
// 设置视频模式
public void SetVideoMode(int mode)
{
2025-10-29 13:47:51 +08:00
SetVideoModeInternal(mode);
2025-10-29 13:13:31 +08:00
}
2025-10-27 10:39:33 +08:00
// 新增方法:获取图像数据
public byte[] GetImageData()
{
try
{
2025-10-27 13:18:15 +08:00
// 根据SDK实现使用正确的命令格式获取图像数据
// 格式为:"ip:param_mode,param_value$"
string command = $"{deviceIp}:{(int)CMD_TYPE.GET_IMAGE_DATA},0$";
2025-10-27 10:39:33 +08:00
2025-10-27 13:18:15 +08:00
if (SendCommand(command, out string response))
2025-10-27 10:39:33 +08:00
{
2025-10-27 13:18:15 +08:00
// 处理图像数据响应
// 注意:由于图像数据可能较大且为二进制,这里需要根据实际响应格式进行解析
if (response != null)
{
// 查找响应中的数据部分
int endPos = response.IndexOf('$');
if (endPos > 0)
{
string dataStr = response.Substring(0, endPos);
// 假设图像数据是base64编码的需要解码
try
{
return Convert.FromBase64String(dataStr);
}
catch
{
// 如果不是base64编码返回空数组
return new byte[0];
}
}
}
2025-10-27 10:39:33 +08:00
}
return new byte[0];
}
catch (Exception ex)
{
Console.WriteLine($"获取图像数据失败: {ex.Message}");
return new byte[0];
}
2025-10-28 16:43:18 +08:00
}
2025-10-24 14:40:54 +08:00
}
2025-10-28 16:43:18 +08:00
}