using HH.WCS.Hexafluo;
|
using HH.WCS.Hexafluo.device;
|
using HH.WCS.HGXCL.util;
|
using HH.WCS.SJML.Dto;
|
using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Net.Sockets;
|
|
namespace HH.WCS.JunzhouNongfu.device
|
{
|
internal class TcpClient
|
{
|
private static Socket client = null;
|
private static string ip = "127.0.0.1";
|
private static int port = 1024;
|
|
static TcpClient()
|
{
|
PostTcpClient();
|
}
|
|
private static void PostTcpClient()
|
{
|
var trp = JsonHelper.GetValue("TcpClient");
|
var Op = JsonConvert.DeserializeObject<SJTransferDto>(trp);
|
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
client.ReceiveTimeout = 2000;
|
client.Connect(Op.ip, Op.port);
|
}
|
|
public static string SendHexOnce(string hex)
|
{
|
LogHelper.Info("发送给电梯信号:" + hex);
|
var res = string.Empty;
|
LogHelper.Info("发送给电梯信号 检测连接是否正常:" + client.Connected);
|
if (client.Connected)
|
{
|
|
client.ReceiveTimeout = 2000;
|
if (client.Connected)
|
{
|
client.Send(PlcHelper.Hex2Bytes(hex));
|
byte[] buffer = new byte[1024];
|
try
|
{
|
// client.Connected
|
var length = client.Receive(buffer, SocketFlags.None);
|
byte[] data = new byte[length];
|
Array.Copy(buffer, data, length);
|
res = BitConverter.ToString(data).Replace("-", " ");
|
LogHelper.Info("电梯回复信号:" + res);
|
//res = BitConverter.ToString(data);
|
//var tmp = res.Split(' ').ToList();
|
//var tmps = tmp.ToList();
|
//tmp.RemoveRange(0, 2);
|
//tmp.RemoveRange(tmp.Count - 2, tmp.Count - 1);
|
//List<string> Str = new List<string>();
|
//if (tmp.Count != int.Parse(tmps[2]))
|
//{
|
// throw new Exception("数据长度和寄存器对不上");
|
//}
|
//else
|
//{
|
// int a = 0;
|
// int b = 1;
|
// for (int i = 0; i < tmp.Count / 2; i++)
|
// {
|
// var fr = tmp[a] + tmp[b];
|
// Str.Add(Convert.ToInt32(fr, 10).ToString());
|
// a = a + 2;
|
// b = b + 2;
|
// }
|
//}
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Info("电梯报错:" + ex.Message);
|
LogHelper.Error(ex.Message, ex);
|
}
|
}
|
}
|
else
|
{
|
LogHelper.Info("发送给电梯信号 连接断开 重新连接");
|
try
|
{
|
client.Close();
|
PostTcpClient();
|
// client.Connect(ip, port);
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Info("电梯重连报错:" + ex.Message);
|
LogHelper.Error(ex.Message, ex);
|
}
|
}
|
return res;
|
}
|
|
|
public static List<int> SendHexOnceList(string hex)
|
{
|
LogHelper.Info("发送给电梯信号:" + hex);
|
var res = string.Empty;
|
List<int> Str = new List<int>();
|
LogHelper.Info("发送给电梯信号 检测连接是否正常:" + client.Connected);
|
if (client.Connected)
|
{
|
|
client.ReceiveTimeout = 2000;
|
if (client.Connected)
|
{
|
client.Send(PlcHelper.Hex2Bytes(hex));
|
byte[] buffer = new byte[1024];
|
try
|
{
|
// client.Connected
|
var length = client.Receive(buffer, SocketFlags.None);
|
byte[] data = new byte[length];
|
Array.Copy(buffer, data, length);
|
res = BitConverter.ToString(data).Replace("-", " ");
|
|
// res = BitConverter.ToString(data);
|
LogHelper.Info("电梯回复信号:" + res);
|
if (res != null)
|
{
|
res = res.Trim();
|
}
|
var tmp = res.Split(' ').ToList();
|
var tmps = tmp.ToList();
|
for (int i = 0; i < 3; i++)
|
{
|
tmp.RemoveAt(0);
|
|
}
|
for (int i = 0; i < 2; i++)
|
{
|
tmp.RemoveAt(tmp.Count - 1);
|
}
|
|
if (tmp.Count != int.Parse(tmps[2]))
|
{
|
throw new Exception("数据长度和寄存器对不上");
|
}
|
else
|
{
|
int a = 0;
|
int b = 1;
|
for (int i = 0; i < tmp.Count / 2; i++)
|
{
|
var fr = tmp[a] + tmp[b];
|
Str.Add(Convert.ToInt32(fr, 10));
|
a = a + 2;
|
b = b + 2;
|
}
|
}
|
LogHelper.Info("回复信号加工:" + JsonConvert.SerializeObject(Str));
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Info("电梯报错:" + ex.Message);
|
LogHelper.Error(ex.Message, ex);
|
}
|
}
|
}
|
else
|
{
|
LogHelper.Info("发送给电梯信号 连接断开 重新连接");
|
try
|
{
|
client.Close();
|
PostTcpClient();
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Info("电梯重连报错:" + ex.Message);
|
LogHelper.Error(ex.Message, ex);
|
}
|
}
|
return Str;
|
}
|
|
|
/// <summary>
|
/// 读保持寄存器
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="qty"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
/// <returns></returns>
|
internal int[] ReadInputRegistersRtu(int address, int qty, string ip, int port = 502)
|
{
|
List<int> res = new List<int>();
|
var hex = $"0103{address.ToString("X4")}{qty.ToString("X4")}";
|
hex = hex + BitConverter.ToString(PlcHelper.CRC16LH(PlcHelper.Hex2Bytes(hex))).Replace("-", "").Replace(" ", "");
|
//var data = SendHexOnce(ip, port, hex);
|
var data = SendHexOnce(hex);
|
if (!string.IsNullOrEmpty(data))
|
{
|
if (PlcHelper.CheckCRC(data))
|
{
|
var lenght = Convert.ToInt16(data.Substring(4, 2), 16) / 2;
|
for (int i = 0; i < lenght; i++)
|
{
|
res[i] = Convert.ToInt16(data.Substring(6 + 4 * 1, 4), 16);
|
}
|
}
|
}
|
return res.ToArray();
|
}
|
/// <summary>
|
/// 写单个寄存器
|
/// </summary>
|
/// <param name="address"></param>
|
/// <param name="value"></param>
|
/// <param name="ip"></param>
|
/// <param name="port"></param>
|
internal bool WriteSingleRegisterRtu(int address, int value, string ip, int port = 502)
|
{
|
var res = false;
|
var hex = $"0106{address.ToString("X4")}{value.ToString("X4")}";
|
hex = hex + BitConverter.ToString(PlcHelper.CRC16LH(PlcHelper.Hex2Bytes(hex))).Replace("-", "").Replace(" ", "");
|
//var data = SendHexOnce(ip, port, hex);
|
var data = SendHexOnce(hex);
|
if (!string.IsNullOrEmpty(data))
|
{
|
res = true;
|
}
|
return res;
|
}
|
}
|
|
}
|