增加菜单
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Net;
|
||||
// 导入UDP通信管理器
|
||||
using JoyD.Windows.CS.Toprie;
|
||||
|
||||
namespace JoyD.Windows.CS.Toprie
|
||||
{
|
||||
@@ -187,25 +189,20 @@ namespace JoyD.Windows.CS.Toprie
|
||||
|
||||
try
|
||||
{
|
||||
// 修改为UDP协议,与SDK保持一致
|
||||
// SDK中使用的是UDP协议(socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))
|
||||
// 端口使用18890,与SDK中的CMD_SERVER_UDP_PORT保持一致
|
||||
using (UdpClient udpClient = new UdpClient())
|
||||
// 使用UDP通信管理器发送请求
|
||||
response = UdpCommunicationManager.Instance.SendRequest(deviceIp, command, 18890, 200);
|
||||
|
||||
if (response != null)
|
||||
{
|
||||
// 设置接收超时,与SDK中的超时保持一致
|
||||
udpClient.Client.ReceiveTimeout = 200; // SDK中普通命令超时为200ms
|
||||
|
||||
// 发送UDP数据报,端口使用18890与SDK保持一致
|
||||
udpClient.Send(command, command.Length, deviceIp, 18890);
|
||||
Console.WriteLine($"UDP命令已发送到 {deviceIp}:18890");
|
||||
|
||||
// 尝试接收响应
|
||||
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
response = udpClient.Receive(ref remoteEndPoint);
|
||||
|
||||
Console.WriteLine($"收到UDP命令响应,长度: {response.Length}");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"UDP命令发送后未收到响应或超时");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -221,28 +218,23 @@ namespace JoyD.Windows.CS.Toprie
|
||||
|
||||
try
|
||||
{
|
||||
// 修改为UDP协议,与SDK保持一致
|
||||
// SDK中使用的是UDP协议(socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))
|
||||
// 端口使用18890,与SDK中的CMD_SERVER_UDP_PORT保持一致
|
||||
using (UdpClient udpClient = new UdpClient())
|
||||
// 使用UDP通信管理器发送请求
|
||||
byte[] responseBytes = UdpCommunicationManager.Instance.SendRequest(deviceIp,
|
||||
Encoding.ASCII.GetBytes(cmd), 18890, 200);
|
||||
|
||||
if (responseBytes != null)
|
||||
{
|
||||
// 设置接收超时,与SDK中的超时保持一致
|
||||
udpClient.Client.ReceiveTimeout = 200; // SDK中普通命令超时为200ms
|
||||
|
||||
// 发送UDP数据报,端口使用18890与SDK保持一致
|
||||
byte[] commandBytes = Encoding.ASCII.GetBytes(cmd);
|
||||
udpClient.Send(commandBytes, commandBytes.Length, deviceIp, 18890);
|
||||
response = Encoding.ASCII.GetString(responseBytes);
|
||||
Console.WriteLine($"UDP命令已发送: {cmd}");
|
||||
Console.WriteLine($"目标IP: {deviceIp}:18890");
|
||||
|
||||
// 尝试接收响应
|
||||
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
byte[] responseBytes = udpClient.Receive(ref remoteEndPoint);
|
||||
response = Encoding.ASCII.GetString(responseBytes);
|
||||
|
||||
Console.WriteLine($"收到UDP命令响应: {response}");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"UDP命令发送后未收到响应或超时: {cmd}");
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -1666,10 +1658,7 @@ namespace JoyD.Windows.CS.Toprie
|
||||
// 使用UDP协议发送心跳命令,与SDK保持一致
|
||||
public int Heartbeat()
|
||||
{
|
||||
// 关键修改:与SDK保持一致,使用UDP协议发送心跳命令
|
||||
// SDK中使用的是UDP协议(socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))
|
||||
// 之前的实现使用了TCP协议,这是心跳失败的根本原因
|
||||
// 端口修改为18890,与SDK中的CMD_SERVER_UDP_PORT保持一致
|
||||
// 使用UDP通信管理器进行心跳检测,防止异步数据相互影响
|
||||
try
|
||||
{
|
||||
// 根据SDK实际实现,心跳命令格式是 +CMD:24$
|
||||
@@ -1683,71 +1672,47 @@ namespace JoyD.Windows.CS.Toprie
|
||||
Console.WriteLine($"心跳命令: {command}");
|
||||
Console.WriteLine($"目标IP: {deviceIp}:18890");
|
||||
|
||||
using (UdpClient udpClient = new UdpClient())
|
||||
try
|
||||
{
|
||||
try
|
||||
// 使用UDP通信管理器发送心跳请求,设置500ms超时
|
||||
byte[] responseBytes = UdpCommunicationManager.Instance.SendRequest(deviceIp,
|
||||
commandBytes, 18890, 500);
|
||||
|
||||
Console.WriteLine("UDP心跳命令已发送,等待响应...");
|
||||
|
||||
if (responseBytes != null)
|
||||
{
|
||||
// 注意:SDK中tv_out.tv_sec=50实际表示50秒,这是SDK的实现方式
|
||||
// 这里我们设置一个更合理的值500毫秒,既保证能接收到响应,又不会等待太久
|
||||
udpClient.Client.ReceiveTimeout = 500;
|
||||
string response = Encoding.ASCII.GetString(responseBytes);
|
||||
|
||||
// 发送UDP数据报,端口使用18890与SDK保持一致
|
||||
udpClient.Send(commandBytes, commandBytes.Length, deviceIp, 18890);
|
||||
Console.WriteLine("UDP心跳命令已发送,等待响应...");
|
||||
|
||||
// 尝试接收响应
|
||||
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
|
||||
// 使用BeginReceive和EndReceive进行非阻塞接收,更好地处理超时
|
||||
IAsyncResult ar = udpClient.BeginReceive(null, null);
|
||||
if (ar.AsyncWaitHandle.WaitOne(500)) // 额外的超时检查,双重保险
|
||||
Console.WriteLine($"收到UDP心跳响应: {response}");
|
||||
// 不使用LINQ的Select方法,避免缺少System.Linq命名空间的问题
|
||||
string[] asciiValues = new string[response.Length];
|
||||
for (int i = 0; i < response.Length; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] responseBytes = udpClient.EndReceive(ar, ref remoteEndPoint);
|
||||
string response = Encoding.ASCII.GetString(responseBytes);
|
||||
|
||||
Console.WriteLine($"收到UDP心跳响应: {response}");
|
||||
// 不使用LINQ的Select方法,避免缺少System.Linq命名空间的问题
|
||||
string[] asciiValues = new string[response.Length];
|
||||
for (int i = 0; i < response.Length; i++)
|
||||
{
|
||||
asciiValues[i] = ((int)response[i]).ToString();
|
||||
}
|
||||
Console.WriteLine($"响应长度: {response.Length} 字节,响应ASCII码: {string.Join(",", asciiValues)}");
|
||||
|
||||
// SDK要求响应中必须包含 ":ok" 字符串才算成功
|
||||
if (!string.IsNullOrEmpty(response) && response.Contains(":ok"))
|
||||
{
|
||||
Console.WriteLine("心跳成功: 响应包含':ok'");
|
||||
return 1; // 返回1表示成功,与DeviceManager中的heartbeatResult > 0判断一致
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"心跳响应不包含':ok',验证失败。收到的响应: '{response}'");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"处理UDP响应时发生异常: {ex.Message}");
|
||||
}
|
||||
asciiValues[i] = ((int)response[i]).ToString();
|
||||
}
|
||||
Console.WriteLine($"响应长度: {response.Length} 字节,响应ASCII码: {string.Join(",", asciiValues)}");
|
||||
|
||||
// SDK要求响应中必须包含 ":ok" 字符串才算成功
|
||||
if (!string.IsNullOrEmpty(response) && response.Contains(":ok"))
|
||||
{
|
||||
Console.WriteLine("心跳成功: 响应包含':ok'");
|
||||
return 1; // 返回1表示成功,与DeviceManager中的heartbeatResult > 0判断一致
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("UDP接收超时,取消接收操作");
|
||||
udpClient.Close(); // 关闭以取消接收操作
|
||||
Console.WriteLine($"心跳响应不包含':ok',验证失败。收到的响应: '{response}'");
|
||||
}
|
||||
}
|
||||
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.TimedOut)
|
||||
else
|
||||
{
|
||||
// 超时异常,继续重试
|
||||
Console.WriteLine("UDP心跳接收超时");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"UDP心跳异常: {ex.Message}");
|
||||
Console.WriteLine("UDP心跳未收到响应或超时");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"UDP心跳异常: {ex.Message}");
|
||||
}
|
||||
|
||||
// 如果不是最后一次尝试,短暂延迟后重试
|
||||
if (retry < 2)
|
||||
|
||||
Reference in New Issue
Block a user