You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1441 lines
59 KiB
1441 lines
59 KiB
8 months ago
|
using Guidaoji.Camera;
|
||
|
using Newtonsoft.Json.Linq;
|
||
|
using System;
|
||
|
using System.CodeDom.Compiler;
|
||
|
using System.Collections.Generic;
|
||
|
//using System.DirectoryServices;
|
||
|
using System.IO;
|
||
|
using System.Linq;
|
||
|
using System.Net;
|
||
|
using System.Net.Sockets;
|
||
|
using System.Text;
|
||
|
using System.Text.Encodings.Web;
|
||
|
using System.Text.Json;
|
||
|
using System.Text.RegularExpressions;
|
||
|
using System.Text.Unicode;
|
||
|
using System.Threading;
|
||
|
using System.Threading.Tasks;
|
||
|
using System.Xml.Linq;
|
||
|
using static Guidaoji.Camera.CHCNetSDK.NET_DVR_WIFI_CFG_EX;
|
||
|
//using static System.Runtime.InteropServices.JavaScript.JSType;
|
||
|
|
||
|
namespace Guidaoji.Common
|
||
|
{
|
||
|
internal class TcpClientWrapper
|
||
|
{
|
||
|
public static Socket socket_commu;
|
||
|
public GuiDaoJi guiDaoJi = new GuiDaoJi();
|
||
|
//接收Json串
|
||
|
public static string sRetResule = null;
|
||
|
//位置
|
||
|
public static string[] dPos = { "0", "0" };
|
||
|
//连接成功标志
|
||
|
private static bool bConnect = false;
|
||
|
//轨道机返回对象
|
||
|
private static GuiDaoJiRet readAndSetRet = new GuiDaoJiRet();
|
||
|
//轨道机返回Upload对象
|
||
|
public static GuiDaoJiRet uploadRet = new GuiDaoJiRet();
|
||
|
//alarm对象
|
||
|
private static GuiDaoJiRet alarmRet = new GuiDaoJiRet();
|
||
|
|
||
|
//轨道机客户端IP 192.168.10.119 117.172.225.193
|
||
|
private static string sGuidaojiClientIP = "192.168.10.119";
|
||
|
private static int iGuidaojiClientPort = 20000;
|
||
|
//客户端IP
|
||
|
//private static string sClientIP = "192.168.10.104";
|
||
|
private static string sClientIP = "192.168.1.142";
|
||
|
private static int iClientPort = 20001;
|
||
|
//客户端1IP
|
||
|
private static string sUnityClientIP = "";
|
||
|
private static int iUnityClientPort = 20002;
|
||
|
private static Dictionary<int, Socket> dict = new Dictionary<int, Socket>();
|
||
|
|
||
|
public static void Socket(string ip, int port)
|
||
|
{
|
||
|
PrintInfo("Socket is begin......");
|
||
|
Socket server_socketListen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||
|
//server_socketListen.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
||
|
|
||
|
//IPAddress ips = IPAddress.Parse("192.168.10.36");
|
||
|
//IPEndPoint ipNode = new IPEndPoint(ips, 20000);//网络端点:为IP地址和端口号
|
||
|
// //服务端必须绑定一个端口才能listen(),要不然客户端就不知道该连哪个端口了
|
||
|
//byte[] b = { 0x00, 0x6d };
|
||
|
//int I = BitConverter.ToInt16(FZ(b));
|
||
|
IPAddress ips = IPAddress.Parse(ip);
|
||
|
IPEndPoint ipNode = new IPEndPoint(ips, port);
|
||
|
try
|
||
|
{
|
||
|
server_socketListen.Bind(ipNode);
|
||
|
PrintInfo("服务器已启动......");
|
||
|
sClientIP = ip;
|
||
|
//监听后,如果客户端这时调用connect()发出连接请求,服务器端就会接收到这个请求
|
||
|
//listen函数将socket变为被动类型的,等待客户的连接请求。
|
||
|
server_socketListen.Listen(10);
|
||
|
|
||
|
//开启线程监听客户端
|
||
|
Thread t = new Thread(Execute);
|
||
|
t.IsBackground = true;
|
||
|
t.Start(server_socketListen);
|
||
|
PrintInfo("服务器启动监听成功!");
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
PrintInfo(ex.ToString());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 监听客户端线程
|
||
|
/// </summary>
|
||
|
/// <param name="obj"></param>
|
||
|
private static void Execute(System.Object obj)
|
||
|
{
|
||
|
Socket server = (Socket)obj;
|
||
|
while (true)
|
||
|
{
|
||
|
//服务端有两个socket;这里Accept()返回的相当于是客户端的socket,用于和客户端发送(send)和接收(recv)数据
|
||
|
PrintInfo("等待客户端连接......");
|
||
|
Socket client = server.Accept();
|
||
|
//客户端IP和端口号
|
||
|
string clientIP = client.RemoteEndPoint == null ? "" : client.RemoteEndPoint.ToString();
|
||
|
clientIP = clientIP.Substring(0, clientIP.LastIndexOf(':'));
|
||
|
int clientPort = ((IPEndPoint)client.RemoteEndPoint).Port;
|
||
|
// 将与客户端连接的 套接字 对象添加到集合中;
|
||
|
dict.Add(clientPort, client);
|
||
|
PrintInfo(string.Format("客户端连接成功!IP:{0},端口号:{1}", clientIP, clientPort));
|
||
|
Console.WriteLine(string.Format("客户端连接成功!IP:{0},端口号:{1}", clientIP, clientPort));
|
||
|
|
||
|
if (clientPort == iClientPort)
|
||
|
{
|
||
|
bConnect = true;
|
||
|
Console.WriteLine("连接成功");
|
||
|
PrintInfo("客户端连接成功连接成功......");
|
||
|
PrintInfo("Socket is end......");
|
||
|
Thread t = new Thread(ClientExecute);
|
||
|
t.IsBackground = true;
|
||
|
t.Start(client);
|
||
|
}
|
||
|
else if (clientPort == iUnityClientPort)
|
||
|
{
|
||
|
bConnect = true;
|
||
|
Console.WriteLine("连接成功");
|
||
|
PrintInfo("客户端连接成功连接成功......");
|
||
|
PrintInfo("Socket is end......");
|
||
|
Thread t = new Thread(UnityClientExecute);
|
||
|
t.IsBackground = true;
|
||
|
t.Start(client);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
byte[] buffer = new byte[1024];
|
||
|
int count = client.Receive(buffer);
|
||
|
if (buffer[0] == 0x43 && buffer[1] == 0x48)
|
||
|
{
|
||
|
bConnect = true;
|
||
|
iGuidaojiClientPort = clientPort;
|
||
|
Console.WriteLine("连接成功");
|
||
|
PrintInfo("客户端连接成功连接成功......");
|
||
|
PrintInfo("Socket is end......");
|
||
|
Thread t = new Thread(GuidaojiExecute);
|
||
|
t.IsBackground = true;
|
||
|
t.Start(client);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// 接收轨道机客户端数据
|
||
|
/// </summary>
|
||
|
/// <param name="obj"></param>
|
||
|
private static void GuidaojiExecute(object obj)
|
||
|
{
|
||
|
PrintInfo("GuidaojiExecute(object obj) is begin");
|
||
|
Socket client = (Socket)obj;
|
||
|
try
|
||
|
{
|
||
|
while (true)
|
||
|
{
|
||
|
byte[] buffer = new byte[1024];
|
||
|
int count = client.Receive(buffer);
|
||
|
Thread.Sleep(100);
|
||
|
//连接断开
|
||
|
if (count == 0)
|
||
|
{
|
||
|
//删除字典组中的socket
|
||
|
if (dict.Any(kvp => kvp.Key == iGuidaojiClientPort))
|
||
|
{
|
||
|
DeleteValue(iGuidaojiClientPort);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
if (count > 0)
|
||
|
{
|
||
|
//校验包头
|
||
|
if (buffer[0] == 0x43 && buffer[1] == 0x48)
|
||
|
{
|
||
|
PrintInfo("校验包头成功");
|
||
|
//校验长度
|
||
|
byte[] length = new byte[2];
|
||
|
Array.Copy(buffer, 2, length, 0, 2);
|
||
|
PrintByte("接收长度:" + count + ";长度:", length);
|
||
|
PrintInfo("接收长度:" + count + ";长度:" + BitConverter.ToInt16(FZ(length),0));
|
||
|
int iCount = (int)BitConverter.ToInt16(FZ(length),0);
|
||
|
short sCount = BitConverter.ToInt16(FZ(length),0);
|
||
|
iCount = sCount;
|
||
|
if (count == iCount)
|
||
|
{
|
||
|
PrintInfo("校验长度成功");
|
||
|
// 计算校验码
|
||
|
byte[] data = new byte[count - 2];
|
||
|
Array.Copy(buffer, 0, data, 0, count - 2);
|
||
|
ushort checkBit = ModbusCRC16.CalculateCrc(data);
|
||
|
PrintByte("校验码:", data);
|
||
|
//判断校验码
|
||
|
//if ((byte)checkBit == buffer[count - 2] && (byte)(checkBit >> 8) == buffer[count - 1])
|
||
|
if ((byte)checkBit == buffer[count - 1] && (byte)(checkBit >> 8) == buffer[count - 2])
|
||
|
{
|
||
|
PrintInfo("校验码成功");
|
||
|
//判断设备地址
|
||
|
//todo
|
||
|
//处理数据
|
||
|
try
|
||
|
{
|
||
|
//提取Json数据
|
||
|
string jsonResult = Encoding.UTF8.GetString(data, 8, count - 10);
|
||
|
PrintInfo("接收唐山客户端数据:", jsonResult);
|
||
|
//将JSON解析为指定类型的对象
|
||
|
var guiDaoJiRetTemp = JsonSerializer.Deserialize<GuiDaoJiRet>(jsonResult);
|
||
|
if (guiDaoJiRetTemp != null)
|
||
|
{
|
||
|
//取出在轨道机接收到的数据
|
||
|
byte[] receiveData = new byte[count];
|
||
|
Array.Copy(buffer, 0, receiveData, 0, count);
|
||
|
|
||
|
if (guiDaoJiRetTemp.action == "upload")
|
||
|
{
|
||
|
//自动应答
|
||
|
byte[] senddata = SendDataUpload();
|
||
|
uploadRet = guiDaoJiRetTemp;
|
||
|
|
||
|
//分别给唐山客户端、unity客户端转发数据
|
||
|
if (dict.Any(kvp => kvp.Key == iClientPort))
|
||
|
{
|
||
|
dict[iClientPort].Send(receiveData);
|
||
|
PrintInfo("Send to Client :", Encoding.UTF8.GetString(receiveData));
|
||
|
}
|
||
|
if (dict.Any(kvp => kvp.Key == iUnityClientPort))
|
||
|
{
|
||
|
dict[iUnityClientPort].Send(receiveData);
|
||
|
PrintInfo("Send to UnityClient :", Encoding.UTF8.GetString(receiveData));
|
||
|
}
|
||
|
}
|
||
|
else if (guiDaoJiRetTemp.action == "set" || guiDaoJiRetTemp.action == "read")
|
||
|
{
|
||
|
readAndSetRet = guiDaoJiRetTemp;
|
||
|
PrintInfo("存放到服务器数据:", readAndSetRet.uid + ":" + readAndSetRet.data.y);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
PrintInfo("ClientExecute(object obj) is end false", ex.Message);
|
||
|
//MessageBox.Show(ex.Message);
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
DeleteValue(iGuidaojiClientPort);
|
||
|
PrintInfo("ClientExecute(object obj) is end false", ex.Message);
|
||
|
//MessageBox.Show(ex.Message);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 接收唐山客户端数据
|
||
|
/// </summary>
|
||
|
/// <param name="obj"></param>
|
||
|
private static void ClientExecute(object obj)
|
||
|
{
|
||
|
PrintInfo("ClientExecute(object obj) is begin");
|
||
|
Socket client = (Socket)obj;
|
||
|
try
|
||
|
{
|
||
|
while (true)
|
||
|
{
|
||
|
byte[] buffer = new byte[1024];
|
||
|
int count = client.Receive(buffer);
|
||
|
Thread.Sleep(100);
|
||
|
//连接断开
|
||
|
if (count == 0)
|
||
|
{
|
||
|
//删除字典组中的socket
|
||
|
if (dict.Any(kvp => kvp.Key == iClientPort))
|
||
|
{
|
||
|
DeleteValue(iClientPort);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
PrintInfo("接收唐山客户端数据:", Encoding.UTF8.GetString(buffer, 0, count));
|
||
|
PrintByte("接收唐山客户端数据:", buffer);
|
||
|
if (count == 1)
|
||
|
{
|
||
|
char cCommand = Convert.ToChar(buffer[0]);
|
||
|
switch (cCommand)
|
||
|
{
|
||
|
case (char)ControlCommands.F: //前进
|
||
|
GuiDaoFOrB(600);
|
||
|
break;
|
||
|
case (char)ControlCommands.B: //后退
|
||
|
GuiDaoFOrB(0);
|
||
|
break;
|
||
|
case (char)ControlCommands.U: //上升
|
||
|
GuiDaoJiUOrD(0);
|
||
|
break;
|
||
|
case (char)ControlCommands.D: //下降
|
||
|
GuiDaoJiUOrD(28);
|
||
|
break;
|
||
|
case (char)ControlCommands.S: //停止
|
||
|
GuiDaoJiStop();
|
||
|
break;
|
||
|
case (char)ControlCommands.R: //位置复位
|
||
|
SendReturn();
|
||
|
break;
|
||
|
case (char)ControlCommands.E: //故障复位
|
||
|
SendReset();
|
||
|
break;
|
||
|
default: break;
|
||
|
}
|
||
|
}
|
||
|
else if (count > 1)
|
||
|
{
|
||
|
//校验包头
|
||
|
if (buffer[0] == 0x43 && buffer[1] == 0x48)
|
||
|
{
|
||
|
PrintInfo("校验包头成功");
|
||
|
//校验长度
|
||
|
byte[] length = new byte[2];
|
||
|
Array.Copy(buffer, 2, length, 0, 2);
|
||
|
PrintByte("接收长度:" + count + ";长度:", length);
|
||
|
PrintInfo("接收长度:" + count + ";长度:" + BitConverter.ToInt16(FZ(length), 0));
|
||
|
int iCount = (int)BitConverter.ToInt16(FZ(length), 0);
|
||
|
short sCount = BitConverter.ToInt16(FZ(length),0);
|
||
|
iCount = sCount;
|
||
|
if (count == iCount)
|
||
|
{
|
||
|
PrintInfo("校验长度成功");
|
||
|
// 计算校验码
|
||
|
byte[] data = new byte[count - 2];
|
||
|
Array.Copy(buffer, 0, data, 0, count - 2);
|
||
|
ushort checkBit = ModbusCRC16.CalculateCrc(data);
|
||
|
PrintByte("校验码:", data);
|
||
|
//判断校验码
|
||
|
//if ((byte)checkBit == buffer[count - 2] && (byte)(checkBit >> 8) == buffer[count - 1])
|
||
|
if ((byte)checkBit == buffer[count - 1] && (byte)(checkBit >> 8) == buffer[count - 2])
|
||
|
{
|
||
|
PrintInfo("校验码成功");
|
||
|
//判断设备地址
|
||
|
//todo
|
||
|
//处理数据
|
||
|
try
|
||
|
{
|
||
|
//提取Json数据
|
||
|
string jsonResult = Encoding.UTF8.GetString(data, 8, count - 10);
|
||
|
PrintInfo("接收唐山客户端数据:", jsonResult);
|
||
|
//将JSON解析为指定类型的对象
|
||
|
var guiDaoJiRetTemp = JsonSerializer.Deserialize<GuiDaoJiRet>(jsonResult);
|
||
|
if (guiDaoJiRetTemp != null && guiDaoJiRetTemp.action == "alarm")
|
||
|
{
|
||
|
alarmRet = guiDaoJiRetTemp;
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
PrintInfo("ClientExecute(object obj) is end false", ex.Message);
|
||
|
//MessageBox.Show(ex.Message);
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
DeleteValue(iClientPort);
|
||
|
PrintInfo("ClientExecute(object obj) is end false", ex.Message);
|
||
|
//MessageBox.Show(ex.Message);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 接收Unity客户端数据
|
||
|
/// </summary>
|
||
|
/// <param name="obj"></param>
|
||
|
private static void UnityClientExecute(object obj)
|
||
|
{
|
||
|
PrintInfo("UnityClientExecute(object obj) is begin");
|
||
|
Socket client = (Socket)obj;
|
||
|
try
|
||
|
{
|
||
|
while (true)
|
||
|
{
|
||
|
byte[] buffer = new byte[1024];
|
||
|
int count = client.Receive(buffer);
|
||
|
//连接断开
|
||
|
if (count == 0)
|
||
|
{
|
||
|
//删除字典组中的socket
|
||
|
if (dict.Any(kvp => kvp.Key == iUnityClientPort))
|
||
|
{
|
||
|
DeleteValue(iUnityClientPort);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
if (count > 0)
|
||
|
{
|
||
|
//将接收到的数据转换为字符串
|
||
|
string response = Encoding.UTF8.GetString(buffer, 0, count);
|
||
|
//获取Json数据的起始位置和长度
|
||
|
int startIndex = response.IndexOf('{');
|
||
|
int endIndex = response.LastIndexOf('}');
|
||
|
int jsonLength = endIndex - startIndex + 1;
|
||
|
//提取Json数据
|
||
|
string jsonResult = response.Substring(startIndex, jsonLength);
|
||
|
//将JSON解析为指定类型的对象
|
||
|
try
|
||
|
{
|
||
|
var guiDaoJiRetTemp = JsonSerializer.Deserialize<GuiDaoJiRet>(jsonResult);
|
||
|
if (guiDaoJiRetTemp != null)
|
||
|
{
|
||
|
if (guiDaoJiRetTemp.action == "upload")
|
||
|
{
|
||
|
//自动应答
|
||
|
byte[] senddata = SendDataUpload();
|
||
|
if (senddata != null)
|
||
|
{
|
||
|
if (dict.Any(kvp => kvp.Key == iUnityClientPort))
|
||
|
{
|
||
|
dict[iUnityClientPort].Send(senddata);
|
||
|
PrintInfo("Send to UnityClient:", Encoding.UTF8.GetString(senddata, 0, senddata.Length));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
PrintInfo("UnityClientExecute(object obj) is end false", ex.Message);
|
||
|
//MessageBox.Show(ex.Message);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
DeleteValue(iUnityClientPort);
|
||
|
PrintInfo("UnityClientExecute(object obj) is end false", ex.Message);
|
||
|
//MessageBox.Show(ex.Message);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 读轨道机位置
|
||
|
/// </summary>
|
||
|
/// <returns></returns>
|
||
|
public static GuiDaoJiRet SendDataRead()
|
||
|
{
|
||
|
PrintInfo("SendDataRead() is begin");
|
||
|
string uid = "XJ00001";
|
||
|
string action = "read";
|
||
|
DateTime date = DateTime.Now;
|
||
|
string fort = date.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
Console.WriteLine(fort);
|
||
|
// 创建JSON数据对象
|
||
|
var json = new
|
||
|
{
|
||
|
uid = uid,
|
||
|
action = action,
|
||
|
date = fort
|
||
|
};
|
||
|
|
||
|
// 将JSON数据对象序列化为字符串
|
||
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
||
|
string jsonString = JsonSerializer.Serialize(json, options);
|
||
|
Console.WriteLine(jsonString);
|
||
|
// 将JSON字符串转换为字节数组
|
||
|
byte[] dataBytes = Encoding.UTF8.GetBytes(jsonString);
|
||
|
|
||
|
// 定义包头和设备地址
|
||
|
ushort baoTou = 0x4348;
|
||
|
ushort ADDR1 = 0x7;
|
||
|
ushort ADDR2 = 0xc925;
|
||
|
|
||
|
// 计算数据长度
|
||
|
ushort length = (ushort)(10 + dataBytes.Length);
|
||
|
Console.WriteLine(length);
|
||
|
// 将数据长度转换为字节数组并存储到集合中
|
||
|
List<byte> numberBytesList = new List<byte>();
|
||
|
|
||
|
numberBytesList.AddRange(FZ(baoTou));
|
||
|
numberBytesList.AddRange(FZ(length));
|
||
|
numberBytesList.AddRange(FZ(ADDR1));
|
||
|
numberBytesList.AddRange(FZ(ADDR2));
|
||
|
|
||
|
numberBytesList.AddRange(dataBytes);
|
||
|
//// 将包头、数据长度、设备地址和校验码组合成一个完整的字节数组
|
||
|
//List<byte> combineBytes = new List<byte>();
|
||
|
//foreach (var item in numberBytesList)
|
||
|
//{
|
||
|
// combineBytes.Add(item);
|
||
|
//}
|
||
|
//var hex = BitConverter.ToString(dataBytes, 0).Replace("-", string.Empty).ToLower();
|
||
|
//Console.WriteLine(hex);
|
||
|
byte[] Bytes = numberBytesList.ToArray();
|
||
|
|
||
|
// 计算校验码
|
||
|
ushort checkBit = ModbusCRC16.CalculateCrc(Bytes);
|
||
|
byte[] ushortBytes = BitConverter.GetBytes(checkBit);
|
||
|
|
||
|
numberBytesList.AddRange(FZ(checkBit));
|
||
|
byte[] data = numberBytesList.ToArray();
|
||
|
var hex = BitConverter.ToString(data, 0).Replace("-", string.Empty).ToLower();
|
||
|
Console.WriteLine(hex);
|
||
|
|
||
|
|
||
|
// 将字节数组转换为大端字节序
|
||
|
//var finalBytes = Bytes.SelectMany(i => BitConverter.GetBytes(i)).ToArray();
|
||
|
if (BitConverter.IsLittleEndian)
|
||
|
{
|
||
|
// 发送数据
|
||
|
PrintInfo("Send data:", Encoding.UTF8.GetString(data, 0, data.Length));
|
||
|
return (SendDataRead(data));
|
||
|
}
|
||
|
PrintInfo("SendDataRead() is end false");
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public static bool SendDataSet(GuiDaoJiSet guiDaoJiSet)
|
||
|
{
|
||
|
PrintInfo("SendDataSet(GuiDaoJi guiDaoJi) is begin");
|
||
|
|
||
|
// 将JSON数据对象序列化为字符串
|
||
|
var options = new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) };
|
||
|
string jsonString = JsonSerializer.Serialize(guiDaoJiSet, options);
|
||
|
PrintInfo("Json串:" + jsonString);
|
||
|
|
||
|
// 将JSON字符串转换为字节数组
|
||
|
// 将字符串转换为GB2312编码的字节数组
|
||
|
//注册
|
||
|
Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||
|
Encoding gb2312 = Encoding.GetEncoding("GB2312");
|
||
|
byte[] dataBytes = gb2312.GetBytes(jsonString);
|
||
|
//byte[] dataBytes = Encoding.UTF8.GetBytes(jsonString);
|
||
|
PrintByte("---", dataBytes);
|
||
|
|
||
|
// 定义包头和设备地址
|
||
|
ushort baoTou = 0x4348;
|
||
|
ushort ADDR1 = 0x7;
|
||
|
ushort ADDR2 = 0xc925;
|
||
|
|
||
|
// 计算数据长度
|
||
|
int ushortSize = sizeof(ushort);
|
||
|
int totalUshortSize = 2 * ushortSize;
|
||
|
ushort length = (ushort)(10 + dataBytes.Length);
|
||
|
|
||
|
// 将数据长度转换为字节数组并存储到集合中
|
||
|
List<byte> numberBytesList = new List<byte>();
|
||
|
|
||
|
numberBytesList.AddRange(FZ(baoTou));
|
||
|
numberBytesList.AddRange(FZ(length));
|
||
|
numberBytesList.AddRange(FZ(ADDR1));
|
||
|
numberBytesList.AddRange(FZ(ADDR2));
|
||
|
|
||
|
//// 将包头、数据长度、设备地址和校验码组合成一个完整的字节数组
|
||
|
//List<byte> combineBytes = new List<byte>();
|
||
|
//foreach (var item in numberBytesList)
|
||
|
//{
|
||
|
// combineBytes.Add(item);
|
||
|
//}
|
||
|
numberBytesList.AddRange(dataBytes);
|
||
|
//var hex = BitConverter.ToString(dataBytes, 0).Replace("-", string.Empty).ToLower();
|
||
|
//Console.WriteLine(hex);
|
||
|
byte[] Bytes = numberBytesList.ToArray();
|
||
|
|
||
|
// 计算校验码
|
||
|
ushort checkBit = ModbusCRC16.CalculateCrc(Bytes);
|
||
|
|
||
|
// 将校验码转换为字节数组
|
||
|
byte[] ushortBytes = BitConverter.GetBytes(checkBit);
|
||
|
|
||
|
numberBytesList.AddRange(FZ(checkBit));
|
||
|
byte[] data = numberBytesList.ToArray();
|
||
|
var hex = BitConverter.ToString(data, 0).Replace("-", string.Empty).ToLower();
|
||
|
Console.WriteLine(hex);
|
||
|
|
||
|
|
||
|
// 将字节数组转换为大端字节序
|
||
|
//var finalBytes = Bytes.SelectMany(i => BitConverter.GetBytes(i)).ToArray();
|
||
|
if (BitConverter.IsLittleEndian)
|
||
|
{
|
||
|
// 发送数据
|
||
|
if (SendDataSet(data) != null)
|
||
|
{
|
||
|
PrintInfo("SendDataSet(GuiDaoJi guiDaoJi) is end true");
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
PrintInfo("SendDataSet(GuiDaoJi guiDaoJi) is end false");
|
||
|
return false;
|
||
|
}
|
||
|
public static byte[] SendDataUpload()
|
||
|
{
|
||
|
PrintInfo("SendDataUpload() is begin");
|
||
|
string uid = "XJ00001";
|
||
|
string action = "upload";
|
||
|
string result = "OK";
|
||
|
string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
|
||
|
// 创建JSON数据对象
|
||
|
var json = new
|
||
|
{
|
||
|
uid = uid,
|
||
|
action = action,
|
||
|
date = date,
|
||
|
result = result
|
||
|
};
|
||
|
|
||
|
// 将JSON数据对象序列化为字符串
|
||
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
||
|
string jsonString = JsonSerializer.Serialize(json, options);
|
||
|
|
||
|
// 将JSON字符串转换为字节数组
|
||
|
byte[] dataBytes = Encoding.UTF8.GetBytes(jsonString);
|
||
|
|
||
|
// 定义包头和设备地址
|
||
|
ushort baoTou = 0x4348;
|
||
|
ushort ADDR1 = 0x7;
|
||
|
ushort ADDR2 = 0xc925;
|
||
|
|
||
|
// 计算数据长度
|
||
|
int ushortSize = sizeof(ushort);
|
||
|
int totalUshortSize = 2 * ushortSize;
|
||
|
ushort length = (ushort)(10 + dataBytes.Length);
|
||
|
|
||
|
// 将数据长度转换为字节数组并存储到集合中
|
||
|
List<byte> numberBytesList = new List<byte>();
|
||
|
|
||
|
numberBytesList.AddRange(FZ(baoTou));
|
||
|
numberBytesList.AddRange(FZ(length));
|
||
|
numberBytesList.AddRange(FZ(ADDR1));
|
||
|
numberBytesList.AddRange(FZ(ADDR2));
|
||
|
|
||
|
// 将包头、数据长度、设备地址和校验码组合成一个完整的字节数组
|
||
|
//List<byte> combineBytes = new List<byte>();
|
||
|
//foreach (var item in numberBytesList)
|
||
|
//{
|
||
|
// combineBytes.Add(item);
|
||
|
//}
|
||
|
numberBytesList.AddRange(dataBytes);
|
||
|
//var hex = BitConverter.ToString(dataBytes, 0).Replace("-", string.Empty).ToLower();
|
||
|
//Console.WriteLine(hex);
|
||
|
byte[] Bytes = numberBytesList.ToArray();
|
||
|
|
||
|
// 计算校验码
|
||
|
ushort checkBit = ModbusCRC16.CalculateCrc(Bytes);
|
||
|
|
||
|
// 将校验码转换为字节数组
|
||
|
byte[] ushortBytes = BitConverter.GetBytes(checkBit);
|
||
|
|
||
|
numberBytesList.AddRange(FZ(checkBit));
|
||
|
byte[] data = numberBytesList.ToArray();
|
||
|
var hex = BitConverter.ToString(data, 0).Replace("-", string.Empty).ToLower();
|
||
|
Console.WriteLine(hex);
|
||
|
|
||
|
|
||
|
// 将字节数组转换为大端字节序
|
||
|
//var finalBytes = Bytes.SelectMany(i => BitConverter.GetBytes(i)).ToArray();
|
||
|
if (BitConverter.IsLittleEndian)
|
||
|
{
|
||
|
// 给轨道机客户端回数据
|
||
|
//socket_commu.Send(data);
|
||
|
PrintInfo("SendDataUpload() is end true. 给轨道机客户端回数据:", Encoding.UTF8.GetString(data, 0, data.Length));
|
||
|
return data;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
PrintInfo("SendDataUpload() is end false");
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 发送报警信息
|
||
|
/// </summary>
|
||
|
/// <param name="sAlarmMessage"></param>
|
||
|
/// <returns></returns>
|
||
|
public static bool SendDataAlarm(string sAlarmMessage)
|
||
|
{
|
||
|
PrintInfo("SendDataAlarm(string sAlarmMessage) is begin");
|
||
|
string uid = "XJ00001";
|
||
|
string action = "alarm";
|
||
|
string result = sAlarmMessage;
|
||
|
string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
|
||
|
// 创建JSON数据对象
|
||
|
var json = new
|
||
|
{
|
||
|
uid = uid,
|
||
|
action = action,
|
||
|
date = date,
|
||
|
result = result
|
||
|
};
|
||
|
|
||
|
// 将JSON数据对象序列化为字符串
|
||
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
||
|
string jsonString = JsonSerializer.Serialize(json, options);
|
||
|
|
||
|
// 将JSON字符串转换为字节数组
|
||
|
byte[] dataBytes = Encoding.UTF8.GetBytes(jsonString);
|
||
|
|
||
|
// 定义包头和设备地址
|
||
|
ushort baoTou = 0x4348;
|
||
|
ushort ADDR1 = 0x7;
|
||
|
ushort ADDR2 = 0xc925;
|
||
|
|
||
|
// 计算数据长度
|
||
|
int ushortSize = sizeof(ushort);
|
||
|
int totalUshortSize = 2 * ushortSize;
|
||
|
ushort length = (ushort)(10 + dataBytes.Length);
|
||
|
|
||
|
// 将数据长度转换为字节数组并存储到集合中
|
||
|
List<byte> numberBytesList = new List<byte>();
|
||
|
|
||
|
numberBytesList.AddRange(FZ(baoTou));
|
||
|
numberBytesList.AddRange(FZ(length));
|
||
|
numberBytesList.AddRange(FZ(ADDR1));
|
||
|
numberBytesList.AddRange(FZ(ADDR2));
|
||
|
|
||
|
// 将包头、数据长度、设备地址和校验码组合成一个完整的字节数组
|
||
|
//List<byte> combineBytes = new List<byte>();
|
||
|
//foreach (var item in numberBytesList)
|
||
|
//{
|
||
|
// combineBytes.Add(item);
|
||
|
//}
|
||
|
numberBytesList.AddRange(dataBytes);
|
||
|
//var hex = BitConverter.ToString(dataBytes, 0).Replace("-", string.Empty).ToLower();
|
||
|
//Console.WriteLine(hex);
|
||
|
byte[] Bytes = numberBytesList.ToArray();
|
||
|
|
||
|
// 计算校验码
|
||
|
ushort checkBit = ModbusCRC16.CalculateCrc(Bytes);
|
||
|
|
||
|
// 将校验码转换为字节数组
|
||
|
byte[] ushortBytes = BitConverter.GetBytes(checkBit);
|
||
|
|
||
|
numberBytesList.AddRange(FZ(checkBit));
|
||
|
byte[] data = numberBytesList.ToArray();
|
||
|
var hex = BitConverter.ToString(data, 0).Replace("-", string.Empty).ToLower();
|
||
|
Console.WriteLine(hex);
|
||
|
|
||
|
|
||
|
// 将字节数组转换为大端字节序
|
||
|
//var finalBytes = Bytes.SelectMany(i => BitConverter.GetBytes(i)).ToArray();
|
||
|
if (BitConverter.IsLittleEndian)
|
||
|
{
|
||
|
if (SendDataAlarm(data) != null)
|
||
|
{
|
||
|
PrintInfo("SendDataAlarm(string sAlarmMessage) is end true");
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
PrintInfo("SendDataAlarm(string sAlarmMessage) is end false");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 移动轨道机过程
|
||
|
/// </summary>
|
||
|
/// <param name="iMoveX">前后</param>
|
||
|
/// <param name="iMoveY">上下</param>
|
||
|
/// <param name="iSelIndex">选择通道</param>
|
||
|
/// <param name="sViedoPath">保存视频路径</param>
|
||
|
/// <returns></returns>
|
||
|
public static bool GuiDaoJMove(double iMoveX, double iMoveY, int iSelIndex, string sViedoPath)
|
||
|
{
|
||
|
PrintInfo("GuiDaoJMove(double iMoveX, double iMoveY, int iSelIndex, string sViedoPath)", iMoveX.ToString() + ":" + iMoveY);
|
||
|
if (!bConnect)
|
||
|
return false;
|
||
|
if (uploadRet.data.alarm[0] == 1)
|
||
|
{
|
||
|
PrintInfo("报警返回");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
GuiDaoJiRet guiDaoJiRet = SendDataRead();
|
||
|
if (guiDaoJiRet != null)
|
||
|
{
|
||
|
PrintInfo("------------1-----------");
|
||
|
//如果需要移动的位置和当前位置一致
|
||
|
if (iMoveX.ToString() == guiDaoJiRet.data.x && iMoveY.ToString() == guiDaoJiRet.data.y)
|
||
|
{
|
||
|
PrintInfo("------------2-----------");
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
CHNetHelp.SaveViedo(true, iSelIndex, sViedoPath);
|
||
|
string sUid = "XJ00001";
|
||
|
GuiDaoJiSet guiDaoJi = new GuiDaoJiSet();
|
||
|
guiDaoJi.uid = sUid;
|
||
|
guiDaoJi.action = "set";
|
||
|
guiDaoJi.send_gap = 1;
|
||
|
guiDaoJi.cont = 0;
|
||
|
guiDaoJi.contda.@return = 0;
|
||
|
guiDaoJi.contda.reset = 0;
|
||
|
guiDaoJi.contda.lamp = 0;
|
||
|
guiDaoJi.move = 1;
|
||
|
guiDaoJi.moveda.rate = 2;
|
||
|
guiDaoJi.moveda.x = guiDaoJiRet.data.x;
|
||
|
guiDaoJi.moveda.y = iMoveY.ToString();
|
||
|
guiDaoJi.voice = 0;
|
||
|
guiDaoJi.voiceda.@string = "";
|
||
|
guiDaoJi.date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
|
||
|
//如果轨道机在上面
|
||
|
if (guiDaoJiRet.data.y != "0.0")
|
||
|
{
|
||
|
guiDaoJi.moveda.y = "0.0";
|
||
|
guiDaoJi.date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
if (SendDataSet(guiDaoJi))
|
||
|
{
|
||
|
PrintInfo("--------------向上-------------", dPos[0] + ":" + dPos[1]);
|
||
|
while (true)
|
||
|
{
|
||
|
if (double.Parse(dPos[1]) == 0)
|
||
|
break;
|
||
|
Thread.Sleep(100);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
guiDaoJi.moveda.x = iMoveX.ToString();
|
||
|
guiDaoJi.moveda.y = "0.0";
|
||
|
guiDaoJi.date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
if (SendDataSet(guiDaoJi))
|
||
|
{
|
||
|
while (true)
|
||
|
{
|
||
|
PrintInfo("--------------向前-------------");
|
||
|
if (dPos[0] == iMoveX.ToString())
|
||
|
break;
|
||
|
Thread.Sleep(100);
|
||
|
}
|
||
|
|
||
|
guiDaoJi.moveda.x = iMoveX.ToString();
|
||
|
guiDaoJi.moveda.y = iMoveY.ToString();
|
||
|
guiDaoJi.date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
if (SendDataSet(guiDaoJi))
|
||
|
{
|
||
|
while (true)
|
||
|
{
|
||
|
PrintInfo("--------------向下-------------");
|
||
|
if (dPos[1] == iMoveY.ToString())
|
||
|
{
|
||
|
CHNetHelp.SaveViedo(false, iSelIndex, sViedoPath);
|
||
|
return true;
|
||
|
}
|
||
|
Thread.Sleep(100);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
CHNetHelp.SaveViedo(false, iSelIndex, sViedoPath);
|
||
|
}
|
||
|
return false;
|
||
|
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 轨道机上下移动
|
||
|
/// </summary>
|
||
|
/// <param name="iMoveX">前后</param>
|
||
|
/// <returns></returns>
|
||
|
public static bool GuiDaoJiUpOrDown(double iMoveX, double iMoveY)
|
||
|
{
|
||
|
PrintInfo("GuiDaoJiUpOrDown(double iMoveY) is begin");
|
||
|
if (!bConnect)
|
||
|
return false;
|
||
|
if (uploadRet.data.alarm[0] == 1)
|
||
|
{
|
||
|
PrintInfo("报警返回");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
GuiDaoJiRet guiDaoJiRet1 = SendDataRead();
|
||
|
if (guiDaoJiRet1 != null)
|
||
|
{
|
||
|
PrintInfo(guiDaoJiRet1.uid + ":" + guiDaoJiRet1.data.y);
|
||
|
////如果需要移动的位置和当前位置一致
|
||
|
//if (guiDaoJiRet1.data.y == "0.0" || guiDaoJiRet1.data.y == "-0.0")
|
||
|
// return true;
|
||
|
|
||
|
PrintInfo("------------2-------------", Math.Round((double.Parse(guiDaoJiRet1.data.x) + iMoveX), 1).ToString() + ":" + Math.Round(double.Parse(guiDaoJiRet1.data.y) + iMoveY, 1).ToString());
|
||
|
string sUid = "XJ00001";
|
||
|
GuiDaoJiSet guiDaoJiSet = new GuiDaoJiSet();
|
||
|
guiDaoJiSet.uid = sUid;
|
||
|
guiDaoJiSet.action = "set";
|
||
|
guiDaoJiSet.send_gap = 1;
|
||
|
guiDaoJiSet.cont = 0;
|
||
|
guiDaoJiSet.contda.@return = 0;
|
||
|
guiDaoJiSet.contda.reset = 0;
|
||
|
guiDaoJiSet.contda.lamp = 0;
|
||
|
guiDaoJiSet.move = 1;
|
||
|
guiDaoJiSet.moveda.rate = 2;
|
||
|
guiDaoJiSet.moveda.x = Math.Round((double.Parse(guiDaoJiRet1.data.x) + iMoveX), 1).ToString();
|
||
|
guiDaoJiSet.moveda.y = Math.Round(double.Parse(guiDaoJiRet1.data.y) + iMoveY, 1).ToString();
|
||
|
guiDaoJiSet.voice = 0;
|
||
|
//Voiceda voiceda = new Voiceda();
|
||
|
//voiceda.@string = "";
|
||
|
//guiDaoJiSet.voiceda.Add(voiceda);
|
||
|
guiDaoJiSet.voiceda.@string = "";
|
||
|
guiDaoJiSet.date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
PrintInfo("------------3-------------", guiDaoJiSet.uid + ":" + guiDaoJiSet.moveda.y);
|
||
|
return SendDataSet(guiDaoJiSet);
|
||
|
}
|
||
|
PrintInfo("GuiDaoJiUpOrDown(double iMoveY) is end false");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 轨道机前后移动
|
||
|
/// </summary>
|
||
|
/// <param name="iMoveX">前后</param>
|
||
|
/// <returns></returns>
|
||
|
public static bool GuiDaoFOrB(double iMoveX)
|
||
|
{
|
||
|
PrintInfo("GuiDaoJiUpOrDown(double iMoveY) is begin");
|
||
|
if (!bConnect)
|
||
|
return false;
|
||
|
if (uploadRet.data.alarm[0] == 1)
|
||
|
{
|
||
|
PrintInfo("报警返回");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
GuiDaoJiRet guiDaoJiRet1 = SendDataRead();
|
||
|
if (guiDaoJiRet1 != null)
|
||
|
{
|
||
|
PrintInfo(guiDaoJiRet1.uid + ":" + guiDaoJiRet1.data.y);
|
||
|
////如果需要移动的位置和当前位置一致
|
||
|
//if (guiDaoJiRet1.data.y == "0.0" || guiDaoJiRet1.data.y == "-0.0")
|
||
|
// return true;
|
||
|
|
||
|
PrintInfo("------------2-------------", Math.Round((double.Parse(guiDaoJiRet1.data.x) + iMoveX), 1).ToString());
|
||
|
string sUid = "XJ00001";
|
||
|
GuiDaoJiSet guiDaoJiSet = new GuiDaoJiSet();
|
||
|
guiDaoJiSet.uid = sUid;
|
||
|
guiDaoJiSet.action = "set";
|
||
|
guiDaoJiSet.send_gap = 1;
|
||
|
guiDaoJiSet.cont = 0;
|
||
|
guiDaoJiSet.contda.@return = 0;
|
||
|
guiDaoJiSet.contda.reset = 0;
|
||
|
guiDaoJiSet.contda.lamp = 0;
|
||
|
guiDaoJiSet.move = 1;
|
||
|
guiDaoJiSet.moveda.rate = 2;
|
||
|
guiDaoJiSet.moveda.x = Math.Round(iMoveX, 1).ToString();
|
||
|
guiDaoJiSet.moveda.y = Math.Round(double.Parse(guiDaoJiRet1.data.y), 1).ToString();
|
||
|
guiDaoJiSet.voice = 0;
|
||
|
//Voiceda voiceda = new Voiceda();
|
||
|
//voiceda.@string = "";
|
||
|
//guiDaoJiSet.voiceda.Add(voiceda);
|
||
|
guiDaoJiSet.voiceda.@string = "";
|
||
|
guiDaoJiSet.date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
PrintInfo("------------3-------------", guiDaoJiSet.uid + ":" + guiDaoJiSet.moveda.y);
|
||
|
return SendDataSet(guiDaoJiSet);
|
||
|
}
|
||
|
PrintInfo("GuiDaoJiUpOrDown(double iMoveY) is end false");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 轨道机上下移动
|
||
|
/// </summary>
|
||
|
/// <param name="iMoveY">上下</param>
|
||
|
/// <returns></returns>
|
||
|
public static bool GuiDaoJiUOrD(double iMoveY)
|
||
|
{
|
||
|
PrintInfo("GuiDaoJiUpOrDown(double iMoveY) is begin");
|
||
|
if (!bConnect)
|
||
|
return false;
|
||
|
if (uploadRet.data.alarm[0] == 1)
|
||
|
{
|
||
|
PrintInfo("报警返回");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
GuiDaoJiRet guiDaoJiRet1 = SendDataRead();
|
||
|
if (guiDaoJiRet1 != null)
|
||
|
{
|
||
|
PrintInfo(guiDaoJiRet1.uid + ":" + guiDaoJiRet1.data.y);
|
||
|
////如果需要移动的位置和当前位置一致
|
||
|
//if (guiDaoJiRet1.data.y == "0.0" || guiDaoJiRet1.data.y == "-0.0")
|
||
|
// return true;
|
||
|
|
||
|
PrintInfo("------------2-------------", Math.Round(double.Parse(guiDaoJiRet1.data.y) + iMoveY, 1).ToString());
|
||
|
string sUid = "XJ00001";
|
||
|
GuiDaoJiSet guiDaoJiSet = new GuiDaoJiSet();
|
||
|
guiDaoJiSet.uid = sUid;
|
||
|
guiDaoJiSet.action = "set";
|
||
|
guiDaoJiSet.send_gap = 1;
|
||
|
guiDaoJiSet.cont = 0;
|
||
|
guiDaoJiSet.contda.@return = 0;
|
||
|
guiDaoJiSet.contda.reset = 0;
|
||
|
guiDaoJiSet.contda.lamp = 0;
|
||
|
guiDaoJiSet.move = 1;
|
||
|
guiDaoJiSet.moveda.rate = 2;
|
||
|
guiDaoJiSet.moveda.x = Math.Round(double.Parse(guiDaoJiRet1.data.x), 1).ToString();
|
||
|
guiDaoJiSet.moveda.y = Math.Round(iMoveY, 1).ToString();
|
||
|
guiDaoJiSet.voice = 0;
|
||
|
//Voiceda voiceda = new Voiceda();
|
||
|
//voiceda.@string = "";
|
||
|
//guiDaoJiSet.voiceda.Add(voiceda);
|
||
|
guiDaoJiSet.voiceda.@string = "";
|
||
|
guiDaoJiSet.date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
PrintInfo("------------3-------------", guiDaoJiSet.uid + ":" + guiDaoJiSet.moveda.y);
|
||
|
return SendDataSet(guiDaoJiSet);
|
||
|
}
|
||
|
PrintInfo("GuiDaoJiUpOrDown(double iMoveY) is end false");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public static void GuiDaoJiStop()
|
||
|
{
|
||
|
PrintInfo("------------Stop1-------------");
|
||
|
string sUid = "XJ00001";
|
||
|
GuiDaoJiSet guiDaoJiSet = new GuiDaoJiSet();
|
||
|
guiDaoJiSet.uid = sUid;
|
||
|
guiDaoJiSet.action = "set";
|
||
|
guiDaoJiSet.send_gap = 1;
|
||
|
guiDaoJiSet.cont = 0;
|
||
|
guiDaoJiSet.contda.@return = 0;
|
||
|
guiDaoJiSet.contda.reset = 0;
|
||
|
guiDaoJiSet.contda.lamp = 0;
|
||
|
guiDaoJiSet.move = 1;
|
||
|
guiDaoJiSet.moveda.rate = 0;
|
||
|
guiDaoJiSet.moveda.x = "0.0";
|
||
|
guiDaoJiSet.moveda.y = "0.0";
|
||
|
guiDaoJiSet.voice = 0;
|
||
|
guiDaoJiSet.voiceda.@string = "";
|
||
|
guiDaoJiSet.date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
PrintInfo("------------Stop2-------------", guiDaoJiSet.uid + ":" + guiDaoJiSet.moveda.y);
|
||
|
SendDataSet(guiDaoJiSet);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 轨道机上下移动
|
||
|
/// </summary>
|
||
|
/// <param name="iMoveX">前后</param>
|
||
|
/// <returns></returns>
|
||
|
public static bool GuiDaoJiMove(double iMoveX, double iMoveY)
|
||
|
{
|
||
|
PrintInfo("GuiDaoJiUpOrDown(double iMoveY) is begin");
|
||
|
if (!bConnect)
|
||
|
return false;
|
||
|
if (uploadRet.data.alarm[0] == 1)
|
||
|
{
|
||
|
PrintInfo("报警返回");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
PrintInfo("------------2-------------");
|
||
|
string sUid = "XJ00001";
|
||
|
GuiDaoJiSet guiDaoJiSet = new GuiDaoJiSet();
|
||
|
guiDaoJiSet.uid = sUid;
|
||
|
guiDaoJiSet.action = "set";
|
||
|
guiDaoJiSet.send_gap = 1;
|
||
|
guiDaoJiSet.cont = 0;
|
||
|
guiDaoJiSet.contda.@return = 0;
|
||
|
guiDaoJiSet.contda.reset = 0;
|
||
|
guiDaoJiSet.contda.lamp = 0;
|
||
|
guiDaoJiSet.move = 1;
|
||
|
guiDaoJiSet.moveda.rate = 2;
|
||
|
guiDaoJiSet.moveda.x = Math.Round(iMoveX, 1).ToString();
|
||
|
guiDaoJiSet.moveda.y = Math.Round(iMoveY, 1).ToString();
|
||
|
guiDaoJiSet.voice = 0;
|
||
|
guiDaoJiSet.voiceda.@string = "";
|
||
|
guiDaoJiSet.date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
PrintInfo("------------3-------------", guiDaoJiSet.uid + ":" + guiDaoJiSet.moveda.y);
|
||
|
return SendDataSet(guiDaoJiSet);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 轨道机移动
|
||
|
/// </summary>
|
||
|
/// <param name="iLed">灯状态</param>
|
||
|
/// <param name="sVoice">语音播放内容</param>
|
||
|
public static bool SetLedAndVoice(int iLed, string sVoice)
|
||
|
{
|
||
|
string sUid = "XJ00001";
|
||
|
GuiDaoJiSet guiDaoJi = new GuiDaoJiSet();
|
||
|
guiDaoJi.uid = sUid;
|
||
|
guiDaoJi.action = "set";
|
||
|
guiDaoJi.send_gap = 2;
|
||
|
guiDaoJi.cont = 1;
|
||
|
guiDaoJi.contda.@return = 0;
|
||
|
guiDaoJi.contda.reset = 0;
|
||
|
guiDaoJi.contda.lamp = iLed;
|
||
|
guiDaoJi.move = 0;
|
||
|
guiDaoJi.moveda.rate = 0;
|
||
|
guiDaoJi.moveda.x = "0.0";
|
||
|
guiDaoJi.moveda.y = "0.0";
|
||
|
guiDaoJi.voice = 1;
|
||
|
guiDaoJi.voiceda.@string = sVoice;
|
||
|
guiDaoJi.date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
|
||
|
return SendDataSet(guiDaoJi);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 轨道故障复位
|
||
|
/// </summary>
|
||
|
public static bool SendReset()
|
||
|
{
|
||
|
string sUid = "XJ00001";
|
||
|
GuiDaoJiSet guiDaoJi = new GuiDaoJiSet();
|
||
|
guiDaoJi.uid = sUid;
|
||
|
guiDaoJi.action = "set";
|
||
|
guiDaoJi.send_gap = 2;
|
||
|
guiDaoJi.cont = 1;
|
||
|
guiDaoJi.contda.@return = 0;
|
||
|
guiDaoJi.contda.reset = 1;
|
||
|
guiDaoJi.contda.lamp = 0;
|
||
|
guiDaoJi.move = 0;
|
||
|
guiDaoJi.moveda.rate = 0;
|
||
|
guiDaoJi.moveda.x = "0.0";
|
||
|
guiDaoJi.moveda.y = "0.0";
|
||
|
guiDaoJi.voice = 0;
|
||
|
guiDaoJi.voiceda.@string = "";
|
||
|
guiDaoJi.date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
|
||
|
return SendDataSet(guiDaoJi);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 轨道位置复位
|
||
|
/// </summary>
|
||
|
public static bool SendReturn()
|
||
|
{
|
||
|
string sUid = "XJ00001";
|
||
|
GuiDaoJiSet guiDaoJi = new GuiDaoJiSet();
|
||
|
guiDaoJi.uid = sUid;
|
||
|
guiDaoJi.action = "set";
|
||
|
guiDaoJi.send_gap = 2;
|
||
|
guiDaoJi.cont = 1;
|
||
|
guiDaoJi.contda.@return = 1;
|
||
|
guiDaoJi.contda.reset = 0;
|
||
|
guiDaoJi.contda.lamp = 0;
|
||
|
guiDaoJi.move = 0;
|
||
|
guiDaoJi.moveda.rate = 0;
|
||
|
guiDaoJi.moveda.x = "0.0";
|
||
|
guiDaoJi.moveda.y = "0.0";
|
||
|
guiDaoJi.voice = 0;
|
||
|
guiDaoJi.voiceda.@string = "";
|
||
|
guiDaoJi.date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
|
||
|
return SendDataSet(guiDaoJi);
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// 向轨道机客户端发数据
|
||
|
/// </summary>
|
||
|
/// <param name="btData"></param>
|
||
|
/// <returns></returns>
|
||
|
public static GuiDaoJiRet SendDataSet(byte[] btData)
|
||
|
{
|
||
|
PrintInfo("GuiDaoJiRet SendDataSet(byte[] btData) is begin");
|
||
|
if (dict.Any(kvp => kvp.Key == iGuidaojiClientPort))
|
||
|
{
|
||
|
dict[iGuidaojiClientPort].Send(btData);
|
||
|
uint time = 50;
|
||
|
while (time-- != 0)
|
||
|
{
|
||
|
PrintInfo("time:", time.ToString());
|
||
|
PrintInfo("sSetState:", "set");
|
||
|
if (readAndSetRet != null && readAndSetRet.action == "set")
|
||
|
{
|
||
|
GuiDaoJiRet sData = readAndSetRet;
|
||
|
PrintInfo("GuiDaoJiRet SendDataSet(byte[] btData) is end true. ", readAndSetRet.uid + ":" + readAndSetRet.data.y);
|
||
|
PrintInfo("GuiDaoJiRet SendDataSet(byte[] btData) is end true. ", sData.uid + ":" + sData.data.y);
|
||
|
return sData;
|
||
|
}
|
||
|
Thread.Sleep(100);
|
||
|
}
|
||
|
}
|
||
|
PrintInfo("GuiDaoJiRet SendDataSet(byte[] btData) is end false");
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 向轨道机客户端发数据
|
||
|
/// </summary>
|
||
|
/// <param name="btData"></param>
|
||
|
/// <returns></returns>
|
||
|
public static GuiDaoJiRet SendDataRead(byte[] btData)
|
||
|
{
|
||
|
PrintInfo("GuiDaoJiRet SendDataRead(byte[] btData) is begin");
|
||
|
if (dict.Any(kvp => kvp.Key == iGuidaojiClientPort))
|
||
|
{
|
||
|
dict[iGuidaojiClientPort].Send(btData);
|
||
|
uint time = 50;
|
||
|
while (time-- != 0)
|
||
|
{
|
||
|
PrintInfo("time:", time.ToString());
|
||
|
PrintInfo("sSetState:", "read");
|
||
|
if (readAndSetRet != null && readAndSetRet.action == "read")
|
||
|
{
|
||
|
GuiDaoJiRet sData = readAndSetRet;
|
||
|
PrintInfo("GuiDaoJiRet SendDataRead(byte[] btData) is end true. ", readAndSetRet.uid + ":" + readAndSetRet.data.y);
|
||
|
PrintInfo("GuiDaoJiRet SendDataRead(byte[] btData) is end true. ", sData.uid + ":" + sData.data.y);
|
||
|
return sData;
|
||
|
}
|
||
|
Thread.Sleep(100);
|
||
|
}
|
||
|
}
|
||
|
PrintInfo("GuiDaoJiRet SendDataRead(byte[] btData) is end false");
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 向唐山客户端发数据
|
||
|
/// </summary>
|
||
|
/// <param name="btData"></param>
|
||
|
/// <returns></returns>
|
||
|
public static GuiDaoJiRet SendDataAlarm(byte[] btData)
|
||
|
{
|
||
|
PrintInfo("GuiDaoJiRet SendDataAlarm(byte[] btData) is begin");
|
||
|
if (dict.Any(kvp => kvp.Key == iClientPort))
|
||
|
{
|
||
|
dict[iClientPort].Send(btData);
|
||
|
uint time = 50;
|
||
|
while (time-- != 0)
|
||
|
{
|
||
|
PrintInfo("time:", time.ToString());
|
||
|
PrintInfo("sSetState:", "alarm");
|
||
|
if (alarmRet != null && alarmRet.action == "alarm")
|
||
|
{
|
||
|
GuiDaoJiRet sData = alarmRet;
|
||
|
PrintInfo("GuiDaoJiRet SendDataAlarm(byte[] btData) is end true. ", alarmRet.uid + ":" + alarmRet.result);
|
||
|
return sData;
|
||
|
}
|
||
|
Thread.Sleep(100);
|
||
|
}
|
||
|
}
|
||
|
PrintInfo("GuiDaoJiRet SendDataAlarm(byte[] btData) is end false");
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public static void Close()
|
||
|
{
|
||
|
socket_commu.Close();
|
||
|
Console.WriteLine("关闭成功");
|
||
|
}
|
||
|
|
||
|
|
||
|
static byte[] FZ(ushort val)
|
||
|
{
|
||
|
byte[] bytes = BitConverter.GetBytes(val);
|
||
|
byte aa = bytes[0];
|
||
|
bytes[0] = bytes[1];
|
||
|
bytes[1] = aa;
|
||
|
return bytes;
|
||
|
}
|
||
|
|
||
|
static byte[] FZ(byte[] bytes)
|
||
|
{
|
||
|
byte aa = bytes[0];
|
||
|
bytes[0] = bytes[1];
|
||
|
bytes[1] = aa;
|
||
|
return bytes;
|
||
|
}
|
||
|
|
||
|
public static void PrintInfo(string funcName, string funcOper = null)
|
||
|
{
|
||
|
//记录时间
|
||
|
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
//获取行号
|
||
|
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
|
||
|
int i = st.GetFrame(0).GetFileLineNumber();
|
||
|
//组织打印信息
|
||
|
string sFileInfo = "[" + i + "]:" + time + " " + funcName + " " + funcOper + "\n";
|
||
|
|
||
|
//创建日志文件夹.
|
||
|
string path = Environment.CurrentDirectory + "/Log";
|
||
|
string txtName = DateTime.Now.ToString("yyyy_MM_dd") + ".txt";
|
||
|
try
|
||
|
{
|
||
|
// 文件夹不存在则创建
|
||
|
if (!Directory.Exists(path))
|
||
|
{
|
||
|
Directory.CreateDirectory(path);
|
||
|
}
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Console.WriteLine("The process failed: {0}", e.ToString());
|
||
|
}
|
||
|
finally { }
|
||
|
|
||
|
try
|
||
|
{
|
||
|
//向文本中输入日志
|
||
|
FileStream fileLog = new FileStream(path + @"\" + txtName + "", FileMode.Append, FileAccess.Write, FileShare.Read);
|
||
|
StreamWriter writeFileLog = new StreamWriter(fileLog);
|
||
|
writeFileLog.Write(sFileInfo);
|
||
|
//清空缓冲区
|
||
|
writeFileLog.Flush();
|
||
|
//关闭流
|
||
|
writeFileLog.Close();
|
||
|
fileLog.Close();
|
||
|
//释放流
|
||
|
fileLog.Dispose();
|
||
|
|
||
|
//向输出流打印日志
|
||
|
System.Diagnostics.Debugger.Log(0, null, sFileInfo);
|
||
|
}
|
||
|
catch { }
|
||
|
}
|
||
|
|
||
|
public static void PrintInt(string funcName, int[] funcOper = null)
|
||
|
{
|
||
|
//记录时间
|
||
|
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
//获取行号
|
||
|
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
|
||
|
int i = st.GetFrame(0).GetFileLineNumber();
|
||
|
//组织打印信息
|
||
|
string sFileInfo = "[" + i + "]:" + time + " " + funcName + " ";
|
||
|
if (funcOper != null)
|
||
|
{
|
||
|
for (int j = 0; j < funcOper.Length; j++)
|
||
|
{
|
||
|
sFileInfo += funcOper[j].ToString() + " ";
|
||
|
}
|
||
|
sFileInfo += "\n";
|
||
|
}
|
||
|
|
||
|
//创建日志文件夹.
|
||
|
string path = Environment.CurrentDirectory + "/Log";
|
||
|
string txtName = DateTime.Now.ToString("yyyy_MM_dd") + ".txt";
|
||
|
try
|
||
|
{
|
||
|
// 文件夹不存在则创建
|
||
|
if (!Directory.Exists(path))
|
||
|
{
|
||
|
Directory.CreateDirectory(path);
|
||
|
}
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Console.WriteLine("The process failed: {0}", e.ToString());
|
||
|
}
|
||
|
finally { }
|
||
|
|
||
|
try
|
||
|
{
|
||
|
//向文本中输入日志
|
||
|
FileStream fileLog = new FileStream(path + @"\" + txtName + "", FileMode.Append, FileAccess.Write, FileShare.Read);
|
||
|
StreamWriter writeFileLog = new StreamWriter(fileLog);
|
||
|
writeFileLog.Write(sFileInfo);
|
||
|
//清空缓冲区
|
||
|
writeFileLog.Flush();
|
||
|
//关闭流
|
||
|
writeFileLog.Close();
|
||
|
fileLog.Close();
|
||
|
//释放流
|
||
|
fileLog.Dispose();
|
||
|
|
||
|
//向输出流打印日志
|
||
|
System.Diagnostics.Debugger.Log(0, null, sFileInfo);
|
||
|
}
|
||
|
catch { }
|
||
|
}
|
||
|
|
||
|
private static void DeleteValue(int iPort)
|
||
|
{
|
||
|
//删除字典组中的socket
|
||
|
if (dict.Any(kvp => kvp.Key == iPort))
|
||
|
{
|
||
|
dict[iPort].Shutdown(SocketShutdown.Both);
|
||
|
dict[iPort].Close();
|
||
|
dict[iPort].Dispose();
|
||
|
dict[iPort] = null;
|
||
|
dict.Remove(iPort);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void PrintByte(string funcName, byte[] data)
|
||
|
{
|
||
|
string sData = BitConverter.ToString(data).Replace('-', ' ');
|
||
|
//记录时间
|
||
|
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
//获取行号
|
||
|
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
|
||
|
int i = st.GetFrame(0).GetFileLineNumber();
|
||
|
//组织打印信息
|
||
|
string sFileInfo = "[" + i + "]:" + time + " " + funcName + " " + sData + "\r\n";
|
||
|
|
||
|
//创建日志文件夹.
|
||
|
string path = Environment.CurrentDirectory + "/Log";
|
||
|
string txtName = DateTime.Now.ToString("yyyy_MM_dd") + ".txt";
|
||
|
try
|
||
|
{
|
||
|
// 文件夹不存在则创建
|
||
|
if (!Directory.Exists(path))
|
||
|
{
|
||
|
Directory.CreateDirectory(path);
|
||
|
}
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Console.WriteLine("The process failed: {0}", e.ToString());
|
||
|
}
|
||
|
finally { }
|
||
|
|
||
|
try
|
||
|
{
|
||
|
//向文本中输入日志
|
||
|
FileStream fileLog = new FileStream(path + @"\" + txtName + "", FileMode.Append, FileAccess.Write, FileShare.Read);
|
||
|
StreamWriter writeFileLog = new StreamWriter(fileLog);
|
||
|
writeFileLog.Write(sFileInfo);
|
||
|
//清空缓冲区
|
||
|
writeFileLog.Flush();
|
||
|
//关闭流
|
||
|
writeFileLog.Close();
|
||
|
fileLog.Close();
|
||
|
//释放流
|
||
|
fileLog.Dispose();
|
||
|
|
||
|
//向输出流打印日志
|
||
|
System.Diagnostics.Debugger.Log(0, null, sFileInfo);
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Console.WriteLine("The process failed: {0}", e.ToString());
|
||
|
//MessageBox.Show(e.ToString());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|