1
pulg
2025-05-27 bc3d7a828114cb9df154da869ec986cf6733a779
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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;
        }
    }
 
}