lss
2025-05-30 38eff4fc0100131b180ffa872009b502629743f5
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
using HH.WCS.Langchao;
using HH.WCS.Langchao.device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
 
namespace HH.WCS.Danjiangkou.device
{
    internal class TcpClient
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ip">127.0.0.1</param>
        /// <param name="port">8888</param>
        /// <param name="hex">01 02 00 00 00 0C 78 0F</param>
        /// <returns></returns>
        private static string SendHexOnce(string ip, int port, string hex) {
            var res = string.Empty;
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client.Connect(ip, port);
            client.ReceiveTimeout = 2000;
            if (client.Connected) {
                client.Send(PlcHelper.Hex2Bytes(hex));
                byte[] buffer = new byte[1024];
                try {
                    var length = client.Receive(buffer, SocketFlags.None);
                    byte[] data = new byte[length];
                    Array.Copy(buffer, data, length);
                    res = BitConverter.ToString(data).Replace("-", "");
                }
                catch (Exception ex) {
                    LogHelper.Error(ex.Message, ex);
                }
                client.Disconnect(true);
                client.Dispose();
            }
            client = null;
            return res;
        }
    }
}