schangxiang@126.com
2025-09-19 0821aa23eabe557c0d9ef5dbe6989c68be35d1fe
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
using IWareCC.CacheInfo;
using IWareCC.Properties;
using IWareCommon.Enum.Srm;
using IWareCommon.Help;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace IWareCC.SRM.Entity
{
    public class SrmEntity
    {
        /// <summary>
        /// 堆垛机名称
        /// </summary>
        public string SrmName { get; set; }
        /// <summary>
        /// 设备编号
        /// </summary>
        public int DeviceId { get; set; }
        /// <summary>
        /// 根据心跳判断堆垛机是否在线
        /// </summary>
        public bool IsOnline { get; set; }
        /// <summary>
        /// 下线次数
        /// </summary>
        public int OffLineTimes { get; set; }
 
        /// <summary>
        /// 上一次心跳数
        /// </summary>
        public int LastHandShake { get; set; }
 
        /// <summary>
        /// 连接PLC读的对象
        /// </summary>
        public PlcS7 RS71500 { get; set; }
        /// <summary>
        /// 连接PLC写的对象
        /// </summary>
        public PlcS7 WS71500 { get; set; }
 
        #region 写入的堆垛机信息
 
        /// <summary>
        /// 心跳
        /// </summary>
        public int WHandShake { get; set; }
        /// <summary>
        /// 堆垛机编号
        /// </summary>
        public int WSrmNo { get; set; }
        /// <summary>
        /// 任务类型
        /// </summary>
        public int WCommand { get; set; }
        /// <summary>
        /// 急停
        /// </summary>
        public int WEmergencyStop { get; set; }
        /// <summary>
        /// 起始列
        /// </summary>
        public int WSourcePosX { get; set; }
        /// <summary>
        /// 起始层
        /// </summary>
        public int WSourcePosY { get; set; }
        /// <summary>
        /// 起始排
        /// </summary>
        public int WSourcePosZ { get; set; }
        /// <summary>
        /// 目标列
        /// </summary>
        public int WDestinationPosX { get; set; }
        /// <summary>
        /// 目标层
        /// </summary>
        public int WDestinationPosY { get; set; }
        /// <summary>
        /// 目标排
        /// </summary>
        public int WDestinationPosZ { get; set; }
        /// <summary>
        ///任务号
        /// </summary>
        public int WTaskNo { get; set; }
        /// <summary>
        /// 任务完成确认
        /// </summary>
        public bool WTaskFinishConfirm { get; set; }
        /// <summary>
        /// 解除报警
        /// </summary>
        public bool WCancleAlarm { get; set; }
 
        /// <summary>
        /// 确认任务已发送的信号
        /// </summary>
        public bool WStb { get; set; }
 
        /// <summary>
        /// 托盘类型
        /// </summary>
        public int WPalletType { get; set; }
 
        /// <summary>
        /// 货物高度
        /// </summary>
        public int WHigh { get; set; }
 
        #endregion
 
        #region 读取到的堆垛机信息
        /// <summary>
        /// 读取心跳
        /// </summary>
        public int RHandShake { get; set; }
        /// <summary>
        /// 设备编号
        /// </summary>
        public int RSrmNo { get; set; }
        /// <summary>
        /// 是否有报警
        /// </summary>
        public bool RAlarm { get; set; }
 
        /// <summary>
        /// 载货台是否有货
        /// </summary>
        public bool RLiftFull { get; set; }
        /// <summary>
        /// 设备模式
        /// </summary>
        public int RMode { get; set; }
 
        /// <summary>
        /// 当前列
        /// </summary>
        public int RPosX { get; set; }
        /// <summary>
        /// 当前层
        /// </summary>
        public int RPosY { get; set; }
        /// <summary>
        /// 当前排
        /// </summary>
        public int RPosZ { get; set; }
        /// <summary>
        /// 当前列坐标
        /// </summary>
        public int RPosXmm { get; set; }
        /// <summary>
        /// 当前层坐标
        /// </summary>
        public int RPosYmm { get; set; }
        /// <summary>
        /// 当前排坐标
        /// </summary>
        public int RPosZmm { get; set; }
        /// <summary>
        /// 取货完成
        /// </summary>
        public bool RPickFinish { get; set; }
        /// <summary>
        /// 放货完成
        /// </summary>
        public bool RDeliveryFinish { get; set; }
        /// <summary>
        /// 任务完成
        /// </summary>
        public bool RTaskFinish { get; set; }
        /// <summary>
        /// 当前巷道
        /// </summary>
        public int RActualLane { get; set; }
        /// <summary>
        /// 任务状态
        /// </summary>
        public int RState { get; set; }
        /// <summary>
        /// 报警代码
        /// </summary>
        public int RAlarmCode { get; set; }
        /// <summary>
        /// 任务号
        /// </summary>
        public int RTaskNo { get; set; }
 
        /// <summary>
        /// 确认收到下发的任务信号
        /// </summary>
        public bool RAck { get; set; }
        #endregion
 
        public OpcReadItem OpcReadItems { get; set; }
 
        public OpcWriteItem OpcWriteItems { get; set; }
 
        public SrmEntity() { }
 
        public SrmEntity(string srmName, int deviceId, PlcS7 plc)
        {
            this.SrmName = srmName;
            this.DeviceId = deviceId;
            this.WS71500 = plc;
 
            OpcWriteItems = new OpcWriteItem
            {
                HandShake = "DB540.DBW0",
                SrmNo = "DB540.DBW2",
                TaskNo = "DB540.DBW34",
                TaskFinishConfirm = "DB540.DBW24",
                EmergencyStop = "DB540.DBW20",
                CancleAlarm = "DB540.DBW22",
                Command = "DB540.DBW18",
                SourcePosX = "DB540.DBW6",
                SourcePosY = "DB540.DBW8",
                SourcePosZ = "DB540.DBW10",
                DestinationPosX = "DB540.DBW12",
                DestinationPosY = "DB540.DBW14",
                DestinationPosZ = "DB540.DBW16",
                Stb = "DB540.DBW30",
                PalletType = "DB540.DBW4",
                High = "DB540.DBW26",
            };
            OpcReadItems = new OpcReadItem
            {
                HandShake = "DB541.DBW0",
                PickFinish = "DB541.DBW28",
                LiftFull = "DB541.DBW8",
                Mode = "DB541.DBW4",
                Alarm = "DB541.DBW6",
                TaskFinish = "DB541.DBW32",
                DeliveryFinish = "DB541.DBW30",
                ActualLane = "DB541.DBW38",
                SrmNo = "DB541.DBW2",
                AlarmCode = "DB541.DBW34",
                State = "DB541.DBW36",
                PosZ = "DB541.DBW10",
                PosX = "DB541.DBW12",
                PosY = "DB541.DBW14",
                PosXmm = "DB541.DBD16",
                PosYmm = "DB541.DBD20",
                PosZmm = "DB541.DBD24",
                TaskNo = "DB541.DBW40",
                Ack = "DB540.DBW32"
            };
        }
 
 
        public bool SendTask(int deviceId, int taskId, string sourcePlace, string toPlace, int type, int palltype,int heighttype,out string msg)
        {
            try
            {
                msg = "";
                if (!CanSendTask(deviceId, out msg))
                {
                    return false;
                }
                var sourceSplit = sourcePlace.Split('-');
                var toSplit = toPlace.Split('-');
      
                bool b1 = this.WriteValue(this.OpcWriteItems.SrmNo, deviceId);
                bool b2 = this.WriteValue(this.OpcWriteItems.TaskNo, taskId);
                bool b3 = this.WriteValue(this.OpcWriteItems.SourcePosY, int.Parse(sourceSplit[2]));
                bool b4 = this.WriteValue(this.OpcWriteItems.SourcePosX, int.Parse(sourceSplit[1]));
                bool b5 = this.WriteValue(this.OpcWriteItems.SourcePosZ, int.Parse(sourceSplit[0]));
                bool b6 = this.WriteValue(this.OpcWriteItems.DestinationPosY, int.Parse(toSplit[2]));
                bool b7 = this.WriteValue(this.OpcWriteItems.DestinationPosX, int.Parse(toSplit[1]));
                bool b8 = this.WriteValue(this.OpcWriteItems.DestinationPosZ, int.Parse(toSplit[0]));
                bool b10 = this.WriteValue(this.OpcWriteItems.Command, 1);
                bool b11 = this.WriteValue(this.OpcWriteItems.PalletType, palltype);//2代表大托盘1小托盘
                bool b12 = this.WriteValue(this.OpcWriteItems.High, heighttype);//2代表大托盘1小托盘
              
                if (!(b1 && b2 && b3 && b4 && b5 && b6 && b7 && b8  && b10&&b11&&b12))
                {
                    msg = string.Format("向堆垛机{0}发送任务{1}失败", SrmName, taskId);
                    return false;
                }
                bool b9 = this.WriteValue(this.OpcWriteItems.Stb, 1);
                if (b9)
                {
                    return true;
                }else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "SendTask", msg);
                return false;
            }
        }
        /// <summary>
        /// 移动任务
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="taskId"></param>
        /// <param name="toPlace"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool SendMove(int deviceId, int taskId, string sourcePlace, string toPlace, out string msg)
        {
            try
            {
                msg = "";
                if (!CanDoMove(deviceId, out msg))
                {
                    return false;
                }
                var toSplit = toPlace.Split('-');
                var sourSplit = sourcePlace.Split('-');
 
                bool b1 = this.WriteValue(this.OpcWriteItems.SrmNo, deviceId);
                bool b2 = this.WriteValue(this.OpcWriteItems.TaskNo, taskId);
                bool b3 = this.WriteValue(this.OpcWriteItems.SourcePosY, int.Parse(sourSplit[2]));
                bool b4 = this.WriteValue(this.OpcWriteItems.SourcePosX, int.Parse(sourSplit[1]));
                bool b5 = this.WriteValue(this.OpcWriteItems.SourcePosZ, int.Parse(sourSplit[0]));
                bool b6 = this.WriteValue(this.OpcWriteItems.DestinationPosY, int.Parse(toSplit[2]));
                bool b7 = this.WriteValue(this.OpcWriteItems.DestinationPosX, int.Parse(toSplit[1]));
                bool b8 = this.WriteValue(this.OpcWriteItems.DestinationPosZ, int.Parse(toSplit[0]));
                bool b9 = this.WriteValue(this.OpcWriteItems.Stb, 1);
                bool b10 = this.WriteValue(this.OpcWriteItems.Command, 1);
                bool b11 = this.WriteValue(this.OpcWriteItems.PalletType, 2);//2代表大托盘1小托盘
                bool b12 = this.WriteValue(this.OpcWriteItems.High, 1);//2代表大托盘1小托盘
                if (!(b1 && b2 && b3 && b4 && b5 && b6 && b7 && b8 && b9 && b10))
                {
                    msg = string.Format("向堆垛机{0}发送移动指令{1}失败", SrmName, taskId);
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "SendMove", msg);
                return false;
            }
        }
        /// <summary>
        /// 任务召回
        /// </summary>
        /// <param name="srmNo"></param>
        /// <param name="taskId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool SendCallBackTask(int deviceId, int taskId, out string msg)
        {
            try
            {
                msg = "";
 
                if (!IsOnline)
                {
                    msg = string.Format("堆垛机{0}不在线", this.SrmName);
                    return false;
                }
 
                if (RMode != (int)ESrmMode.自动模式)
                {
                    msg = string.Format("堆垛机{0}非自动模式", this.SrmName);
                    return false;
                }
 
                if (RAlarm)
                {
                    msg = string.Format("堆垛机{0}报警中", this.SrmName);
                    return false;
                }
 
                if (RLiftFull)
                {
                    msg = string.Format("堆垛机{0}叉齿有货", this.SrmName);
                    return false;
                }
 
                if (RState != (int)ESrmState.空闲)
                {
                    msg = string.Format("堆垛机{0}非空闲", this.SrmName);
                    return false;
                }
                bool b1 = this.WriteValue(this.OpcWriteItems.SrmNo, deviceId);
                bool b2 = this.WriteValue(this.OpcWriteItems.TaskNo, taskId);
                bool b3 = this.WriteValue(this.OpcWriteItems.Command, (int)ESrmCmd.召回);
                if (!(b1 && b2 && b3))
                {
                    msg = string.Format("向堆垛机{0}发送召回任务{1}失败", SrmName, taskId);
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "SendCallBackTask", msg);
                return false;
            }
        }
        /// <summary>
        /// 任务删除
        /// </summary>
        /// <param name="srmNo"></param>
        /// <param name="taskId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool SendDelTask(int deviceId, int taskId, out string msg)
        {
            try
            {
                msg = "";
 
                //if (!IsOnline)
                //{
                //    msg = string.Format("堆垛机{0}不在线", this.SrmName);
                //    return false;
                //}
 
                //if (RMode != (int)ESrmMode.自动模式)
                //{
                //    msg = string.Format("堆垛机{0}非自动模式", this.SrmName);
                //    return false;
                //}
 
                //if (RState != (int)ESrmState.空闲)
                //{
                //    msg = string.Format("堆垛机{0}非空闲", this.SrmName);
                //    return false;
                //}
 
                bool b1 = this.WriteValue(this.OpcWriteItems.SrmNo, deviceId);
                bool b2 = this.WriteValue(this.OpcWriteItems.TaskNo, taskId);
                bool b3 = this.WriteValue(this.OpcWriteItems.Command, (int)ESrmCmd.任务删除);
                if (!(b1 && b2 && b3))
                {
                    msg = string.Format("向堆垛机{0}发送召回任务{1}失败", SrmName, taskId);
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "SendCallBackTask", msg);
                return false;
            }
        }
        /// <summary>
        /// 是否可以发送出入库任务
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CanSendTask(int deviceId, out string msg)
        {
            msg = "";
            if (!IsOnline)
            {
                msg = string.Format("堆垛机{0}不在线", this.SrmName);
                return false;
            }
 
            if (RMode != (int)ESrmMode.自动模式)
            {
                msg = string.Format("堆垛机{0}非自动模式", this.SrmName);
                return false;
            }
 
            if (RAlarm)
            {
                msg = string.Format("堆垛机{0}报警中", this.SrmName);
                return false;
            }
 
            if (RLiftFull)
            {
                msg = string.Format("堆垛机{0}叉齿有货", this.SrmName);
                return false;
            }
 
            if (RState != (int)ESrmState.空闲)
            {
                msg = string.Format("堆垛机{0}非空闲", this.SrmName);
                return false;
            }
            return true;
        }
 
        /// <summary>
        /// 是否可以发送移库任务
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CanDoMove(int deviceId, out string msg)
        {
            msg = "";
            if (!IsOnline)
            {
                msg = string.Format("堆垛机{0}不在线", this.SrmName);
                return false;
            }
 
            if (RMode != (int)ESrmMode.自动模式)
            {
                msg = string.Format("堆垛机{0}非自动模式", this.SrmName);
                return false;
            }
 
            if (RAlarm)
            {
                msg = string.Format("堆垛机{0}报警中", this.SrmName);
                return false;
            }
 
            if (RLiftFull)
            {
                msg = string.Format("堆垛机{0}叉齿有货", this.SrmName);
                return false;
            }
 
            if (RState != (int)ESrmState.空闲)
            {
                msg = string.Format("堆垛机{0}非空闲", this.SrmName);
                return false;
            }
            return true;
        }
 
        /// <summary>
        /// 向设备写入指令
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        public bool WriteValue(string addr, object value)
        {
            string msg = string.Empty;
            if (!string.IsNullOrEmpty(addr))
            {
                try
                {
                    if (!this.WS71500.IsConnected)
                    {
                        this.WS71500.Close();
                        Thread.Sleep(100);
                        this.WS71500.Open();
                    }
 
                    msg = this.WS71500.Write(addr, value).ToString();
                    if (msg != "NoError")
                    {
                       string msg1 = this.WS71500.Write(addr, value).ToString();
                       if (msg1 != "NoError")
                       {
                           this.WS71500.Close();
                           Thread.Sleep(100);
                           this.WS71500.Open();
                           LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), this.SrmName + "WriteValue" + addr, msg);
                           return false;
                       }
                       else 
                       {
                           return true;
                       }
                    }
                    else
                    {
                        return true;
                    }
                }
                catch
                {
 
                    msg = addr + "写入失败  ";
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), this.SrmName + "WriteValue", msg);
                    return false;
                }
            }
            else
            {
                msg = addr + "写入失败  ";
                LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), this.SrmName+"WriteValue", msg);
                return false;
            }
        }
    }
}