Tjiny
2025-06-04 9067ccda40f7656ac352711727ce5a8b70382e16
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using System;
using HH.WCS.Mobox3.Template.Entity.Dto;
using HH.WCS.Mobox3.Template.Util.Helper;
using S7.Net;
 
namespace HH.WCS.Mobox3.Template.Util.EquipmentCommunication
{
    /// <summary>
    /// 西门子S7线体服务帮助类
    /// </summary>
    public class S7Helper
    {
        /// <summary>
        /// 连接plc
        /// </summary>
        /// <param name="plc"></param>
        public static void Link(Plc plc)
        {
            try
            {
                //if (!plc.IsConnected)
                //{
                plc.Close();
                plc.Open();
                if (plc.IsConnected) LogHelper.Info($"已链接到plc{plc.IP}");
                else LogHelper.Info($"plc{plc.IP}链接失败");
                //}
            }
            catch (Exception ex)
            {
                LogHelper.Info($"plc{plc.IP}链接失败,err={ex.Message}");
            }
        }
 
        /// <summary>
        /// 根据参数获取不同的plc连接
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        private static Plc GetPlc(string address)
        {
            // 获取指定plc
            Plc plc = null;
 
            plc = new Plc(CpuType.S71500, address, 0, 1);
 
            Link(plc);
 
            return plc;
        }
 
