2
schangxiang@126.com
2024-10-01 ffa9dc9c2d28c0b8d88b26d483ba9667e316db01
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using S7.Net;
using logtxtWrite;
 
 
 
using iWareModel;
using iWareCommon.Utils;
namespace iWareSda
{
    public class SS7 : PLCService
    {
        private PlcS7 plcS7 = null;
        public SS7(SS7Param s7Param)
        {
            plcS7 = new PlcS7(s7Param.cpu, s7Param.ip, s7Param.rack, s7Param.slot);
            _LogType = LogType.SS7;
        }
        /// <summary>
        /// 打开S7连接
        /// </summary>
        /// <returns></returns>
        public override MessageModel OpenService()
        {
            MessageModel fre = new MessageModel();
            try
            {
                var code = plcS7.Open();
                if (code == ErrorCode.NoError)
                {
                    base.IsConnected = true;
                    fre.result = true;
                    return fre;
                }
                base.IsConnected = false;
                fre.result = false;
                fre.resMsg = "打开失败," + code.ToString();
                return fre;
            }
            catch (Exception ex)
            {
                base.IsConnected = false;
                Log4NetHelper.WriteErrorLog(_LogType, "初始化s7Init出现异常", ex);
                throw ex;
            }
        }
 
        /// <summary>
        /// S7读取
        /// </summary>
        /// <param name="dbNumber">DB块名</param>
        /// <param name="offset">偏移量</param>
        /// <param name="type"></param>
        /// <returns></returns>
        public override MessageModel WriteValuePoint(string dbNumber, string offset, object value, Object proObj)
        {
            MessageModel fre = new MessageModel();
            object objReuslt = new object();
            var address = GetAddress(dbNumber, offset, value.GetType());
            try
            {
                if (value.GetType() == typeof(bool))
                {//布尔要转换成1和0写入
                    int myData = Convert.ToInt32(value);
                    objReuslt = plcS7.Write(address, myData);
                }
                //浮点
                else if (value.GetType() == typeof(double) || value.GetType() == typeof(float))
                {
                    double MyData = Convert.ToDouble(value);
                    objReuslt = plcS7.Write(address, MyData.ConvertToUInt());
 
                }
                //整数
                else if (value.GetType() == typeof(Int32) || value.GetType() == typeof(short))
                {
                    short MyData = Convert.ToInt16(value);
                    objReuslt = plcS7.Write(address, MyData.ConvertToUshort());
                }
                //双整数
                else if (value.GetType() == typeof(Int32))
                {
                    int myData = Convert.ToInt32(value);
                    objReuslt = plcS7.Write(address, myData);
                }
                //Char
                else if (value.GetType() == typeof(Char))
                {//char要转换成int类型,写给PLC
                    int myData = Convert.ToInt32(value);
                    byte b = (byte)myData;
                    objReuslt = plcS7.Write(address, b);
                }
                else
                {
                    objReuslt = plcS7.Write(address, value);
                }
                ValidateReusltForWrite(objReuslt, address);
                Log4NetHelper.WriteInfoLog(LogType.SS7, String.Format("写入地址:{0},值:{1}", address, value.ToString()));
                fre.result = true;
                return fre;
            }
            catch (Exception ex)
            {
                Log4NetHelper.WriteErrorLog(LogType.SS7, String.Format("写入出现异常:" + ex.Message + ",地址:{0},值:{1}", address, value.ToString()), ex);
                fre.result = false;
                fre.resMsg = "写入异常:" + ex.Message;
                return fre;
            }
        }
 
        /// <summary>
        /// S7读取
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="type"></param>
        /// <returns></returns>
        public override object ReadValuePoint(string dbNumber, string offset, Type type)
        {
            var address = GetAddress(dbNumber, offset, type);
            object objReuslt = null;
            try
            {
                objReuslt = plcS7.Read(address);
                ValidateReusltForRead(objReuslt, address);
                if (type == typeof(Char))
                {//特别处理char类型
                    char _plcData = (char)Convert.ToInt32(objReuslt);
                    objReuslt = _plcData.ToString();
                }
                else if (type == typeof(double))
                {
                    objReuslt = ((uint)objReuslt).ConvertToDouble();
                }
                else if (type == typeof(bool))
                {
                    return Convert.ToBoolean(objReuslt);
                }
            }
            catch (Exception ex)
            {
                Log4NetHelper.WriteErrorLog(LogType.SS7, String.Format("读取出现异常:" + ex.Message + ",地址:{0}", address), ex);
                throw;
            }
            return objReuslt;
 
        }
 
        public override string GetAddress(string dbNumber, string offset, Type type = default(Type))
        {
            var address = "";
            if (type == typeof(bool))
            {//布尔要转换成1和0写入
                address = "DB" + dbNumber + "." + "DBX" + offset;
            }
            //浮点
            else if (type == typeof(double) || type == typeof(float))
            {
                address = "DB" + dbNumber + "." + "DBD" + offset;
            }
            //整数
            else if (type == typeof(Int32) || type == typeof(short))
            {
                address = "DB" + dbNumber + "." + "DBW" + offset;
            }
            //双整数
            else if (type == typeof(Int32))
            {
                address = "DB" + dbNumber + "." + "DBD" + offset;
            }
            //Char
            else if (type == typeof(Char))
            {//char要转换成int类型,写给PLC
                address = "DB" + dbNumber + "." + "DBB" + offset;
            }
            else
            {
                address = "DB" + dbNumber + "." + "DBW" + offset;
            }
            return address;
        }
 
        public override void Close()
        {
            if (plcS7 != null)
                plcS7.Close();
        }
 
        #region 私有方法
 
        /// <summary>
        /// 验证结果(读取)
        /// </summary>
        /// <param name="objReuslt"></param>
        private void ValidateReusltForRead(Object objReuslt, string address)
        {
            if (objReuslt.GetType() == typeof(ErrorCode))
            {
                ErrorCode errCode = (ErrorCode)objReuslt;
                if (errCode != ErrorCode.NoError)
                {
                    throw new Exception(String.Format("读取" + address + "出现异常:" + errCode.ToString()));
                }
            }
        }
 
        /// <summary>
        /// 验证结果(写入)
        /// </summary>
        /// <param name="objReuslt"></param>
        private void ValidateReusltForWrite(Object objReuslt, string address)
        {
            if (objReuslt.GetType() == typeof(ErrorCode))
            {
                ErrorCode errCode = (ErrorCode)objReuslt;
                if (errCode != ErrorCode.NoError)
                {
                    throw new Exception(String.Format("写入" + address + "出现异常:" + errCode.ToString()));
                }
            }
        }
 
        #endregion
    }
}