schangxiang@126.com
2025-07-23 ba449717184ae09590aaead8a7240103b26cec5e
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using S7.Net;
using logtxtWrite;
using iWareCommon.Utils;
 
using HslCommunication.Profinet.Siemens;
using HslCommunication;
 
using iWareModel;
using HslCommunication.Profinet.AllenBradley;
using iWareCommon;
namespace iWareSda
{
    /// <summary>
    /// 罗克韦尔PLC (Allen-Bradley)
    /// </summary>
    public class AB_HslCommunicationService : PLCService
    {
        private AllenBradleyNet abTcpNet = null;
        private readonly string _ip;
        private readonly int _port;
        public AB_HslCommunicationService(HslCommunicationParam hslcpParam)
        {
            _ip = hslcpParam.ip;
            _port = hslcpParam.port;
 
            _LogType = LogType.HslCommunicationService;
        }
        /// <summary>
        /// 打开S7连接
        /// </summary>
        /// <returns></returns>
        public override MessageModel OpenService()
        {
            MessageModel fre = new MessageModel();
            try
            {
                //创建连接对象
                abTcpNet = new AllenBradleyNet();
                abTcpNet.IpAddress = _ip;
                if (_port > 0) abTcpNet.Port = Convert.ToInt32(_port);
 
                //不记录日志!!!!否则文件太大导致问题!
                //abTcpNet.LogNet = new HslCommunication.LogNet.LogNetSingle("AB_HslCommunicationService_logs.txt");
 
                OperateResult operateResult = abTcpNet.ConnectServer();
                if (operateResult.IsSuccess)
                {
                    base.IsConnected = true;
                    fre.result = true;
                    return fre;
                }
                base.IsConnected = false;
                fre.result = false;
                fre.resMsg = "打开失败," + operateResult.Message;
                return fre;
            }
            catch (Exception ex)
            {
                base.IsConnected = false;
                Log4NetHelper.WriteErrorLog(_LogType, "初始化AB PLC 出现异常", ex);
                throw ex;
            }
        }
 
        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public override MessageModel WriteValuePoint(string dbNumber, string offset, object value, Object proObj)
        {
            MessageModel fre = new MessageModel();
            OperateResult operateResult = null;
            Type proObjType = proObj.GetType();
            var address = dbNumber + offset;
            try
            {
                if (proObjType == typeof(bool))
                {//布尔类型
                    bool myData = Convert.ToBoolean(value);
                    operateResult = abTcpNet.Write(address, myData);
                }
                //浮点
                else if (proObjType == typeof(double) || proObjType == typeof(float))
                {
                    double MyData = Convert.ToDouble(value);
                    operateResult = abTcpNet.Write(address, MyData);
 
                }
                //整数
                else if (proObjType == typeof(Int16) || proObjType == typeof(short))
                {
                    short MyData = Convert.ToInt16(value);
                    operateResult = abTcpNet.Write(address, MyData.ConvertToUshort());
                }
                //双整数
                else if (proObjType == typeof(Int32))
                {
                    int myData = Convert.ToInt32(value);
                    operateResult = abTcpNet.Write(address, myData);
                }
                //Char
                else if (proObjType == typeof(Char))
                {//char要转换成int类型,写给PLC
                    int myData = Convert.ToInt32(value);
                    byte b = (byte)myData;
                    operateResult = abTcpNet.Write(address, b);
                }
                else
                {
                    operateResult = abTcpNet.Write(address, value.ToString());
                }
                if (!operateResult.IsSuccess)
                {
                    Log4NetHelper.WriteErrorLog(_LogType, String.Format("写入出现异常:" + operateResult.Message + ",地址:{0},值:{1}", address, value.ToString()), null);
                    fre.result = false;
                    fre.resMsg = "写入失败:" + operateResult.Message;
                }
                else
                {
                    fre.result = true;
                    Log4NetHelper.WriteInfoLog(_LogType, String.Format("写入成功,写入地址:{0},值:{1}", address, value.ToString()));
                }
            }
            catch (Exception ex)
            {
                Log4NetHelper.WriteErrorLog(_LogType, String.Format("写入出现异常:" + ex.Message + ",地址:{0},值:{1}", address, value.ToString()), ex);
                fre.result = false;
                fre.resMsg = "写入失败:" + ex.Message;
            }
            return fre;
        }
 
        /// <summary>
        /// 读取
        /// </summary>
        /// <param name="dbNumber">DB块名</param>
        /// <param name="offset">偏移量</param>
        /// <param name="type"></param>
        /// <returns></returns>
        public override object ReadValuePoint(string dbNumber, string offset, Type type = default(Type))
        {
            var address = dbNumber + offset;
            try
            {
                if (type == typeof(bool))
                {//布尔要转换成1和0写入
                    bool MyPlcData = abTcpNet.ReadBool(address).Content;
                    return MyPlcData;
                }
                //浮点
                else if (type == typeof(double) || type == typeof(float))
                {
                    var MyPlcData = abTcpNet.ReadFloat(address).Content;
                    return MyPlcData;
 
                }
                //整数
                else if (type == typeof(short))
                {
                    short MyPlcData = abTcpNet.ReadInt16(address).Content;
                    return MyPlcData;
                }
                //双整数
                else if (type == typeof(Int32))
                {
                    int MyPlcData = abTcpNet.ReadInt32(address).Content;
                    return MyPlcData;
                }
                //Char
                else if (type == typeof(Char))
                {//char要转换成int类型,写给PLC
                    char MyPlcData = (char)(abTcpNet.ReadByte(address).Content);
                    return MyPlcData.ToString();
                }
                else
                {
                    var MyPlcData = abTcpNet.ReadString(address, 5000).Content.ToString();
                    return MyPlcData.ToString();
                }
            }
            catch (Exception ex)
            {
                Log4NetHelper.WriteErrorLog(_LogType, String.Format("读取出现异常:" + ex.Message + ",地址:{0}", address), ex);
                throw;
            }
        }
 
 
        public override void Close()
        {
            if (abTcpNet != null)
                abTcpNet.ConnectClose();
        }
    }
}