        /// <summary>
        /// 读取设备信息 - 输送线(数值类型)
        /// </summary>
        /// <param name="startAddr">偏移量起点</param>
        /// <param name="conveyorlinesInfoDto">设备信息</param>
        /// <param name="dbNo">db块</param>
        /// <param name="dataType">数据类型</param>
        /// <returns></returns>
        public static int ReadPipelinePlcForInt(int startAddr, ConveyorlinesInfoDto conveyorlinesInfoDto, int dbNo,
            string dataType)
        {
            int data = 0;
 
            var plc = GetPlc(conveyorlinesInfoDto.address);
 
            if (plc != null)
            {
                try
                {
                    if (plc.IsConnected)
                    {
                        if (dataType == "Int")
                            // 读取plc信息
                            data = Convert.ToInt32(plc.Read(DataType.DataBlock, dbNo, startAddr, VarType.Int, 1));
                        else if (dataType == "DInt")
                            // 读取plc信息
                            data = Convert.ToInt32(plc.Read(DataType.DataBlock, dbNo, startAddr, VarType.DInt, 1));
                    }
                    else
                    {
                        data = -1;
                        LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}连接失败", "PLC读写");
                    }
                }
                catch (Exception e)
                {
                    data = -1;
                    LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}异常报错:" + e, "PLC读写");
                }
            }
            else
            {
                data = -1;
                LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}的plc配置失败", "PLC读写");
            }
 
            return data;
        }
 
        /// <summary>
        /// 读取设备信息 - 输送线(字符串类型)
        /// </summary>
        /// <param name="startAddr">偏移量起点</param>
        /// <param name="conveyorlinesInfoDto">设备信息</param>
        /// <param name="dbNo">db块</param>
        /// <returns></returns>
        public static string ReadPipelinePlcForString(int startAddr, ConveyorlinesInfoDto conveyorlinesInfoDto,
            int dbNo)
        {
            string data = "";
 
            var plc = GetPlc(conveyorlinesInfoDto.address);
 
            if (plc != null)
            {
                try
                {
                    if (plc.IsConnected)
                    {
                        data = Convert.ToString(plc.Read(DataType.DataBlock, dbNo, startAddr, VarType.String, 24));
                    }
                    else
                    {
                        LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}连接失败", "PLC读写");
                    }
                }
                catch (Exception e)
                {
                    LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}异常报错:" + e, "PLC读写");
                }
            }
            else
            {
                LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}的plc配置失败", "PLC读写");
            }
 
            return data;
        }
 
        /// <summary>
        /// 往设备写任务 - 输送线
        /// </summary>
        /// <param name="conveyorlinesInfoDto"></param>
        /// <param name="lineTask"></param>
        /// <param name="startAddr"></param>
        /// <param name="dbNo"></param>
        /// <returns></returns>
        public static bool SendTaskPipelinePlc(ConveyorlinesInfoDto conveyorlinesInfoDto, LineTaskDto lineTask,
            int startAddr, int dbNo)
        {
            var result = false;
        
            var plc = GetPlc(conveyorlinesInfoDto.address);
        
            if (plc != null)
            {
                try
                {
                    if (plc.IsConnected)
                    {
                        // 写任务
                        // 任务号
                        plc.Write(DataType.DataBlock, dbNo, startAddr, Convert.ToInt32(lineTask.Task_No));
                        
                        // 条形码
                        plc.Write(DataType.DataBlock, dbNo, startAddr + 4, lineTask.Pallet_No);
                        
                        // 写起点
                        plc.Write(DataType.DataBlock, dbNo, startAddr + 30,
                            Convert.ToInt16(lineTask.Source_Station));
                        
                        // 写终点
                        plc.Write(DataType.DataBlock, dbNo, startAddr + 32,
                            Convert.ToInt16(lineTask.Destination_Station));
        
                        LogHelper.Info(
                            $"线体编号{conveyorlinesInfoDto.devicesign},写入数据【任务号:{lineTask.Task_No}、条形码{lineTask.Pallet_No}、起点:{lineTask.Source_Station}、终点:{lineTask.Destination_Station}】",
                            "PLC读写");
        
                        var taskNo =
                            Convert.ToInt32(plc.Read(DataType.DataBlock, dbNo, startAddr, VarType.DInt, 1));
                        
                        var cntrCode = Convert.ToString(plc.Read(DataType.DataBlock, dbNo, startAddr + 4 ,VarType.String, 24));
                        
                        var sourceStation =
                            Convert.ToInt32(plc.Read(DataType.DataBlock, dbNo, startAddr + 30, VarType.Int, 1));
                        
                        var destinationStation =
                            Convert.ToInt32(plc.Read(DataType.DataBlock, dbNo, startAddr + 32, VarType.Int, 1));
        
                        result = lineTask.Task_No == taskNo;
        
                        LogHelper.Info(
                            $"线体编号{conveyorlinesInfoDto.devicesign},读取数据【任务号:{taskNo}、托盘号:{cntrCode}、起点:{sourceStation}、终点:{destinationStation}】,bool返回为{result}",
                            "PLC读写");
                    }
                    else
                    {
                        LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}连接失败", "PLC读写");
                    }
                }
                catch (Exception e)
                {
                    LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}异常报错:" + e, "PLC读写");
                }
            }
            else
            {
                LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}的plc配置失败", "PLC读写");
            }
        
            return result;
        }
 
        // /// <summary>
        // /// 往设备写数据 - 输送线
        // /// </summary>
        // /// <param name="conveyorlinesInfoDto"></param>
        // /// <param name="pipeLineDto"></param>
        // /// <param name="startAddr"></param>
        // /// <param name="dbNo"></param>
        // /// <returns></returns>
        // public static bool SendTaskPipelinePlcTwo(ConveyorlinesInfoDto conveyorlinesInfoDto, PipeLineDto pipeLineDto,
        //     int startAddr, int dbNo)
        // {
        //     var result = false;
        //
        //     var plc = GetPlc(conveyorlinesInfoDto.address);
        //
        //     if (plc != null)
        //     {
        //         try
        //         {
        //             if (plc.IsConnected)
        //             {
        //                 // 写任务
        //                 plc.Write(DataType.DataBlock, dbNo, startAddr + 14, Convert.ToInt32(pipeLineDto.WCS_TaskNo));
        //                 plc.Write(DataType.DataBlock, dbNo, startAddr + 18,
        //                     Convert.ToInt16(pipeLineDto.WCS_Destination));
        //
        //                 LogHelper.Info(
        //                     $"线体编号{conveyorlinesInfoDto.devicesign},写入数据【任务号:{pipeLineDto.WCS_TaskNo}、目的地:{pipeLineDto.WCS_Destination}】",
        //                     "PLC读写");
        //
        //                 var taskNo =
        //                     Convert.ToInt32(plc.Read(DataType.DataBlock, dbNo, startAddr + 14, VarType.DInt, 1));
        //                 var destination =
        //                     Convert.ToInt32(plc.Read(DataType.DataBlock, dbNo, startAddr + 18, VarType.Int, 1));
        //
        //                 result = pipeLineDto.WCS_TaskNo == taskNo;
        //
        //                 LogHelper.Info(
        //                     $"线体编号{conveyorlinesInfoDto.devicesign},读取数据【任务号:{taskNo}、目的地:{destination}】,bool返回为{result}",
        //                     "PLC读写");
        //
        //             }
        //             else
        //             {
        //                 LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}连接失败", "PLC读写");
        //             }
        //         }
        //         catch (Exception e)
        //         {
        //             LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}异常报错:" + e, "PLC读写");
        //         }
        //     }
        //     else
        //     {
        //         LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}的plc配置失败", "PLC读写");
        //     }
        //
        //     return result;
        // }
 
        /// <summary>
        /// 往设备写数据 - 输送线(单条数据)
        /// </summary>
        /// <param name="conveyorlinesInfoDto"></param>
        /// <param name="data"></param>
        /// <param name="startAddr"></param>
        /// <param name="dbNo"></param>
        /// <returns></returns>
        public static bool WritePipelinePlcFromInt(ConveyorlinesInfoDto conveyorlinesInfoDto, int data, int startAddr,
            int dbNo)
        {
            var result = false;
 
            var plc = GetPlc(conveyorlinesInfoDto.address);
 
            if (plc != null)
            {
                try
                {
                    if (plc.IsConnected)
                    {
                        // 写任务
                        plc.Write(DataType.DataBlock, dbNo, startAddr,
                            Convert.ToInt16(data));
 
                        LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign},往偏移量{startAddr}写入数据{data}", "PLC读写");
 
                        var readData = Convert.ToInt32(plc.Read(DataType.DataBlock, dbNo, startAddr, VarType.Int, 1));
 
                        result = data == readData;
 
                        LogHelper.Info(
                            $"线体编号{conveyorlinesInfoDto.devicesign},往偏移量{startAddr}读取数据{readData},bool返回为{result}",
                            "PLC读写");
                    }
                    else
                    {
                        LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}连接失败", "PLC读写");
                    }
                }
                catch (Exception e)
                {
                    LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}异常报错:" + e, "PLC读写");
                }
            }
            else
            {
                LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}的plc配置失败", "PLC读写");
            }
 
            return result;
        }
 
        // /// <summary>
        // /// 清理管道 - 输送线
        // /// </summary>
        // /// <param name="conveyorlinesInfoDto"></param>
        // /// <param name="startAddr"></param>
        // /// <returns></returns>
        // public static bool clearPipeLinePlc(ConveyorlinesInfoDto conveyorlinesInfoDto, int startAddr)
        // {
        //     var result = false;
        //
        //     var plc = GetPlc(conveyorlinesInfoDto.address);
        //
        //     if (plc != null)
        //     {
        //         try
        //         {
        //             if (plc.IsConnected)
        //             {
        //                 result = plc.WriteBytes(DataType.DataBlock, 500, startAddr, new byte[42]) == ErrorCode.NoError;
        //
        //                 var readBytes = plc.ReadBytes(DataType.DataBlock, 1000, startAddr + 6, 42);
        //
        //                 LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}管道清理,读取到的数据长度为{readBytes.Length}",
        //                     "PLC读写");
        //             }
        //             else
        //             {
        //                 LogHelper.Info($"线体编号{conveyorlinesInfoDto.devicesign}连接失败", "PLC读写");
        //             }
        //         }
        //         catch (Exception e)
        //         {
        //             LogHelper.Info($"堆垛机编号{conveyorlinesInfoDto.devicesign}异常报错:" + e, "PLC读写");
        //         }
        //     }
        //     else
        //     {
        //         LogHelper.Info($"堆垛机编号{conveyorlinesInfoDto.devicesign}的plc配置失败", "PLC读写");
        //     }
        //
        //     return result;
        // }
 
    }
}