alielie
2026-06-15 f08871fe7d148d667f2f11f80f64de71a0d9504c
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
using HslCommunication;
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
 
namespace PLCHelper
{
    public class RestTcpClient : IDisposable
    {
        public string IP { get; private set; }
        public int Port { get; private set; }
 
        public int ReceiveTimeout { get; set; } = 5000;
 
        private TcpClient tcpClient;
        private Stream Stream;
        private BinaryReader reader;
        private BinaryWriter writer;
 
        public RestTcpClient(string ip, int port)
        {
            IP = ip;
            Port = port;
        }
 
        public void ConnectServer()
        {
            tcpClient = new TcpClient(IP, Port);
            tcpClient.ReceiveTimeout = ReceiveTimeout;
            Stream = tcpClient.GetStream();
            reader = new BinaryReader(Stream);
            writer = new BinaryWriter(Stream);
        }
 
        public OperateResult<bool> ReadBool(string data)
        {
            OperateResult<bool> res = new OperateResult<bool>();
            if (Request(data, out string response, out string errMsg))
            {
                res.Content = bool.Parse(response);
                res.IsSuccess = true;
            }
            else
            {
                res.Message = errMsg;
                res.IsSuccess = false;
            }
            return res;
        }
 
        public OperateResult Write(string data, bool val)
        {
            OperateResult res = new OperateResult();
            if (Request(data, out string response, out string errMsg))
            {
                res.IsSuccess = true;
            }
            else
            {
                res.Message = errMsg;
                res.IsSuccess = false;
            }
            return res;
        }
 
        public void Send(string data)
        {
            data = data + "\r";
            writer.Write(Encoding.UTF8.GetBytes(data));
            writer.Flush();
        }
 
        public bool Request(string data, out string response, out string errMsg)
        {
            lock (this)
            {
                errMsg = string.Empty;
                response = string.Empty;
                try
                {
                    Send(data);
                    response = Receive();
                    return true;
                }
                catch (Exception ex)
                {
                    errMsg = ex.Message;
                    return false;
                }
            }
        }
 
        private string Receive()
        {
            byte[] array = new byte[1024];
            int num = tcpClient.Client.Receive(array);
            if (num == 0)
            {
                throw new Exception("Closed");
            }
            var bytes = new byte[num];
            Array.Copy(array, bytes, num);
            string data = Encoding.UTF8.GetString(bytes);
            if (data.EndsWith("\r")) data = data.Substring(0, data.Length - 1);
            return data;
        }
 
        public void Dispose()
        {
            tcpClient.Close();
        }
    }
}