diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/A8SDK.cs b/Windows/CS/Framework4.0/Toprie/Toprie/A8SDK.cs index c25275b..2981f6a 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/A8SDK.cs +++ b/Windows/CS/Framework4.0/Toprie/Toprie/A8SDK.cs @@ -202,82 +202,92 @@ namespace JoyD.Windows.CS.Toprie return V8.SDK_serch_device(list_len); } + // 快门校正方法 public void Shutter_correction() { v8Instance.Shutter_correction(); } + // 快门次数属性 public int Shutter_times { get { return v8Instance.Shutter_times; } - set + set { v8Instance.Shutter_times = value; } } + // 色板属性 public int Color_plate { get { return v8Instance.Color_plate; } - set + set { v8Instance.Color_plate = value; } } + // 镜像模式属性 public int Mirror_mode { get { return v8Instance.Mirror_mode; } - set + set { v8Instance.Mirror_mode = value; } } + // 视频模式属性 public int Video_mode { get { return v8Instance.Video_mode; } - set + set { v8Instance.Video_mode = value; } } - public void Set_area_pos(int index, AreaPos area_data) + // 设置区域位置 + public void Set_area_pos(int index, SharedStructures.AreaPos area_data) { v8Instance.Set_area_pos(index, area_data); } - public AreaPos Get_area_pos(int index) + // 获取区域位置 + public SharedStructures.AreaPos Get_area_pos(int index) { return v8Instance.Get_area_pos(index); } - public void Set_spot_pos(int index, SpotPos spot_data) + // 设置点位置 + public void Set_spot_pos(int index, SharedStructures.SpotPos spot_data) { v8Instance.Set_spot_pos(index, spot_data); } - public SpotPos Get_spot_pos(int index) + // 获取点位置 + public SharedStructures.SpotPos Get_spot_pos(int index) { return v8Instance.Get_spot_pos(index); } - public LinePos Line_pos + // 线位置属性 + public SharedStructures.LinePos Line_pos { - set + set { v8Instance.Line_pos = value; } @@ -287,29 +297,32 @@ namespace JoyD.Windows.CS.Toprie } } - public ImagePos All_pos + // 所有位置属性 + public SharedStructures.ImagePos All_pos { - get + get { return v8Instance.All_pos; } } + // 温度范围属性 public int Temp_range { - set + set { v8Instance.Temp_range = value; } - get + get { return v8Instance.Temp_range; } } + // 视频ISP X偏移属性 public int Video_isp_x_offset { - set + set { v8Instance.Video_isp_x_offset = value; } @@ -319,9 +332,10 @@ namespace JoyD.Windows.CS.Toprie } } + // 视频ISP Y偏移属性 public int Video_isp_y_offset { - set + set { v8Instance.Video_isp_y_offset = value; } @@ -331,9 +345,10 @@ namespace JoyD.Windows.CS.Toprie } } + // 视频ISP X缩放属性 public int Video_isp_x_scale { - set + set { v8Instance.Video_isp_x_scale = value; } @@ -343,9 +358,10 @@ namespace JoyD.Windows.CS.Toprie } } + // 视频ISP Y缩放属性 public int Video_isp_y_scale { - set + set { v8Instance.Video_isp_y_scale = value; } @@ -355,9 +371,10 @@ namespace JoyD.Windows.CS.Toprie } } + // LED设置属性 public int Set_led { - set + set { v8Instance.Set_led = value; } @@ -367,9 +384,10 @@ namespace JoyD.Windows.CS.Toprie } } - public EmailServer Email_server + // 邮件服务器属性 + public SharedStructures.EmailServer Email_server { - set + set { v8Instance.Email_server = value; } @@ -379,9 +397,10 @@ namespace JoyD.Windows.CS.Toprie } } - public TftpServer Tftp_server + // TFTP服务器属性 + public SharedStructures.TftpServer Tftp_server { - set + set { v8Instance.Tftp_server = value; } @@ -391,7 +410,8 @@ namespace JoyD.Windows.CS.Toprie } } - public NetworkEth Network_eth + // 网络以太网属性 + public SharedStructures.NetworkEth Network_eth { set { @@ -403,9 +423,10 @@ namespace JoyD.Windows.CS.Toprie } } + // 融合距离属性 public int Fusion_distance { - set + set { v8Instance.Fusion_distance = value; } @@ -415,134 +436,160 @@ namespace JoyD.Windows.CS.Toprie } } - public void Set_envir_param(EnvirParam data) + // 设置环境参数 + public void Set_envir_param(SharedStructures.EnvirParam data) { v8Instance.Set_envir_param(data); } - public EnvirParam Get_area_envir_param(int index) + // 获取区域环境参数 + public SharedStructures.EnvirParam Get_area_envir_param(int index) { return v8Instance.Get_area_envir_param(index); } - public EnvirParam Get_spot_envir_param(int index) + // 获取点环境参数 + public SharedStructures.EnvirParam Get_spot_envir_param(int index) { return v8Instance.Get_spot_envir_param(index); } - public EnvirParam Get_line_envir_param() + // 获取线环境参数 + public SharedStructures.EnvirParam Get_line_envir_param() { return v8Instance.Get_line_envir_param(); } - public EnvirParam Get_globa_envir_param() + // 获取全局环境参数 + public SharedStructures.EnvirParam Get_globa_envir_param() { return v8Instance.Get_globa_envir_param(); } - public void Set_alarm_param(AlarmParam data) + // 设置报警参数 + public void Set_alarm_param(SharedStructures.AlarmParam data) { v8Instance.Set_alarm_param(data); } - public AlarmParam Get_area_alarm_param(int index) + // 获取区域报警参数 + public SharedStructures.AlarmParam Get_area_alarm_param(int index) { return v8Instance.Get_area_alarm_param(index); } - public AlarmParam Get_spot_alarm_param(int index) + // 获取点报警参数 + public SharedStructures.AlarmParam Get_spot_alarm_param(int index) { return v8Instance.Get_spot_alarm_param(index); } - public AlarmParam Get_globa_alarm_param() + // 获取全局报警参数 + public SharedStructures.AlarmParam Get_globa_alarm_param() { return v8Instance.Get_globa_alarm_param(); } - public AlarmParam Get_line_alarm_param() + // 获取线报警参数 + public SharedStructures.AlarmParam Get_line_alarm_param() { return v8Instance.Get_line_alarm_param(); } - public AreaTemp Get_area_temp(int index) + // 获取区域温度 + public SharedStructures.AreaTemp Get_area_temp(int index) { return v8Instance.Get_area_temp(index); } - public SpotTemp Get_spot_temp(int index) + // 获取点温度 + public SharedStructures.SpotTemp Get_spot_temp(int index) { return v8Instance.Get_spot_temp(index); } - public LineTemp Get_line_temp() + // 获取线温度 + public SharedStructures.LineTemp Get_line_temp() { return v8Instance.Get_line_temp(); } - public GlobaTemp Get_globa_temp() + // 获取全局温度 + public SharedStructures.GlobaTemp Get_globa_temp() { return v8Instance.Get_globa_temp(); } - public ImageTemp Get_all_temp() + // 获取所有温度 + public SharedStructures.ImageTemp Get_all_temp() { return v8Instance.Get_all_temp(); } + // 电源重启 public void Power_reboot() { v8Instance.Power_reboot(); } + // 参数恢复 public void Param_recover() { v8Instance.Param_recover(); } + // 更新 public void Update() { v8Instance.Update(); } + // 心跳检测 public int Heartbeat() { return v8Instance.Heartbeat(); } + // 设置串口 public void Set_uart(int nSpeed, int nBits, char nEvent, int nStop) { v8Instance.Set_uart(nSpeed, nBits, nEvent, nStop); } + // 发送串口命令 public void Send_uart_command(byte[] cmd) { v8Instance.Send_uart_command(cmd); } - public void Autofocus(FocusParam data) + // 自动对焦 + public void Autofocus(SharedStructures.FocusParam data) { v8Instance.Autofocus(data); } + // 设置设备名称 public void Set_device_name(string data) { v8Instance.Set_device_name(data); } + // 获取设备名称 public string Get_device_name() { return v8Instance.Get_device_name(); } + // 获取检测数量 public byte[] Get_detect_number() { return v8Instance.Get_detect_number(); } + // 温度帧属性 public char Temp_frame { - set + set { v8Instance.Temp_frame = value; } @@ -552,9 +599,10 @@ namespace JoyD.Windows.CS.Toprie } } + // 报警属性 public char Alarm { - set + set { v8Instance.Alarm = value; } @@ -564,7 +612,8 @@ namespace JoyD.Windows.CS.Toprie } } - public int Comp_temp + // 补偿温度属性 + public float Comp_temp { get { @@ -572,9 +621,42 @@ namespace JoyD.Windows.CS.Toprie } } - public void Set_time(TimeParam data) + // 设置时间 + public void Set_time(SharedStructures.TimeParam data) { v8Instance.Set_time(data); } + + // 设置图像模式 + public void SetImageMode(int mode) + { + v8Instance.SetImageMode(mode); + } + + // 获取图像数据 + public byte[] GetImageData() + { + return v8Instance.GetImageData(); + } + + // 设备搜索 + public static List SearchDevices() + { + List devices = new List(); + const int maxDevices = 10; + string searchResult = SDK_serch_device(maxDevices); + if (!string.IsNullOrEmpty(searchResult)) + { + string[] deviceList = searchResult.Split(';'); + foreach (string device in deviceList) + { + if (!string.IsNullOrEmpty(device)) + { + devices.Add(device); + } + } + } + return devices; + } } } \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Camera.cs b/Windows/CS/Framework4.0/Toprie/Toprie/Camera.cs index 626decf..4a55c21 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/Camera.cs +++ b/Windows/CS/Framework4.0/Toprie/Toprie/Camera.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -63,10 +64,12 @@ namespace JoyD.Windows.CS.Toprie _deviceManager = new DeviceManager { AutoReconnectEnabled = true, - ReconnectInterval = 2000, // 2秒 - MaxReconnectAttempts = 5 + ReconnectInterval = 2000 // 2秒 }; + // 设置静态属性 + DeviceManager.MaxReconnectAttempts = 5; + // 注册图像接收事件 _deviceManager.ImageReceived += DeviceManager_ImageReceived; @@ -227,12 +230,61 @@ namespace JoyD.Windows.CS.Toprie /// private void DeviceManager_ImageReceived(object sender, ImageReceivedEventArgs e) { - // 不在这里使用using,因为我们需要将图像传递给UI线程 - Image image = e.ImageData; + Image image = null; + try + { + if (e.ImageData != null && e.ImageData.Length > 0) + { + // 创建内存流并从流中创建图像 + using (MemoryStream ms = new MemoryStream(e.ImageData)) + { + // 检查流是否可读且有效 + if (ms.CanRead && ms.Length > 0) + { + // 从流中创建图像 + using (Image tempImage = System.Drawing.Image.FromStream(ms)) + { + // 创建一个全新的位图而不仅仅是克隆,确保数据完整性 + image = new Bitmap(tempImage); + + // 立即验证新创建的图像是否有效 + try + { + // 访问Width和Height属性来验证图像是否有效 + int width = image.Width; + int height = image.Height; + if (width <= 0 || height <= 0) + { + Console.WriteLine("创建的图像尺寸无效"); + image.Dispose(); + image = null; + } + } + catch (Exception) + { + Console.WriteLine("创建的图像无效"); + image.Dispose(); + image = null; + } + } + } + } + } + } + catch (Exception ex) + { + Console.WriteLine($"创建图像失败: {ex.Message}"); + // 确保在异常情况下释放资源 + if (image != null) + { + image.Dispose(); + image = null; + } + } if (image == null) { - Console.WriteLine("接收到空图像数据"); + Console.WriteLine("接收到空或无效的图像数据"); return; } @@ -267,20 +319,28 @@ namespace JoyD.Windows.CS.Toprie { try { + // 创建本地副本以避免闭包问题 + Image imageForUI = clonedImage; + clonedImage = null; // 防止在异步操作期间被修改 + // 使用BeginInvoke在UI线程上更新图像,避免可能的死锁问题 this.BeginInvoke(new Action(() => { try { - UpdateImageOnUI(clonedImage); + UpdateImageOnUI(imageForUI); } catch (Exception ex) { Console.WriteLine($"更新UI图像失败: {ex.Message}"); // 如果UI更新失败,确保克隆的图像被释放 - if (clonedImage != null) + if (imageForUI != null) { - clonedImage.Dispose(); + try + { + imageForUI.Dispose(); + } + catch {} } } })); @@ -288,7 +348,15 @@ namespace JoyD.Windows.CS.Toprie catch (Exception) { // 异常情况下确保释放图像资源 - clonedImage?.Dispose(); + if (clonedImage != null) + { + try + { + clonedImage.Dispose(); + } + catch {} + clonedImage = null; + } throw; } } @@ -327,12 +395,76 @@ namespace JoyD.Windows.CS.Toprie /// private void UpdateImageOnUI(Image image) { + // 线程安全检查 - 确保在UI线程上执行 + if (this.InvokeRequired) + { + try + { + this.BeginInvoke(new Action(UpdateImageOnUI), image); + } + catch (ObjectDisposedException) + { + // 如果控件已被释放,确保释放图像资源 + if (image != null) + { + try { image.Dispose(); } catch {} + } + Console.WriteLine("控件已释放,跳过UI更新"); + } + return; + } // 空值检查 if (image == null) { Console.WriteLine("传入UpdateImageOnUI的图像为空"); return; } + // 连接状态检查 - 只在设备实际连接时处理图像 + if (_deviceManager.ConnectionStatus != ConnectionStatus.Connected) + { + Console.WriteLine("设备未连接,跳过图像更新"); + try { image.Dispose(); } catch {} + return; + } + + // 增强图像有效性检查 + bool isImageValid = false; + Image safeImage = null; + + try + { + // 创建一个安全的图像副本,确保原图像不被破坏 + safeImage = new Bitmap(image); + + // 预先验证图像是否有效 + if (safeImage.Width > 0 && safeImage.Height > 0) + { + // 尝试访问图像的像素数据,这是更严格的验证方式 + using (Graphics g = Graphics.FromImage(new Bitmap(1, 1))) + { + // 简单绘制操作验证图像是否可绘制 + g.DrawImage(safeImage, 0, 0, 1, 1); + } + isImageValid = true; + Console.WriteLine($"图像验证成功: {safeImage.Width}x{safeImage.Height}"); + } + else + { + Console.WriteLine($"图像尺寸无效: {safeImage.Width}x{safeImage.Height}"); + } + } + catch (Exception ex) + { + Console.WriteLine($"图像验证失败: {ex.Message}"); + // 释放临时创建的安全图像 + if (safeImage != null) + { + try { safeImage.Dispose(); } catch {} + } + // 释放原始图像 + try { image.Dispose(); } catch {} + return; + } try { @@ -340,7 +472,8 @@ namespace JoyD.Windows.CS.Toprie if (this.IsDisposed || imageBox == null || imageBox.IsDisposed) { // 如果控件已释放,确保图像也被释放 - image.Dispose(); + try { safeImage.Dispose(); } catch {} + try { image.Dispose(); } catch {} Console.WriteLine("控件已释放,无法更新图像"); return; } @@ -350,28 +483,45 @@ namespace JoyD.Windows.CS.Toprie try { - // 尝试设置新图像 - imageBox.Image = image; - - // 验证图像是否成功设置 - if (imageBox.Image != image) + // 确保控件仍处于有效状态 + if (imageBox.IsDisposed || !isImageValid || safeImage == null) { - Console.WriteLine("图像设置失败,可能参数无效"); - image.Dispose(); // 释放未成功设置的图像 + try { safeImage.Dispose(); } catch {} + try { image.Dispose(); } catch {} return; } - // 仅在新图像成功设置后释放旧图像 - if (oldImage != null && oldImage != image) // 防止自引用释放 + // 尝试设置新图像(使用验证过的安全副本) + imageBox.Image = safeImage; + + // 验证图像是否成功设置 + if (imageBox.Image != safeImage) { - oldImage.Dispose(); + Console.WriteLine("图像设置失败,可能参数无效"); + try { safeImage.Dispose(); } catch {} + try { image.Dispose(); } catch {} + return; + } + + // 释放原始图像,因为我们现在使用的是安全副本 + try { image.Dispose(); } catch {} + + // 仅在新图像成功设置后释放旧图像 + if (oldImage != null && oldImage != safeImage) // 防止自引用释放 + { + try + { + oldImage.Dispose(); + } + catch {} } } catch (ArgumentException ex) when (ex.Message.Contains("参数无效")) { // 特别处理"参数无效"异常 - Console.WriteLine($"图像参数无效: {ex.Message}"); - image.Dispose(); // 释放无效图像 + Console.WriteLine($"图像参数无效异常: {ex.Message}"); + try { safeImage.Dispose(); } catch {} + try { image.Dispose(); } catch {} // 尝试设置旧图像回来,如果可能的话 if (oldImage != null) @@ -383,20 +533,17 @@ namespace JoyD.Windows.CS.Toprie catch { // 如果设置旧图像也失败,释放它 - oldImage.Dispose(); + try { oldImage.Dispose(); } catch {} } } } } catch (Exception ex) { - Console.WriteLine($"更新图像UI失败: {ex.Message}"); - // 确保在任何异常情况下都释放传入的图像资源 - try - { - image.Dispose(); - } - catch {} + Console.WriteLine($"更新图像UI异常: {ex.Message}"); + // 确保在任何异常情况下都释放所有图像资源 + try { safeImage.Dispose(); } catch {} + try { image.Dispose(); } catch {} } } @@ -433,57 +580,94 @@ namespace JoyD.Windows.CS.Toprie /// private void HandleConnectionStatusChanged(ConnectionStatusChangedEventArgs e) { - // 更新UI状态 - UpdateUIState(e.NewStatus == ConnectionStatus.Connected); - - switch (e.NewStatus) + try { - case ConnectionStatus.Connected: - Console.WriteLine("设备已连接"); - - // 清除错误信息 - ShowError(string.Empty); - - // 确保设置为热图模式 - try - { - _deviceManager.SetImageMode(ImageMode.Thermal); - Console.WriteLine("连接成功后确认热图模式"); - } - catch (Exception ex) - { - Console.WriteLine($"连接成功后设置热图模式失败: {ex.Message}"); - } - - if (!_isReceivingImage) - { - StartReceiveImage(); - } - break; - case ConnectionStatus.Disconnected: - Console.WriteLine("设备已断开连接"); - - // 停止接收图像 - if (_isReceivingImage) - { - _deviceManager.StopReceiveImage(); - _isReceivingImage = false; - } - - if (!string.IsNullOrEmpty(e.Message)) - { - ShowError(e.Message); - } - else - { - ShowError("设备连接已断开"); - } - break; - case ConnectionStatus.Connecting: - case ConnectionStatus.Reconnecting: - Console.WriteLine($"正在连接设备...{(!string.IsNullOrEmpty(e.Message) ? " " + e.Message : "")}"); - ShowError(string.Empty); // 清除之前的错误信息 - break; + // 确保在UI线程上更新UI状态 + if (this.InvokeRequired) + { + this.Invoke(new Action(HandleConnectionStatusChanged), e); + return; + } + + // 更新UI状态 + UpdateUIState(e.Status == ConnectionStatus.Connected); + + // 检查_deviceManager是否为空 + if (_deviceManager == null) + { + Console.WriteLine("设备管理器未初始化"); + return; + } + + switch (e.Status) + { + case ConnectionStatus.Connected: + Console.WriteLine("设备已连接"); + + // 清除错误信息 + ShowError(string.Empty); + + // 确保设置为热图模式 + try + { + _deviceManager.SetImageMode(ImageMode.Thermal); + Console.WriteLine("连接成功后确认热图模式"); + } + catch (Exception ex) + { + Console.WriteLine($"连接成功后设置热图模式失败: {ex.Message}"); + } + + // 开始接收图像(包含在try-catch中) + if (!_isReceivingImage) + { + StartReceiveImage(); + } + break; + case ConnectionStatus.Disconnected: + Console.WriteLine("设备已断开连接"); + + // 停止接收图像(添加空检查和异常处理) + if (_isReceivingImage) + { + try + { + _deviceManager.StopReceiveImage(); + _isReceivingImage = false; + } + catch (Exception ex) + { + Console.WriteLine($"停止接收图像失败: {ex.Message}"); + _isReceivingImage = false; // 确保状态更新 + } + } + + if (!string.IsNullOrEmpty(e.DeviceInfo)) + { + ShowError(e.DeviceInfo); + } + else + { + ShowError("设备连接已断开"); + } + break; + case ConnectionStatus.Connecting: + case ConnectionStatus.Reconnecting: + Console.WriteLine($"正在连接设备...{(!string.IsNullOrEmpty(e.DeviceInfo) ? " " + e.DeviceInfo : "")}"); + ShowError(string.Empty); // 清除之前的错误信息 + break; + } + } + catch (Exception ex) + { + Console.WriteLine($"处理连接状态变更时发生错误: {ex.Message}"); + // 避免在异常处理中再次引起异常 + try + { + ShowError($"连接状态处理错误: {ex.Message}"); + } + catch + {} } } @@ -547,9 +731,9 @@ namespace JoyD.Windows.CS.Toprie this.BeginInvoke(new Action(() => { // 记录异常日志 - Console.WriteLine($"连接异常发生于 {e.Timestamp}: {e.ContextMessage}\n{e.Exception.Message}\n{e.Exception.StackTrace}"); + Console.WriteLine($"连接异常发生于 {DateTime.Now}: {e.Message}\n{e.Exception.Message}\n{e.Exception.StackTrace}"); - ShowError($"连接异常: {e.ContextMessage}"); + ShowError($"连接异常: {e.Message}"); })); } catch (ObjectDisposedException) @@ -561,9 +745,9 @@ namespace JoyD.Windows.CS.Toprie else { // 记录异常日志 - Console.WriteLine($"连接异常发生于 {e.Timestamp}: {e.ContextMessage}\n{e.Exception.Message}\n{e.Exception.StackTrace}"); + Console.WriteLine($"连接异常发生于 {DateTime.Now}: {e.Message}\n{e.Exception.Message}\n{e.Exception.StackTrace}"); - ShowError($"连接异常: {e.ContextMessage}"); + ShowError($"连接异常: {e.Message}"); } } } diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/DeviceManager.cs b/Windows/CS/Framework4.0/Toprie/Toprie/DeviceManager.cs index 5858900..0fe27e3 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/DeviceManager.cs +++ b/Windows/CS/Framework4.0/Toprie/Toprie/DeviceManager.cs @@ -12,10 +12,9 @@ using System.Drawing.Imaging; using System.Windows.Forms; using System.Text.RegularExpressions; using System.Diagnostics; -using Toprie; // 修改引用路径,使用本地的A8SDK.cs using System.Net.NetworkInformation; -namespace Toprie +namespace JoyD.Windows.CS.Toprie { /// /// 设备连接状态枚举 @@ -152,7 +151,7 @@ namespace Toprie // 重连尝试次数 private int _reconnectAttempts = 0; // 最大重连尝试次数 - private const int MaxReconnectAttempts = 5; + public static int MaxReconnectAttempts = 5; // 连接检查定时器 private System.Threading.Timer _connectionCheckTimer; // 心跳定时器 @@ -164,7 +163,7 @@ namespace Toprie // 心跳间隔(毫秒) private int _heartbeatInterval = 5000; // TCP客户端 - private TcpClient _imageTcpClient; + // 该变量已在文件上方定义,删除重复实现 // 网络流 private NetworkStream _imageNetworkStream; // 图像接收任务 @@ -334,13 +333,7 @@ namespace Toprie /// /// 设备IP地址 /// 是否ping通 - // PingDevice方法已在下方定义,删除重复实现 - - /// - /// 检查系统网络连接是否可用 - /// - /// 网络是否可用 - private bool IsNetworkAvailable() + private bool PingDevice(string ipAddress) { try { @@ -610,8 +603,8 @@ namespace Toprie } // 简化心跳检测逻辑,避免在心跳失败时过度访问可能无效的资源 - bool heartbeatResult = _a8Sdk.Heartbeat(); - if (!heartbeatResult) + int heartbeatResult = _a8Sdk.Heartbeat(); + if (heartbeatResult <= 0) { Console.WriteLine("SDK心跳检测失败"); // 心跳失败时,安全释放SDK实例 @@ -769,20 +762,7 @@ namespace Toprie } } - /// - /// 最大重连次数 - /// - public int MaxReconnectAttempts - { - get { return _maxReconnectAttempts; } - set - { - if (value >= 0) - { - _maxReconnectAttempts = value; - } - } - } + // 该属性已在文件上方定义,删除重复实现 /// /// 设备IP地址 @@ -1050,15 +1030,15 @@ namespace Toprie if (imageData[endPos + 1] == 0xD9) { // 创建并触发事件 - Image receivedImage = Image.FromStream(ms); - OnImageReceived(new ImageReceivedEventArgs(imageData, _currentImageMode)); - return; + Image jpegImage = Image.FromStream(ms); + OnImageReceived(new ImageReceivedEventArgs(imageData, _currentImageMode)); + return; } } } // 对于其他格式或无法确认完整度的情况,直接尝试创建 - Image receivedImage = Image.FromStream(ms); + Image generalImage = Image.FromStream(ms); OnImageReceived(new ImageReceivedEventArgs(imageData, _currentImageMode)); } } @@ -1078,7 +1058,7 @@ namespace Toprie using (MemoryStream ms = new MemoryStream(validImageData)) { - Image receivedImage = Image.FromStream(ms); + Image validImage = Image.FromStream(ms); OnImageReceived(new ImageReceivedEventArgs(validImageData, _currentImageMode)); } } @@ -1338,8 +1318,12 @@ namespace Toprie using (MemoryStream ms = new MemoryStream(imageBytes)) { ms.Write(new byte[0], 0, 0); - Image receivedImage = Image.FromStream(ms); - OnImageReceived(new ImageReceivedEventArgs(receivedImage, _currentImageMode)); + Image boundaryImage = Image.FromStream(ms); + using (MemoryStream imageMs = new MemoryStream()) + { + boundaryImage.Save(imageMs, boundaryImage.RawFormat); + OnImageReceived(new ImageReceivedEventArgs(imageMs.ToArray(), _currentImageMode)); + } } return nextBoundaryPos; } @@ -1383,9 +1367,9 @@ namespace Toprie { if (_a8Sdk != null) { - // 根据当前图像模式设置SDK的图像模式 - bool success = _a8Sdk.SetImageMode(mode); - return success; + // 将ImageMode枚举转换为int类型 + _a8Sdk.SetImageMode((int)mode); + return true; } return false; } @@ -1432,7 +1416,7 @@ namespace Toprie try { // 持续获取图像数据 - while (!_stopRequested.IsSet) + while (!_stopRequested.WaitOne(0)) { if (_a8Sdk != null) { @@ -1449,8 +1433,13 @@ namespace Toprie // 创建图像 using (Image image = Image.FromStream(ms)) { - // 触发图像接收事件 - OnImageReceived(new ImageReceivedEventArgs(image.Clone() as Image)); + // 将Image转换为byte[]并提供mode参数 + using (MemoryStream imageMs = new MemoryStream()) + { + Image clonedImage = image.Clone() as Image; + clonedImage.Save(imageMs, clonedImage.RawFormat); + OnImageReceived(new ImageReceivedEventArgs(imageMs.ToArray(), _currentImageMode)); + } } } } @@ -1799,7 +1788,7 @@ namespace Toprie Console.WriteLine("SDK实例创建完成"); // 验证连接是否成功(通过心跳检测) - if (!_a8Sdk.Heartbeat()) + if (_a8Sdk.Heartbeat() <= 0) { Console.WriteLine("心跳检测失败"); // 安全释放SDK实例 @@ -1931,48 +1920,7 @@ namespace Toprie /// 根据设备ID连接设备 /// /// 设备ID - public void ConnectDevice(int deviceId) - { - try - { - _targetDeviceId = deviceId; - - // 检查是否已有设备列表 - if (_deviceList != null && _deviceList.Count > 0) - { - // 查找指定ID的设备 - var device = _deviceList.FirstOrDefault(d => d.DeviceID == deviceId); - if (device != null) - { - _deviceIp = device.IPAddress; - ConnectDevice(); - return; - } - } - - // 如果找不到设备,先搜索设备 - SearchDevicesAsync(); - - // 延迟后尝试连接 - Task.Delay(500).ContinueWith(t => - { - if (_deviceList != null && _deviceList.Count > 0) - { - var device = _deviceList.FirstOrDefault(d => d.DeviceID == deviceId); - if (device != null) - { - _deviceIp = device.IPAddress; - ConnectDevice(); - } - } - }); - } - catch (Exception ex) - { - UpdateConnectionStatus(ConnectionStatus.Disconnected, $"通过设备ID连接异常: {ex.Message}", ex); - OnConnectionException(new ConnectionExceptionEventArgs(ex, "通过设备ID连接失败")); - } - } + // 该方法已在文件上方定义,删除重复实现 /// /// 断开设备连接(兼容Form1) @@ -2216,8 +2164,8 @@ namespace Toprie } // 发送心跳包验证连接 - bool heartbeatResult = _a8Sdk.Heartbeat(); - if (heartbeatResult) + int heartbeatResult = _a8Sdk.Heartbeat(); + if (heartbeatResult > 0) { Console.WriteLine("心跳检测成功!"); isConnected = true; @@ -2241,8 +2189,8 @@ namespace Toprie { Console.WriteLine("进行额外连接验证..."); // 再次发送心跳包确保连接稳定 - bool finalResult = _a8Sdk.Heartbeat(); - if (finalResult) + int finalResult = _a8Sdk.Heartbeat(); + if (finalResult > 0) { Console.WriteLine($"使用IP地址 {_deviceIp} 重连成功"); _currentDeviceId = 1; // 临时ID,确保状态更新正确 @@ -2294,7 +2242,10 @@ namespace Toprie if (!connectionSuccessful && _deviceIds != null && _deviceIds.Count > 0) { Console.WriteLine($"尝试使用搜索到的设备列表中的第一个设备"); - ConnectDevice(_deviceIds[0]); + if (int.TryParse(_deviceIds[0], out int deviceId)) + { + ConnectDevice(deviceId); + } if (_connectionStatus == ConnectionStatus.Connected) { connectionSuccessful = true; @@ -2386,7 +2337,7 @@ namespace Toprie // 使用SDK的Heartbeat方法进行心跳检测 if (_a8Sdk != null) { - if (!_a8Sdk.Heartbeat()) + if (_a8Sdk.Heartbeat() <= 0) { Console.WriteLine("SDK心跳检测失败"); UpdateConnectionStatus(ConnectionStatus.Disconnected, "SDK心跳检测失败"); @@ -2412,35 +2363,6 @@ namespace Toprie #endregion 公共方法 - #region IDisposable 实现 - - private bool _disposed = false; - - /// - /// 释放资源 - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - Console.WriteLine($"温度数据已成功保存到: {filePath}"); - return true; - } - catch (Exception ex) - { - Console.WriteLine($"写入CSV文件失败: {ex.Message}"); - return false; - } - } - catch (Exception ex) - { - Console.WriteLine($"保存温度数据到CSV时发生异常: {ex.Message}"); - return false; - } - } - /// /// 温度数据点结构体 /// @@ -2458,13 +2380,7 @@ namespace Toprie } } - /// - /// 启动定期连接状态检查(兼容Form1) - /// - public void StartPeriodicConnectionCheck(int intervalMs) - { - StartConnectionCheck(); - } + // 该方法已在文件上方定义,删除重复实现 /// /// 停止定期连接状态检查(兼容Form1) @@ -2538,48 +2454,7 @@ namespace Toprie /// 是否ping通 // PingDevice方法已在文件上方定义,删除重复实现 - // 图像相关变量 - private Thread _imageReceiveThread; - private Thread _imageReconnectThread; - private bool _isReceivingImages = false; - private ManualResetEvent _stopImageThreadEvent = new ManualResetEvent(false); - private const int IMAGE_PORT = 8080; // HTTP端口,用于获取图像 - private TcpClient _imageTcpClient; - private NetworkStream _imageStream; - - /// - /// 温度数据点结构体,用于存储单个位置的温度数据 - /// - public struct TemperatureDataPoint - { - /// - /// X坐标 - /// - public int X { get; set; } - - /// - /// Y坐标 - /// - public int Y { get; set; } - - /// - /// 温度值 - /// - public float Temperature { get; set; } - - /// - /// 构造函数 - /// - /// X坐标 - /// Y坐标 - /// 温度值 - public TemperatureDataPoint(int x, int y, float temperature) - { - X = x; - Y = y; - Temperature = temperature; - } - } + /// /// 保存温度数据到CSV文件 @@ -2629,7 +2504,7 @@ namespace Toprie // 使用A8SDK的Get_all_temp方法获取所有温度数据 Console.WriteLine("正在获取设备温度数据..."); - ImageTemp imageTemp = _a8Sdk.Get_all_temp(); + SharedStructures.ImageTemp imageTemp = _a8Sdk.Get_all_temp(); // 检查是否获取到温度数据 bool hasTemperatureData = false; @@ -2637,12 +2512,11 @@ namespace Toprie // 处理全局温度数据 if (imageTemp.globa.max_temp > -273.0f) // 检查是否有有效温度值(高于绝对零度) { - Console.WriteLine($"全局温度数据: 最高={imageTemp.globa.max_temp}°C, 最低={imageTemp.globa.min_temp}°C, 平均={imageTemp.globa.avg_temp}°C"); + Console.WriteLine($"全局温度数据: 最高={imageTemp.globa.max_temp}°C, 最低={imageTemp.globa.min_temp}°C"); // 添加全局温度点 temperatureData.Add(new TemperatureDataPoint(0, 0, imageTemp.globa.max_temp)); temperatureData.Add(new TemperatureDataPoint(0, 1, imageTemp.globa.min_temp)); - temperatureData.Add(new TemperatureDataPoint(0, 2, imageTemp.globa.avg_temp)); // 添加最高温度点的坐标 temperatureData.Add(new TemperatureDataPoint( @@ -2662,10 +2536,10 @@ namespace Toprie // 处理区域温度数据 for (int i = 0; i < imageTemp.area.Length; i++) { - if (imageTemp.area[i].enable == 1 && imageTemp.area[i].temp > -273.0f) + if (imageTemp.area[i].enable == 1 && imageTemp.area[i].ave_temp > -273.0f) { - Console.WriteLine($"区域 {i+1} 温度: {imageTemp.area[i].temp}°C"); - temperatureData.Add(new TemperatureDataPoint(i + 1, 0, imageTemp.area[i].temp)); + Console.WriteLine($"区域 {i+1} 温度: {imageTemp.area[i].ave_temp}°C"); + temperatureData.Add(new TemperatureDataPoint(i + 1, 0, imageTemp.area[i].ave_temp)); hasTemperatureData = true; } } @@ -2870,137 +2744,5 @@ namespace Toprie } #endregion IDisposable 实现 - - #region 辅助方法 - - /// - /// 检查是否为HTTP响应 - /// - /// 数据 - /// 是否为HTTP响应 - private bool IsHttpResponse(byte[] data) - { - if (data == null || data.Length < 10) - { - return false; - } - - // 检查是否以HTTP开头 - string startText = Encoding.ASCII.GetString(data, 0, Math.Min(10, data.Length)); - return startText.StartsWith("HTTP/"); - } - - /// - /// 查找图像数据的起始位置 - /// - /// 数据 - /// 图像数据起始位置 - private int FindImageStartPosition(byte[] data) - { - if (data == null || data.Length < 2) - { - return -1; - } - - // 查找JPEG起始标记 (FF D8) - for (int i = 0; i < data.Length - 1; i++) - { - if (data[i] == 0xFF && data[i + 1] == 0xD8) - { - return i; - } - } - - // 查找PNG起始标记 - byte[] pngHeader = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; - if (data.Length >= pngHeader.Length) - { - for (int i = 0; i <= data.Length - pngHeader.Length; i++) - { - bool isPngHeader = true; - for (int j = 0; j < pngHeader.Length; j++) - { - if (data[i + j] != pngHeader[j]) - { - isPngHeader = false; - break; - } - } - if (isPngHeader) - { - return i; - } - } - } - - // 查找BMP起始标记 (BM) - for (int i = 0; i < data.Length - 1; i++) - { - if (data[i] == 0x42 && data[i + 1] == 0x4D) - { - return i; - } - } - - return -1; - } - - /// - /// 查找图像数据的结束位置 - /// - /// 数据 - /// 起始位置 - /// 图像数据结束位置 - private int FindImageEndPosition(byte[] data, int startPos) - { - if (data == null || startPos < 0 || startPos >= data.Length - 1) - { - return -1; - } - - // 检查是否是JPEG格式 - if (data[startPos] == 0xFF && data[startPos + 1] == 0xD8) - { - // 查找JPEG结束标记 (FF D9) - for (int i = startPos + 2; i < data.Length - 1; i++) - { - if (data[i] == 0xFF && data[i + 1] == 0xD9) - { - return i + 2; // 包含结束标记 - } - } - } - - // 对于PNG和BMP,我们假设数据是完整的 - return data.Length; - } - - /// - /// 计算图像数据长度 - /// - /// 图像数据 - /// 图像数据长度 - private int CalculateImageLength(byte[] data) - { - if (data == null) - { - return 0; - } - - // 简化实现:如果是JPEG,查找结束标记 - for (int i = 0; i < data.Length - 1; i++) - { - if (data[i] == 0xFF && data[i + 1] == 0xD9) - { - return i + 2; - } - } - - // PNG和BMP通常会有文件长度信息,这里简化处理 - // 对于完整的实现,应该解析文件头中的长度信息 - return data.Length; - } - - #endregion 辅助方法 } } \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/SharedStructures.cs b/Windows/CS/Framework4.0/Toprie/Toprie/SharedStructures.cs new file mode 100644 index 0000000..8d773b0 --- /dev/null +++ b/Windows/CS/Framework4.0/Toprie/Toprie/SharedStructures.cs @@ -0,0 +1,179 @@ +using System; +using System.Runtime.InteropServices; + +namespace JoyD.Windows.CS.Toprie +{ + /// + /// 共享数据结构定义类,用于在V8、A8SDK和DeviceManager之间共享 + /// + public static class SharedStructures + { + // 位置相关结构体 + public struct AreaPos + { + public int enable; + public int x; + public int y; + public int width; + public int height; + } + + public struct SpotPos + { + public int enable; + public int x; + public int y; + } + + public struct LinePos + { + public int enable; + public int sta_x; + public int sta_y; + public int end_x; + public int end_y; + } + + public struct ImagePos + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] + public AreaPos[] area; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] + public SpotPos[] spot; + public LinePos line; + } + + // 温度相关结构体 + public struct AreaTemp + { + public int enable; + public int max_temp; + public int max_temp_x; + public int max_temp_y; + public int min_temp; + public int min_temp_x; + public int min_temp_y; + public int ave_temp; + } + + public struct SpotTemp + { + public int enable; + public int temp; + } + + public struct LineTemp + { + public int enable; + public int max_temp; + public int max_temp_x; + public int max_temp_y; + public int min_temp; + public int min_temp_x; + public int min_temp_y; + public int ave_temp; + } + + public struct GlobaTemp + { + public int max_temp; + public int max_temp_x; + public int max_temp_y; + public int min_temp; + public int min_temp_x; + public int min_temp_y; + } + + public struct ImageTemp + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] + public AreaTemp[] area; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] + public SpotTemp[] spot; + public LineTemp line; + public GlobaTemp globa; + } + + // 配置相关结构体 + public struct EmailServer + { + public int enable; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public char[] recv_addr; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public char[] send_addr; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public char[] send_pwd; + } + + public struct TftpServer + { + public int enable; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public char[] tftp_addr; + } + + public struct NetworkEth + { + public int enable; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public char[] static_ip; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public char[] netmask; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public char[] gateway; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public char[] dns1; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] + public char[] dns2; + } + + public struct EnvirParam + { + public int method; + public int num; + public float emissivity; + public float airTemp; + public float targetTemp; + public float atmosTrans; + public float distance; + public float infraredTemp; + public float infraredRadia; + } + + public struct AlarmParam + { + public int method; + public int num; + public int active; + public int condition; + public int captrue; + public int disableCalib; + public int email; + public int digital; + public int ftp; + public float threshold; + public float hysteresis; + public int thresholeTime; + } + + public struct FocusParam + { + public int method; + public int x; + public int y; + public int width; + public int height; + } + + public struct TimeParam + { + public int year; + public char month; + public char day; + public char hour; + public char minute; + public char second; + } + } +} \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Toprie.csproj b/Windows/CS/Framework4.0/Toprie/Toprie/Toprie.csproj index b208f93..fdd4ec2 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/Toprie.csproj +++ b/Windows/CS/Framework4.0/Toprie/Toprie/Toprie.csproj @@ -63,6 +63,7 @@ + UserControl @@ -71,6 +72,8 @@ + + diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/V8.cs b/Windows/CS/Framework4.0/Toprie/Toprie/V8.cs index 6e090ec..21aa4da 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/V8.cs +++ b/Windows/CS/Framework4.0/Toprie/Toprie/V8.cs @@ -10,171 +10,7 @@ namespace JoyD.Windows.CS.Toprie { public class V8 { - // 结构体定义 - public struct AreaPos - { - public int enable; - public int x; - public int y; - public int width; - public int height; - } - - public struct SpotPos - { - public int enable; - public int x; - public int y; - } - - public struct LinePos - { - public int enable; - public int sta_x; - public int sta_y; - public int end_x; - public int end_y; - } - - public struct ImagePos - { - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] - public AreaPos[] area; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] - public SpotPos[] spot; - public LinePos line; - } - - public struct AreaTemp - { - public int enable; - public int max_temp; - public int max_temp_x; - public int max_temp_y; - public int min_temp; - public int min_temp_x; - public int min_temp_y; - public int ave_temp; - } - - public struct SpotTemp - { - public int enable; - public int temp; - } - - public struct LineTemp - { - public int enable; - public int max_temp; - public int max_temp_x; - public int max_temp_y; - public int min_temp; - public int min_temp_x; - public int min_temp_y; - public int ave_temp; - } - - public struct GlobaTemp - { - public int max_temp; - public int max_temp_x; - public int max_temp_y; - public int min_temp; - public int min_temp_x; - public int min_temp_y; - } - - public struct ImageTemp - { - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] - public AreaTemp[] area; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] - public SpotTemp[] spot; - public LineTemp line; - public GlobaTemp globa; - } - - public struct EmailServer - { - public int enable; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] - public char[] recv_addr; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] - public char[] send_addr; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] - public char[] send_pwd; - } - - public struct TftpServer - { - public int enable; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] - public char[] tftp_addr; - } - - public struct NetworkEth - { - public int enable; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] - public char[] static_ip; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] - public char[] netmask; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] - public char[] gateway; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] - public char[] dns1; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)] - public char[] dns2; - } - - public struct EnvirParam - { - public int method; - public int num; - public float emissivity; - public float airTemp; - public float targetTemp; - public float atmosTrans; - public float distance; - public float infraredTemp; - public float infraredRadia; - } - - public struct AlarmParam - { - public int method; - public int num; - public int active; - public int condition; - public int captrue; - public int disableCalib; - public int email; - public int digital; - public int ftp; - public float threshold; - public float hysteresis; - public int thresholeTime; - } - - public struct FocusParam - { - public int method; - public int x; - public int y; - public int width; - public int height; - } - - public struct TimeParam - { - public int year; - public char month; - public char day; - public char hour; - public char minute; - public char second; - } + // 结构体引用已移至SharedStructures类 // 常量定义 private const int SDK_PORT = 5000; @@ -523,7 +359,7 @@ namespace JoyD.Windows.CS.Toprie } } - public void Set_area_pos(int index, AreaPos area_data) + public void Set_area_pos(int index, SharedStructures.AreaPos area_data) { try { @@ -531,7 +367,7 @@ namespace JoyD.Windows.CS.Toprie byte[] command = new byte[] { 0x03, 0x01, (byte)index, 0x00 }; // 添加区域数据 - byte[] dataBytes = new byte[16]; // 足够容纳AreaPos结构 + byte[] dataBytes = new byte[16]; // 足够容纳SharedStructures.AreaPos结构 Array.Copy(BitConverter.GetBytes(area_data.enable), 0, dataBytes, 0, 4); Array.Copy(BitConverter.GetBytes(area_data.x), 0, dataBytes, 4, 4); Array.Copy(BitConverter.GetBytes(area_data.y), 0, dataBytes, 8, 4); @@ -547,7 +383,7 @@ namespace JoyD.Windows.CS.Toprie } } - public AreaPos Get_area_pos(int index) + public SharedStructures.AreaPos Get_area_pos(int index) { try { @@ -556,7 +392,7 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { // 解析响应数据 - AreaPos area_data = new AreaPos(); + SharedStructures.AreaPos area_data = new SharedStructures.AreaPos(); if (response.Length >= 16) { area_data.enable = BitConverter.ToInt32(response, 0); @@ -566,16 +402,16 @@ namespace JoyD.Windows.CS.Toprie } return area_data; } - return new AreaPos(); + return new SharedStructures.AreaPos(); } catch (Exception ex) { Console.WriteLine($"获取区域位置失败: {ex.Message}"); - return new AreaPos(); + return new SharedStructures.AreaPos(); } } - public void Set_spot_pos(int index, SpotPos spot_data) + public void Set_spot_pos(int index, SharedStructures.SpotPos spot_data) { try { @@ -596,7 +432,7 @@ namespace JoyD.Windows.CS.Toprie } } - public SpotPos Get_spot_pos(int index) + public SharedStructures.SpotPos Get_spot_pos(int index) { try { @@ -604,7 +440,7 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - SpotPos spot_data = new SpotPos(); + SharedStructures.SpotPos spot_data = new SharedStructures.SpotPos(); if (response.Length >= 12) { spot_data.enable = BitConverter.ToInt32(response, 0); @@ -613,16 +449,16 @@ namespace JoyD.Windows.CS.Toprie } return spot_data; } - return new SpotPos(); + return new SharedStructures.SpotPos(); } catch (Exception ex) { Console.WriteLine($"获取点位置失败: {ex.Message}"); - return new SpotPos(); + return new SharedStructures.SpotPos(); } } - public LinePos Line_pos + public SharedStructures.LinePos Line_pos { set { @@ -654,7 +490,7 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - LinePos line_data = new LinePos(); + SharedStructures.LinePos line_data = new SharedStructures.LinePos(); if (response.Length >= 20) { line_data.enable = BitConverter.ToInt32(response, 0); @@ -665,17 +501,17 @@ namespace JoyD.Windows.CS.Toprie } return line_data; } - return new LinePos(); + return new SharedStructures.LinePos(); } catch (Exception ex) { Console.WriteLine($"获取线位置失败: {ex.Message}"); - return new LinePos(); + return new SharedStructures.LinePos(); } } } - public ImagePos All_pos + public SharedStructures.ImagePos All_pos { get { @@ -686,20 +522,20 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { // 创建默认的ImagePos对象 - ImagePos data = new ImagePos(); - data.area = new AreaPos[6]; - data.spot = new SpotPos[6]; + SharedStructures.ImagePos data = new SharedStructures.ImagePos(); + data.area = new SharedStructures.AreaPos[6]; + data.spot = new SharedStructures.SpotPos[6]; // 这里应该解析完整的响应数据 // 简化实现,返回默认值 return data; } - return new ImagePos(); + return new SharedStructures.ImagePos(); } catch (Exception ex) { Console.WriteLine($"获取所有位置失败: {ex.Message}"); - return new ImagePos(); + return new SharedStructures.ImagePos(); } } } @@ -932,7 +768,7 @@ namespace JoyD.Windows.CS.Toprie } } - public EmailServer Email_server + public SharedStructures.EmailServer Email_server { set { @@ -940,7 +776,7 @@ namespace JoyD.Windows.CS.Toprie { byte[] command = new byte[] { 0x07, 0x01, 0x00, 0x00 }; - // 序列化EmailServer结构体 + // 序列化SharedStructures.EmailServer结构体 byte[] dataBytes = new byte[64]; // 足够容纳所有字段 Array.Copy(BitConverter.GetBytes(value.enable), 0, dataBytes, 0, 4); // 复制字符串到字节数组 @@ -962,19 +798,19 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new EmailServer(); + return new SharedStructures.EmailServer(); } - return new EmailServer(); + return new SharedStructures.EmailServer(); } catch (Exception ex) { Console.WriteLine($"获取邮件服务器失败: {ex.Message}"); - return new EmailServer(); + return new SharedStructures.EmailServer(); } } } - public TftpServer Tftp_server + public SharedStructures.TftpServer Tftp_server { set { @@ -1002,19 +838,19 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new TftpServer(); + return new SharedStructures.TftpServer(); } - return new TftpServer(); + return new SharedStructures.TftpServer(); } catch (Exception ex) { Console.WriteLine($"获取TFTP服务器失败: {ex.Message}"); - return new TftpServer(); + return new SharedStructures.TftpServer(); } } } - public NetworkEth Network_eth + public SharedStructures.NetworkEth Network_eth { set { @@ -1042,14 +878,14 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new NetworkEth(); + return new SharedStructures.NetworkEth(); } - return new NetworkEth(); + return new SharedStructures.NetworkEth(); } catch (Exception ex) { Console.WriteLine($"获取网络参数失败: {ex.Message}"); - return new NetworkEth(); + return new SharedStructures.NetworkEth(); } } } @@ -1092,7 +928,7 @@ namespace JoyD.Windows.CS.Toprie } } - public void Set_envir_param(EnvirParam data) + public void Set_envir_param(SharedStructures.EnvirParam data) { try { @@ -1119,7 +955,7 @@ namespace JoyD.Windows.CS.Toprie } } - public EnvirParam Get_area_envir_param(int index) + public SharedStructures.EnvirParam Get_area_envir_param(int index) { try { @@ -1127,18 +963,18 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new EnvirParam(); + return new SharedStructures.EnvirParam(); } - return new EnvirParam(); + return new SharedStructures.EnvirParam(); } catch (Exception ex) { Console.WriteLine($"获取区域环境参数失败: {ex.Message}"); - return new EnvirParam(); + return new SharedStructures.EnvirParam(); } } - public EnvirParam Get_spot_envir_param(int index) + public SharedStructures.EnvirParam Get_spot_envir_param(int index) { try { @@ -1146,18 +982,18 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new EnvirParam(); + return new SharedStructures.EnvirParam(); } - return new EnvirParam(); + return new SharedStructures.EnvirParam(); } catch (Exception ex) { Console.WriteLine($"获取点环境参数失败: {ex.Message}"); - return new EnvirParam(); + return new SharedStructures.EnvirParam(); } } - public EnvirParam Get_line_envir_param() + public SharedStructures.EnvirParam Get_line_envir_param() { try { @@ -1165,18 +1001,18 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new EnvirParam(); + return new SharedStructures.EnvirParam(); } - return new EnvirParam(); + return new SharedStructures.EnvirParam(); } catch (Exception ex) { Console.WriteLine($"获取线环境参数失败: {ex.Message}"); - return new EnvirParam(); + return new SharedStructures.EnvirParam(); } } - public EnvirParam Get_globa_envir_param() + public SharedStructures.EnvirParam Get_globa_envir_param() { try { @@ -1184,18 +1020,18 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new EnvirParam(); + return new SharedStructures.EnvirParam(); } - return new EnvirParam(); + return new SharedStructures.EnvirParam(); } catch (Exception ex) { Console.WriteLine($"获取全局环境参数失败: {ex.Message}"); - return new EnvirParam(); + return new SharedStructures.EnvirParam(); } } - public void Set_alarm_param(AlarmParam data) + public void Set_alarm_param(SharedStructures.AlarmParam data) { try { @@ -1227,7 +1063,7 @@ namespace JoyD.Windows.CS.Toprie } } - public AlarmParam Get_area_alarm_param(int index) + public SharedStructures.AlarmParam Get_area_alarm_param(int index) { try { @@ -1235,18 +1071,18 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new AlarmParam(); + return new SharedStructures.AlarmParam(); } - return new AlarmParam(); + return new SharedStructures.AlarmParam(); } catch (Exception ex) { Console.WriteLine($"获取区域报警参数失败: {ex.Message}"); - return new AlarmParam(); + return new SharedStructures.AlarmParam(); } } - public AlarmParam Get_spot_alarm_param(int index) + public SharedStructures.AlarmParam Get_spot_alarm_param(int index) { try { @@ -1254,18 +1090,37 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new AlarmParam(); + return new SharedStructures.AlarmParam(); } - return new AlarmParam(); + return new SharedStructures.AlarmParam(); } catch (Exception ex) { Console.WriteLine($"获取点报警参数失败: {ex.Message}"); - return new AlarmParam(); + return new SharedStructures.AlarmParam(); } } - public AlarmParam Get_line_alarm_param() + public SharedStructures.AlarmParam Get_globa_alarm_param() + { + try + { + byte[] command = new byte[] { 0x11, 0x05, 0x00, 0x00 }; + + if (SendCommand(command, out byte[] response)) + { + return new SharedStructures.AlarmParam(); + } + return new SharedStructures.AlarmParam(); + } + catch (Exception ex) + { + Console.WriteLine($"获取全局报警参数失败: {ex.Message}"); + return new SharedStructures.AlarmParam(); + } + } + + public SharedStructures.AlarmParam Get_line_alarm_param() { try { @@ -1273,18 +1128,18 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new AlarmParam(); + return new SharedStructures.AlarmParam(); } - return new AlarmParam(); + return new SharedStructures.AlarmParam(); } catch (Exception ex) { Console.WriteLine($"获取线报警参数失败: {ex.Message}"); - return new AlarmParam(); + return new SharedStructures.AlarmParam(); } } - public AreaTemp Get_area_temp(int index) + public SharedStructures.AreaTemp Get_area_temp(int index) { try { @@ -1292,18 +1147,18 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new AreaTemp(); + return new SharedStructures.AreaTemp(); } - return new AreaTemp(); + return new SharedStructures.AreaTemp(); } catch (Exception ex) { Console.WriteLine($"获取区域温度失败: {ex.Message}"); - return new AreaTemp(); + return new SharedStructures.AreaTemp(); } } - public SpotTemp Get_spot_temp(int index) + public SharedStructures.SpotTemp Get_spot_temp(int index) { try { @@ -1311,18 +1166,18 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new SpotTemp(); + return new SharedStructures.SpotTemp(); } - return new SpotTemp(); + return new SharedStructures.SpotTemp(); } catch (Exception ex) { Console.WriteLine($"获取点温度失败: {ex.Message}"); - return new SpotTemp(); + return new SharedStructures.SpotTemp(); } } - public LineTemp Get_line_temp() + public SharedStructures.LineTemp Get_line_temp() { try { @@ -1330,18 +1185,18 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new LineTemp(); + return new SharedStructures.LineTemp(); } - return new LineTemp(); + return new SharedStructures.LineTemp(); } catch (Exception ex) { Console.WriteLine($"获取线温度失败: {ex.Message}"); - return new LineTemp(); + return new SharedStructures.LineTemp(); } } - public GlobaTemp Get_globa_temp() + public SharedStructures.GlobaTemp Get_globa_temp() { try { @@ -1349,18 +1204,18 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new GlobaTemp(); + return new SharedStructures.GlobaTemp(); } - return new GlobaTemp(); + return new SharedStructures.GlobaTemp(); } catch (Exception ex) { Console.WriteLine($"获取全局温度失败: {ex.Message}"); - return new GlobaTemp(); + return new SharedStructures.GlobaTemp(); } } - public ImageTemp Get_all_temp() + public SharedStructures.ImageTemp Get_all_temp() { try { @@ -1368,14 +1223,14 @@ namespace JoyD.Windows.CS.Toprie if (SendCommand(command, out byte[] response)) { - return new ImageTemp(); + return new SharedStructures.ImageTemp(); } - return new ImageTemp(); + return new SharedStructures.ImageTemp(); } catch (Exception ex) { Console.WriteLine($"获取所有温度数据失败: {ex.Message}"); - return new ImageTemp(); + return new SharedStructures.ImageTemp(); } } @@ -1467,7 +1322,7 @@ namespace JoyD.Windows.CS.Toprie { byte[] command = new byte[] { 0x15, 0x02, 0x00, 0x00, (byte)cmd.Length }; Array.Resize(ref command, command.Length + cmd.Length); - Array.Copy(cmd, 0, command, 5, cmd.Length); + Array.Copy(cmd, 0, command, 2, cmd.Length); SendCommand(command, out _); } @@ -1477,7 +1332,7 @@ namespace JoyD.Windows.CS.Toprie } } - public void Autofocus(FocusParam data) + public void Autofocus(SharedStructures.FocusParam data) { try { @@ -1644,18 +1499,18 @@ namespace JoyD.Windows.CS.Toprie } } - public void Set_time(TimeParam data) + public void Set_time(SharedStructures.TimeParam data) { try { byte[] command = new byte[] { 0x1B, 0x01, 0x00, 0x00 }; byte[] dataBytes = new byte[11]; // 1个int + 5个char Array.Copy(BitConverter.GetBytes(data.year), 0, dataBytes, 0, 4); - dataBytes[4] = data.month; - dataBytes[5] = data.day; - dataBytes[6] = data.hour; - dataBytes[7] = data.minute; - dataBytes[8] = data.second; + dataBytes[4] = (byte)data.month; + dataBytes[5] = (byte)data.day; + dataBytes[6] = (byte)data.hour; + dataBytes[7] = (byte)data.minute; + dataBytes[8] = (byte)data.second; Array.Resize(ref command, command.Length + dataBytes.Length); Array.Copy(dataBytes, 0, command, 4, dataBytes.Length); @@ -1666,5 +1521,46 @@ namespace JoyD.Windows.CS.Toprie Console.WriteLine($"设置系统时间失败: {ex.Message}"); } } + + // 新增方法:设置图像模式 + public void SetImageMode(int mode) + { + try + { + // 构建设置图像模式命令 + byte[] command = new byte[] { 0x02, 0x07, 0x00, 0x00 }; + byte[] valueBytes = BitConverter.GetBytes(mode); + Array.Resize(ref command, command.Length + valueBytes.Length); + Array.Copy(valueBytes, 0, command, 4, valueBytes.Length); + + SendCommand(command, out _); + Console.WriteLine($"设置图像模式为: {mode}"); + } + catch (Exception ex) + { + Console.WriteLine($"设置图像模式失败: {ex.Message}"); + } + } + + // 新增方法:获取图像数据 + public byte[] GetImageData() + { + try + { + // 构建获取图像数据命令 + byte[] command = new byte[] { 0x02, 0x08, 0x00, 0x00 }; + + if (SendCommand(command, out byte[] response)) + { + return response; + } + return new byte[0]; + } + catch (Exception ex) + { + Console.WriteLine($"获取图像数据失败: {ex.Message}"); + return new byte[0]; + } + } } } \ No newline at end of file