alielie
2026-06-04 771a0e7b6b1276087515bbe94de28358cb92963b
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
using HslCommunication.ModBus;
 
namespace Plugin.DODriverControl.Api
{
    public class ModBusTcpController : IDIOController
    {
        public string Ip { get; set; }
        public int Port { get; set; }
 
        private ModbusTcpNet client;
 
        public bool Start(out string errMsg)
        {
            errMsg = string.Empty;
            client = new ModbusTcpNet(Ip, Port);
            var res = client.ConnectServer();
            errMsg = res.Message;
            return res.IsSuccess;
        }
 
        public bool ReadDIBool(string address, out bool res, out string msg)
        {
            var result = client?.ReadBool($"x=2;{address}");
            msg = result?.Message;
            res = result?.Content == true;
            return result?.IsSuccess == true;
        }
 
        public bool ReadDOBool(string address, out bool res, out string msg)
        {
            var result = client?.ReadBool(address);
            msg = result?.Message;
            res = result?.Content == true;
            return result?.IsSuccess == true;
        }
 
        public bool WriteBool(string address, bool value, out string msg)
        {
            msg = string.Empty;
            var res = client?.Write(address, value);
            msg = res.Message;
            return res?.IsSuccess == true;
        }
 
        public bool Close()
        {
            return client?.ConnectClose()?.IsSuccess == true;
 
        }
        public void Dispose()
        {
            client?.Dispose();
        }
 
        public bool ReadDIDouble(string address, out double? res, out string msg)
        {
            var result = client?.ReadDouble($"x=2;{address}");
            msg = result?.Message;
            res = result?.Content;
            return result?.IsSuccess == true;
        }
    }
}