自动化立体仓库 - WCS系统
#
lsh
2024-06-28 271f8ac3fc635fcf96d3226dc7d37f81c9755c2b
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
package com.zy.core.thread;
 
import HslCommunication.Core.Types.OperateResult;
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.alibaba.fastjson.JSON;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasCrnOpt;
import com.zy.asrs.entity.BasCrnp;
import com.zy.asrs.service.BasCrnOptService;
import com.zy.asrs.service.BasCrnpService;
import com.zy.core.CrnThread;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.*;
import com.zy.core.model.CrnSlave;
import com.zy.core.model.Task;
import com.zy.core.model.command.CrnCommand;
import com.zy.core.model.cpmmandParam.CrnCommandParam;
import com.zy.core.model.protocol.CrnProtocol;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
 
import java.text.MessageFormat;
import java.util.Date;
 
/**
 * 堆垛机线程
 * Created by vincent on 2020/8/4
 */
@Data
@Slf4j
public class SiemensCrnThread implements Runnable, CrnThread {
 
    private SiemensS7Net siemensNet;
    private CrnSlave slave;
    private CrnProtocol crnProtocol;
    private boolean resetFlag = false;
    private boolean resetFlagTwo = false;
 
    public Long sign = System.currentTimeMillis();
 
    public boolean isRunning = true;
 
    /**
     * 堆垛机是否在回原点运动中标记
     */
    private boolean backHpFlag = false;
    private boolean backHpFlagTwo = false;
 
    public SiemensCrnThread(CrnSlave slave) {
        this.slave = slave;
    }
 
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        this.connect();
        sign = System.currentTimeMillis();
        while (isRunning) {
            try {
                int step = 1;
                Task task = MessageQueue.poll(SlaveType.Crn, slave.getId());
                if (task != null) {
                    step = task.getStep();
                }
                switch (step) {
                    // 读数据
                    case 1:
                        readStatus();
                        break;
                    // 写入数据
                    case 2:
                        write((CrnCommand) task.getData());
                        break;
                    // 复位
                    case 3:
                        CrnCommand command = (CrnCommand) task.getData();
                        if (null == command) {
                            command = new CrnCommand();
                        }
                        command.setCrnNo(slave.getId()); // 堆垛机编号
                        command.setTaskNo((short) 0); // 工作号
                        command.setAckFinish((short) 1);  // 任务完成确认位
                        command.setTaskMode(CrnTaskModeType.NONE); // 任务模式
                        command.setSourcePosX((short)0);     // 源库位排
                        command.setSourcePosY((short)0);     // 源库位列
                        command.setSourcePosZ((short)0);     // 源库位层
                        command.setDestinationPosX((short)0);     // 目标库位排
                        command.setDestinationPosY((short)0);     // 目标库位列
                        command.setDestinationPosZ((short)0);     // 目标库位层
                        write(command);
                        break;
                    // 写入数据
                    case 5:
                        write5((CrnCommand) task.getData());
                        break;
                    // 复位
                    case 6:
                        CrnCommand command2 = (CrnCommand) task.getData();
                        if (null == command2) {
                            command2 = new CrnCommand();
                        }
                        command2.setCrnNo(slave.getId()); // 堆垛机编号
                        command2.setTaskNo((short) 0); // 工作号
                        command2.setAckFinish((short) 1);  // 任务完成确认位
                        command2.setTaskMode(CrnTaskModeType.NONE); // 任务模式
                        command2.setSourcePosX((short)0);     // 源库位排
                        command2.setSourcePosY((short)0);     // 源库位列
                        command2.setSourcePosZ((short)0);     // 源库位层
                        command2.setDestinationPosX((short)0);     // 目标库位排
                        command2.setDestinationPosY((short)0);     // 目标库位列
                        command2.setDestinationPosZ((short)0);     // 目标库位层
                        write5(command2);
                        break;
                    // 复位
                    case 7:
                        write9((CrnCommandParam) task.getData());
                        break;
                    default:
                        break;
                }
                Thread.sleep(500);
            } catch (Exception e) {
//                e.printStackTrace();
            }
//            System.out.println("第"+slave.getId()+"个堆垛机线程运行一轮:"+(System.currentTimeMillis()-sign));
            sign = System.currentTimeMillis();
        }
    }
 
    /**
     * 初始化堆垛机状态
     */
    private void initCrn() {
        if (null == crnProtocol) {
            crnProtocol = new CrnProtocol();
        }
        crnProtocol.setMode((short) -1);
//        crnProtocol.setTaskNo((short)0);
        crnProtocol.setStatus((short)-1);
        crnProtocol.setStatusTwo((short)-1);
        crnProtocol.setBay((short)0);
        crnProtocol.setLevel((short)0);
        crnProtocol.setForkPos((short) -1);
        crnProtocol.setLiftPos((short) -1);
        crnProtocol.setWalkPos((short)0);
        crnProtocol.setLoaded((short)0);
        crnProtocol.setAlarm((short)0);
        crnProtocol.setxSpeed((short) 0);
        crnProtocol.setySpeed((short) 0);
        crnProtocol.setzSpeed((short) 0);
        crnProtocol.setxDistance((short) 0);
        crnProtocol.setyDistance((short) 0);
        crnProtocol.setxDuration((short) 0);
        crnProtocol.setyDuration((short) 0);
 
        // 根据实时信息更新数据库
        BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class);
        BasCrnp basCrnp = basCrnpService.selectById(slave.getId());
        basCrnp.setCrnSts((int)crnProtocol.getMode());
        basCrnp.setCrnStatusOne(-1);
        basCrnp.setCrnStatusTwo(-1);
        if (!basCrnpService.updateById(basCrnp)){
            log.error("堆垛机plc数据库更新失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
        }
    }
 
    @Override
    public boolean connect() {
        boolean result = false;
        siemensNet = new SiemensS7Net(SiemensPLCS.S1200, slave.getIp());
        siemensNet.setRack(slave.getRack().byteValue());
        siemensNet.setSlot(slave.getSlot().byteValue());
        OperateResult connect = siemensNet.ConnectServer();
        if(connect.IsSuccess){
            result = true;
            OutputQueue.CRN.offer(MessageFormat.format( "【{0}】堆垛机plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            log.info("堆垛机plc连接成功 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
        } else {
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】堆垛机plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            log.error("堆垛机plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
            initCrn();
        }
//        siemensNet.ConnectClose();
        return result;
    }
 
    /**
     * 读取状态
     */
    private void readStatus(){
        try {
            OperateResultExOne<byte[]> result = new OperateResultExOne<byte[]>();
            if (slave.getId()>2 && slave.getId()<6){
                result = siemensNet.Read("DB101.0", (short) 66);
            } else {
                result = siemensNet.Read("DB101.0", (short) 56);
            }
            if (result.IsSuccess) {
                if (null == crnProtocol) {
                    crnProtocol = new CrnProtocol();
                    crnProtocol.setCrnNo(slave.getId());
                }
                crnProtocol.setMode(siemensNet.getByteTransform().TransInt16(result.Content, 0));
                crnProtocol.setTaskNo(siemensNet.getByteTransform().TransInt16(result.Content, 2));
                crnProtocol.setStatus(siemensNet.getByteTransform().TransInt16(result.Content, 4));
                crnProtocol.setBay(siemensNet.getByteTransform().TransInt16(result.Content, 6));
                crnProtocol.setLevel(siemensNet.getByteTransform().TransInt16(result.Content, 8));
                crnProtocol.setForkPos(siemensNet.getByteTransform().TransInt16(result.Content, 10));
 
                if (slave.getId()<3 || slave.getId()>5){
                    crnProtocol.setLiftPos(siemensNet.getByteTransform().TransInt16(result.Content, 12));
                    crnProtocol.setWalkPos(siemensNet.getByteTransform().TransInt16(result.Content, 14));
                    crnProtocol.setLoaded(siemensNet.getByteTransform().TransInt16(result.Content, 16));
                    crnProtocol.setAlarm(siemensNet.getByteTransform().TransInt16(result.Content, 18));
                    crnProtocol.setTemp1(siemensNet.getByteTransform().TransInt16(result.Content, 20));
                    crnProtocol.setTemp2(siemensNet.getByteTransform().TransInt16(result.Content, 22));
                    crnProtocol.setTemp3(siemensNet.getByteTransform().TransInt16(result.Content, 24));
                    crnProtocol.setTemp4(siemensNet.getByteTransform().TransInt16(result.Content, 26));
                    crnProtocol.setxSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 28));
                    crnProtocol.setySpeed(siemensNet.getByteTransform().TransInt16(result.Content, 32));
                    crnProtocol.setzSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 36));
                    crnProtocol.setxDistance(siemensNet.getByteTransform().TransInt16(result.Content, 40));
                    crnProtocol.setyDistance(siemensNet.getByteTransform().TransInt16(result.Content, 44));
                    crnProtocol.setxDuration(siemensNet.getByteTransform().TransInt16(result.Content, 48));
                    crnProtocol.setyDuration(siemensNet.getByteTransform().TransInt16(result.Content, 52));
                } else {
                    crnProtocol.setLoaded(siemensNet.getByteTransform().TransInt16(result.Content, 12));
 
                    crnProtocol.setTaskNoTwo(siemensNet.getByteTransform().TransInt16(result.Content, 14));
                    crnProtocol.setStatusTwo(siemensNet.getByteTransform().TransInt16(result.Content, 16));
                    crnProtocol.setBayTwo(siemensNet.getByteTransform().TransInt16(result.Content, 18));
                    crnProtocol.setLevelTwo(siemensNet.getByteTransform().TransInt16(result.Content, 20));
                    crnProtocol.setForkPosTwo(siemensNet.getByteTransform().TransInt16(result.Content, 22));
                    crnProtocol.setLoadedTwo(siemensNet.getByteTransform().TransInt16(result.Content, 24));
 
                    crnProtocol.setWalkPos(siemensNet.getByteTransform().TransInt16(result.Content, 26));
                    crnProtocol.setWalkPosTwo(siemensNet.getByteTransform().TransInt16(result.Content, 28));
 
                    crnProtocol.setLiftPos(siemensNet.getByteTransform().TransInt16(result.Content, 30));
                    crnProtocol.setAlarm(siemensNet.getByteTransform().TransInt16(result.Content, 32));
 
                    crnProtocol.setxSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 34));
                    crnProtocol.setySpeed(siemensNet.getByteTransform().TransInt16(result.Content, 38));
                    crnProtocol.setzSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 42));
//                    crnProtocol.setzSpeed(siemensNet.getByteTransform().TransInt16(result.Content, 42));
 
                    crnProtocol.setxDistance(siemensNet.getByteTransform().TransInt16(result.Content, 50));
                    crnProtocol.setyDistance(siemensNet.getByteTransform().TransInt16(result.Content, 54));
                    crnProtocol.setxDuration(siemensNet.getByteTransform().TransInt16(result.Content, 58));
                    crnProtocol.setyDuration(siemensNet.getByteTransform().TransInt16(result.Content, 62));
                }
 
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
 
                // 复位信号
                if (!Cools.isEmpty(crnProtocol.getStatusType()) && (crnProtocol.getStatusType().equals(CrnStatusType.WAITING) || crnProtocol.getStatusType().equals(CrnStatusType.SEPARATE_WAITING))) {
                    log.error("-------------------------工位1-------------------------第一步、[堆垛机号:{}, 工作号:{}, 载货台:{}]==>> 状态为{},等待确认!!",
                            slave.getId(),crnProtocol.getTaskNo(), crnProtocol.getLoaded()==1 ? "有物" : "无物",crnProtocol.getStatusType());
                    if (resetFlag) {
                        if(crnProtocol.getTaskNo()==9999){
                            backHpFlag = false;
                        }
                        CrnCommand crnCommand = new CrnCommand();
                        crnCommand.setAckFinish((short)1);
                        if (write(crnCommand)) {
                            resetFlag = false;
                        }
                    }
                }
 
                // 复位信号
                if (!Cools.isEmpty(crnProtocol.getStatusTypeTwo()) && (crnProtocol.getStatusTypeTwo().equals(CrnStatusType.WAITING) || crnProtocol.getStatusTypeTwo().equals(CrnStatusType.SEPARATE_WAITING))) {
                    log.error("-------------------------工位2-------------------------第一步、[堆垛机号:{}, 工作号:{}, 载货台:{}]==>> 状态为{},等待确认!!",
                            slave.getId(),crnProtocol.getTaskNoTwo(), crnProtocol.getLoadedTwo()==1 ? "有物" : "无物",crnProtocol.getStatusTypeTwo());
                    if (resetFlagTwo) {
                        if(crnProtocol.getTaskNoTwo()==9999){
                            backHpFlagTwo = false;
                        }
                        CrnCommand crnCommand = new CrnCommand();
                        crnCommand.setAckFinish((short)1);
                        if (write5(crnCommand)) {
                            resetFlagTwo = false;
                        }
                    }
                }
 
                try {
                    // 根据实时信息更新数据库
                    BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class);
                    BasCrnp basCrnp = new BasCrnp();
                    basCrnp.setCrnNo(slave.getId());
                    basCrnp.setCrnSts((int)crnProtocol.getMode());
                    if (!basCrnpService.updateById(crnProtocol.toSqlModel(basCrnp))){
                        log.error("堆垛机plc数据库更新失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
                    }
                } catch (Exception ignore){}
 
            } else {
                initCrn();
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】读取堆垛机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
                log.error("读取堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot());
            }
        } catch (Exception e) {
            e.printStackTrace();
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】读取堆垛机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.error("读取堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            initCrn();
        }
    }
 
    private void convertRow(CrnCommand crnCommand) {
        if (crnCommand.getSourcePosX() != null && crnCommand.getSourcePosX() != 0) {
            crnCommand.setSourcePosX((short) (crnCommand.getSourcePosX() - ((slave.getId() - 1) * 4)));
        }
        if (crnCommand.getDestinationPosX() != null && crnCommand.getDestinationPosX() != 0) {
            crnCommand.setDestinationPosX((short) (crnCommand.getDestinationPosX() - ((slave.getId() - 1) * 4)));
        }
    }
 
    /**
     * 写入数据
     */
    private boolean write(CrnCommand command) throws InterruptedException {
        if (null == command) {
            log.error("堆垛机写入命令为空");
            return false;
        }
//        convertRow(command);
        command.setCrnNo(slave.getId());
        short[] array = new short[10];
        array[0] = command.getAckFinish();
        array[1] = command.getTaskNo();
        array[2] = command.getTaskMode();
        array[3] = command.getSourcePosX();
        array[4] = command.getSourcePosY();
        array[5] = command.getSourcePosZ();
        array[6] = command.getDestinationPosX();
        array[7] = command.getDestinationPosY();
        array[8] = command.getDestinationPosZ();
//        array[9] = command.getSourceStaNo();
//        array[10] = command.getDestinationStaNo();
        array[9] = command.getCommand();
        OperateResult result18 = siemensNet.Write("DB100.18", (short)0);
        OperateResult result = siemensNet.Write("DB100.0", array);
        log.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), array);
 
 
 
        //堆垛机任务写入后,回读一次,看是否成功
        Thread.sleep(200);
        int writeCount = 1;
        do {
            try{
                if(!result.IsSuccess){
                    log.error("写入堆垛机plc数据失败,重新下发任务  写入直接失败 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSONString(command),writeCount);
                    result = siemensNet.Write("DB100.0", array);
                    Thread.sleep(100);
                    writeCount++;
                    continue;
                }
                OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 24);
                if (resultRead.IsSuccess) {
                    CrnCommand one = new CrnCommand();
                    one.setTaskNo(siemensNet.getByteTransform().TransInt16(resultRead.Content, 2));
                    one.setTaskMode(siemensNet.getByteTransform().TransInt16(resultRead.Content, 4));
                    one.setSourcePosX(siemensNet.getByteTransform().TransInt16(resultRead.Content, 6));
                    one.setSourcePosY(siemensNet.getByteTransform().TransInt16(resultRead.Content, 8));
                    one.setSourcePosZ(siemensNet.getByteTransform().TransInt16(resultRead.Content, 10));
                    one.setDestinationPosX(siemensNet.getByteTransform().TransInt16(resultRead.Content, 12));
                    one.setDestinationPosY(siemensNet.getByteTransform().TransInt16(resultRead.Content, 14));
                    one.setDestinationPosZ(siemensNet.getByteTransform().TransInt16(resultRead.Content, 16));
                    if (!command.getTaskNo().equals(one.getTaskNo()) || !command.getTaskMode().equals(one.getTaskMode())
                            || !command.getSourcePosX().equals(one.getSourcePosX()) || !command.getSourcePosY().equals(one.getSourcePosY())
                            || !command.getSourcePosZ().equals(one.getSourcePosZ()) || !command.getDestinationPosX().equals(one.getDestinationPosX())
                            || !command.getDestinationPosY().equals(one.getDestinationPosY()) || !command.getDestinationPosZ().equals(one.getDestinationPosZ())
                    ){
                        try{
                            log.error("堆垛机命令地址写入后回读失败==>不一致[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(one));
                        }catch (Exception e){
                            try{
                                log.error("日志打印失败:===>>参数one报错 [id:{}],{}", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(resultRead));
                            }catch (Exception e1){
                                log.error("日志打印失败:===>> [id:{}],{}", slave.getId(), JSON.toJSONString(command));
                            }
                        }
                        try{
                            Thread.sleep(100);
                        }catch (Exception e){
 
                        }
                        log.error("写入堆垛机plc数据失败,重新下发任务  回读不一致 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSONString(command),writeCount);
                        result = siemensNet.Write("DB100.0", array);
                        writeCount++;
                        continue;
                    } else {
                        log.info("堆垛机命令地址写入后回读成功[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(one));
                        break;
                    }
                }
            }catch (Exception e){
                log.error("堆垛机命令地址写入后回读出错,异常:"+e);
            }
            writeCount++;
        } while (writeCount<6);
 
        if (command.getAckFinish() == 0) {
            short commandFinish = 1;
            Thread.sleep(100L);
            result = siemensNet.Write("DB100.18", commandFinish);
            int signFinish = 1;
            while (signFinish<5){
                OperateResultExOne<byte[]> result10018 = siemensNet.Read("DB100.18", (short) 2);
                short transInt16 = siemensNet.getByteTransform().TransInt16(result10018.Content, 0);
                if (transInt16 != commandFinish){
                    log.info("下发DB100.18  回读失败" + "commandFinish:"+commandFinish);
                    log.info("下发DB100.18  回读失败" + "array:"+ JSON.toJSONString(array));
                    result = siemensNet.Write("DB100.18", commandFinish);
                    signFinish++;
                }else {
                    log.info("下发DB100.18" + "commandFinish:"+commandFinish);
                    log.info("下发DB100.18" + "array:"+ JSON.toJSONString(array));
                    break;
                }
            }
        }
 
        try {
            // 日志记录
            BasCrnOptService bean = SpringUtils.getBean(BasCrnOptService.class);
            BasCrnOpt basCrnOpt = new BasCrnOpt(
                    command.getTaskNo().intValue(),    // 任务号
                    command.getCrnNo(),    // 堆垛机[非空]
                    new Date(),    // 下发时间
                    command.getTaskModeType().toString(),    // 模式
                    command.getSourcePosX().intValue(),    // 源排
                    command.getSourcePosY().intValue(),    // 源列
                    command.getSourcePosZ().intValue(),    // 源层
                    null,    // 源站
                    command.getDestinationPosX().intValue(),    // 目标排
                    command.getDestinationPosY().intValue(),    // 目标列
                    command.getDestinationPosZ().intValue(),    // 目标层
                    null,    // 目标站
                    null,    // 响应结果
                    null,    // 修改时间
                    null    // 修改人员
            );
            bean.insert(basCrnOpt);
        } catch (Exception ignore) {}
 
        if (result != null && result.IsSuccess) {
            Thread.sleep(200);
            this.readStatus();
            log.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSONString(command));
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSONString(command)));
            return true;
        } else {
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.error("写入堆垛机plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            return false;
        }
    }
 
    /**
     * 写入数据  工位2
     */
    private boolean write5(CrnCommand command) throws InterruptedException {
        if (null == command) {
            log.error("堆垛机写入命令为空");
            return false;
        }
//        convertRow(command);
        command.setCrnNo(slave.getId());
        short[] array = new short[10];
        array[0] = command.getAckFinish();
        array[1] = command.getTaskNo();
        array[2] = command.getTaskMode();
        array[3] = command.getSourcePosX();
        array[4] = command.getSourcePosY();
        array[5] = command.getSourcePosZ();
        array[6] = command.getDestinationPosX();
        array[7] = command.getDestinationPosY();
        array[8] = command.getDestinationPosZ();
//        array[9] = command.getSourceStaNo();
//        array[10] = command.getDestinationStaNo();
        array[9] = command.getCommand();
        OperateResult result18 = siemensNet.Write("DB100.38", (short)0);
        OperateResult result = siemensNet.Write("DB100.20", array);
        log.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), array);
 
        //堆垛机任务写入后,回读一次,看是否成功
        Thread.sleep(200);
        int writeCount = 1;
        do {
            try{
                if(!result.IsSuccess){
                    log.error("写入堆垛机plc数据失败,重新下发任务  写入直接失败 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSONString(command),writeCount);
                    result = siemensNet.Write("DB100.20", array);
                    Thread.sleep(100);
                    writeCount++;
                    continue;
                }
                OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.20", (short) 24);
                if (resultRead.IsSuccess) {
                    CrnCommand one = new CrnCommand();
                    one.setTaskNo(siemensNet.getByteTransform().TransInt16(resultRead.Content, 2));
                    one.setTaskMode(siemensNet.getByteTransform().TransInt16(resultRead.Content, 4));
                    one.setSourcePosX(siemensNet.getByteTransform().TransInt16(resultRead.Content, 6));
                    one.setSourcePosY(siemensNet.getByteTransform().TransInt16(resultRead.Content, 8));
                    one.setSourcePosZ(siemensNet.getByteTransform().TransInt16(resultRead.Content, 10));
                    one.setDestinationPosX(siemensNet.getByteTransform().TransInt16(resultRead.Content, 12));
                    one.setDestinationPosY(siemensNet.getByteTransform().TransInt16(resultRead.Content, 14));
                    one.setDestinationPosZ(siemensNet.getByteTransform().TransInt16(resultRead.Content, 16));
                    if (!command.getTaskNo().equals(one.getTaskNo()) || !command.getTaskMode().equals(one.getTaskMode())
                            || !command.getSourcePosX().equals(one.getSourcePosX()) || !command.getSourcePosY().equals(one.getSourcePosY())
                            || !command.getSourcePosZ().equals(one.getSourcePosZ()) || !command.getDestinationPosX().equals(one.getDestinationPosX())
                            || !command.getDestinationPosY().equals(one.getDestinationPosY()) || !command.getDestinationPosZ().equals(one.getDestinationPosZ())
                    ){
                        try{
                            log.error("堆垛机命令地址写入后回读失败==>不一致[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(one));
                        }catch (Exception e){
                            try{
                                log.error("日志打印失败:===>>参数one报错 [id:{}],{}", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(resultRead));
                            }catch (Exception e1){
                                log.error("日志打印失败:===>> [id:{}],{}", slave.getId(), JSON.toJSONString(command));
                            }
                        }
                        try{
                            Thread.sleep(100);
                        }catch (Exception e){
 
                        }
                        log.error("写入堆垛机plc数据失败,重新下发任务  回读不一致 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSONString(command),writeCount);
                        result = siemensNet.Write("DB100.20", array);
                        writeCount++;
                        continue;
                    } else {
                        log.info("堆垛机命令地址写入后回读成功[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSONString(command),JSON.toJSONString(one));
                        break;
                    }
                }
            }catch (Exception e){
                log.error("堆垛机命令地址写入后回读出错,异常:"+e);
            }
            writeCount++;
        } while (writeCount<6);
 
        if (command.getAckFinish() == 0) {
            short commandFinish = 1;
            Thread.sleep(100L);
            result = siemensNet.Write("DB100.38", commandFinish);
            int signFinish = 1;
            while (signFinish<5){
                OperateResultExOne<byte[]> result10018 = siemensNet.Read("DB100.38", (short) 2);
                short transInt16 = siemensNet.getByteTransform().TransInt16(result10018.Content, 0);
                if (transInt16 != commandFinish){
                    log.info("下发DB100.38  回读失败" + "commandFinish:"+commandFinish);
                    log.info("下发DB100.38  回读失败" + "array:"+ JSON.toJSONString(array));
                    result = siemensNet.Write("DB100.38", commandFinish);
                    signFinish++;
                }else {
                    log.info("下发DB100.38" + "commandFinish:"+commandFinish);
                    log.info("下发DB100.38" + "array:"+ JSON.toJSONString(array));
                    break;
                }
            }
        }
 
        try {
            // 日志记录
            BasCrnOptService bean = SpringUtils.getBean(BasCrnOptService.class);
            BasCrnOpt basCrnOpt = new BasCrnOpt(
                    command.getTaskNo().intValue(),    // 任务号
                    command.getCrnNo(),    // 堆垛机[非空]
                    new Date(),    // 下发时间
                    command.getTaskModeType().toString(),    // 模式
                    command.getSourcePosX().intValue(),    // 源排
                    command.getSourcePosY().intValue(),    // 源列
                    command.getSourcePosZ().intValue(),    // 源层
                    null,    // 源站
                    command.getDestinationPosX().intValue(),    // 目标排
                    command.getDestinationPosY().intValue(),    // 目标列
                    command.getDestinationPosZ().intValue(),    // 目标层
                    null,    // 目标站
                    null,    // 响应结果
                    null,    // 修改时间
                    null    // 修改人员
            );
            bean.insert(basCrnOpt);
        } catch (Exception ignore) {}
 
        if (result != null && result.IsSuccess) {
            Thread.sleep(200);
            this.readStatus();
            log.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSONString(command));
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSONString(command)));
            return true;
        } else {
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.error("写入堆垛机plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            return false;
        }
    }
 
    /**
     * 写入数据  工位2
     */
    private boolean write9(CrnCommandParam command) throws InterruptedException {
        if (null == command) {
            log.error("堆垛机写入命令为空");
            return false;
        }
//        convertRow(command);
        command.setCrnNo(slave.getId());
        short[] array = new short[10];
        array[0] = command.getAckFinish();
        array[1] = command.getTaskNo();
        array[2] = command.getTaskMode();
        array[3] = command.getSourcePosX();
        array[4] = command.getSourcePosY();
        array[5] = command.getSourcePosZ();
        array[6] = command.getDestinationPosX();
        array[7] = command.getDestinationPosY();
        array[8] = command.getDestinationPosZ();
//        array[9] = command.getSourceStaNo();
//        array[10] = command.getDestinationStaNo();
        array[9] = command.getCommand();
        OperateResult result18 = siemensNet.Write("DB100.38", (short)0);
        OperateResult result = siemensNet.Write("DB100.20", array);
        log.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), array);
 
        //堆垛机任务写入后,回读一次,看是否成功
        Thread.sleep(200);
        int writeCount = 1;
        do {
            try{
                if(!result.IsSuccess){
                    log.error("写入堆垛机plc数据失败,重新下发任务  写入直接失败 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSON(command),writeCount);
                    result = siemensNet.Write("DB100.20", array);
                    Thread.sleep(100);
                    writeCount++;
                    continue;
                }
                OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.20", (short) 24);
                if (resultRead.IsSuccess) {
                    CrnCommand one = new CrnCommand();
                    one.setTaskNo(siemensNet.getByteTransform().TransInt16(resultRead.Content, 2));
                    one.setTaskMode(siemensNet.getByteTransform().TransInt16(resultRead.Content, 4));
                    one.setSourcePosX(siemensNet.getByteTransform().TransInt16(resultRead.Content, 6));
                    one.setSourcePosY(siemensNet.getByteTransform().TransInt16(resultRead.Content, 8));
                    one.setSourcePosZ(siemensNet.getByteTransform().TransInt16(resultRead.Content, 10));
                    one.setDestinationPosX(siemensNet.getByteTransform().TransInt16(resultRead.Content, 12));
                    one.setDestinationPosY(siemensNet.getByteTransform().TransInt16(resultRead.Content, 14));
                    one.setDestinationPosZ(siemensNet.getByteTransform().TransInt16(resultRead.Content, 16));
                    if (!command.getTaskNo().equals(one.getTaskNo()) || !command.getTaskMode().equals(one.getTaskMode())
                            || !command.getSourcePosX().equals(one.getSourcePosX()) || !command.getSourcePosY().equals(one.getSourcePosY())
                            || !command.getSourcePosZ().equals(one.getSourcePosZ()) || !command.getDestinationPosX().equals(one.getDestinationPosX())
                            || !command.getDestinationPosY().equals(one.getDestinationPosY()) || !command.getDestinationPosZ().equals(one.getDestinationPosZ())
                    ){
                        try{
                            log.error("堆垛机命令地址写入后回读失败==>不一致[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one));
                        }catch (Exception e){
                            try{
                                log.error("日志打印失败:===>>参数one报错 [id:{}],{}", slave.getId(), JSON.toJSON(command),JSON.toJSON(resultRead));
                            }catch (Exception e1){
                                log.error("日志打印失败:===>> [id:{}],{}", slave.getId(), JSON.toJSON(command));
                            }
                        }
                        try{
                            Thread.sleep(100);
                        }catch (Exception e){
 
                        }
                        log.error("写入堆垛机plc数据失败,重新下发任务  回读不一致 ===>> [id:{}],{},[写入次数:{}]", slave.getId(), JSON.toJSON(command),writeCount);
                        result = siemensNet.Write("DB100.20", array);
                        writeCount++;
                        continue;
                    } else {
                        log.info("堆垛机命令地址写入后回读成功[id:{}] >>>>> 写入[{}],===>>回读[{}]", slave.getId(), JSON.toJSON(command),JSON.toJSON(one));
                        break;
                    }
                }
            }catch (Exception e){
                log.error("堆垛机命令地址写入后回读出错,异常:"+e);
            }
            writeCount++;
        } while (writeCount<6);
 
        if (command.getAckFinish() == 0) {
            short commandFinish = 1;
            Thread.sleep(100L);
            result = siemensNet.Write("DB100.38", commandFinish);
            int signFinish = 1;
            while (signFinish<5){
                OperateResultExOne<byte[]> result10018 = siemensNet.Read("DB100.38", (short) 2);
                short transInt16 = siemensNet.getByteTransform().TransInt16(result10018.Content, 0);
                if (transInt16 != commandFinish){
                    log.info("下发DB100.38  回读失败" + "commandFinish:"+commandFinish);
                    log.info("下发DB100.38  回读失败" + "array:"+ JSON.toJSONString(array));
                    result = siemensNet.Write("DB100.38", commandFinish);
                    signFinish++;
                }else {
                    log.info("下发DB100.38" + "commandFinish:"+commandFinish);
                    log.info("下发DB100.38" + "array:"+ JSON.toJSONString(array));
                    break;
                }
            }
        }
 
        try {
            // 日志记录
            BasCrnOptService bean = SpringUtils.getBean(BasCrnOptService.class);
            BasCrnOpt basCrnOpt = new BasCrnOpt(
                    command.getTaskNo().intValue(),    // 任务号
                    command.getCrnNo(),    // 堆垛机[非空]
                    new Date(),    // 下发时间
                    command.getTaskModeType().toString(),    // 模式
                    command.getSourcePosX().intValue(),    // 源排
                    command.getSourcePosY().intValue(),    // 源列
                    command.getSourcePosZ().intValue(),    // 源层
                    null,    // 源站
                    command.getDestinationPosX().intValue(),    // 目标排
                    command.getDestinationPosY().intValue(),    // 目标列
                    command.getDestinationPosZ().intValue(),    // 目标层
                    null,    // 目标站
                    null,    // 响应结果
                    null,    // 修改时间
                    null    // 修改人员
            );
            bean.insert(basCrnOpt);
        } catch (Exception ignore) {}
 
        if (result != null && result.IsSuccess) {
            Thread.sleep(200);
            this.readStatus();
            log.info("堆垛机命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
            return true;
        } else {
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.error("写入堆垛机plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            return false;
        }
    }
 
 
    @Override
    public void close() {
        siemensNet.ConnectClose();
    }
 
    /******************************************************************************************/
    /**************************************** 测试专用 *****************************************/
    /*****************************************************************************************/
    public static void main(String[] args) throws InterruptedException {
        CrnSlave slave = new CrnSlave();
        slave.setId(1);
        slave.setIp("192.168.6.9");
        slave.setRack(0);
        slave.setSlot(0);
        SiemensCrnThread crnThread = new SiemensCrnThread(slave);
        crnThread.connect();
        crnThread.readStatus();
        System.out.println(JSON.toJSONString(crnThread.crnProtocol));
        Thread.sleep(3000L);
 
        // 1.入库 源和目标都发
//        CrnCommand command = new CrnCommand();
//        command.setCrnNo(1); // 堆垛机编号
//        command.setTaskNo((short) 0); // 工作号
//        command.setAckFinish((short) 0);  // 任务完成确认位
//        command.setTaskMode(CrnTaskModeType.PAKIN); // 任务模式
//        command.setSourcePosX((short) 1);     // 源库位排
//        command.setSourcePosY((short) 0);     // 源库位列
//        command.setSourcePosZ((short) 1);     // 源库位层
//        command.setDestinationPosX((short) 2);     // 目标库位排
//        command.setDestinationPosY((short) 3);     // 目标库位列
//        command.setDestinationPosZ((short) 1);     // 目标库位层
//        crnThread.write(command);
 
        // 2.出库 源和目标都发
//        CrnCommand command = new CrnCommand();
//        command.setCrnNo(1); // 堆垛机编号
//        command.setTaskNo((short) 0); // 工作号
//        command.setAckFinish((short) 0);  // 任务完成确认位
//        command.setTaskMode(CrnTaskModeType.PAKOUT); // 任务模式
//        command.setSourcePosX((short) 2);     // 源库位排
//        command.setSourcePosY((short) 4);     // 源库位列
//        command.setSourcePosZ((short) 3);     // 源库位层
//        command.setDestinationPosX((short) 1);     // 目标库位排
//        command.setDestinationPosY((short) 0);     // 目标库位列
//        command.setDestinationPosZ((short) 1);     // 目标库位层
//        crnThread.write(command);
 
 
//        // 3.库位移转   源和目标都发 pass
//        CrnCommand command = new CrnCommand();
//        command.setCrnNo(slave.getId()); // 堆垛机编号
//        command.setTaskNo((short) 0); // 工作号
//        command.setAckFinish((short) 0);  // 任务完成确认位
//        command.setTaskMode(CrnTaskModeType.LOC_MOVE); // 任务模式:  库位移转
//        command.setSourcePosX((short)2);     // 源库位排
//        command.setSourcePosY((short)2);     // 源库位列
//        command.setSourcePosZ((short)3);     // 源库位层
//        command.setDestinationPosX((short)2);     // 目标库位排
//        command.setDestinationPosY((short)4);     // 目标库位列
//        command.setDestinationPosZ((short)4);     // 目标库位层
//        crnThread.write(command);
 
        // 4.站位移转   源和目标都发
//        CrnCommand command = new CrnCommand();
//        command.setCrnNo(slave.getId()); // 堆垛机编号
//        command.setTaskNo((short) 0); // 工作号
//        command.setAckFinish((short) 0);  // 任务完成确认位
//        command.setTaskMode(CrnTaskModeType.SITE_MOVE); // 任务模式:  库位移转
//        command.setSourcePosX((short)1);     // 源库位排
//        command.setSourcePosY((short)0);     // 源库位列
//        command.setSourcePosZ((short)1);     // 源库位层
//        command.setDestinationPosX((short)2);     // 目标库位排
//        command.setDestinationPosY((short)0);     // 目标库位列
//        command.setDestinationPosZ((short)1);     // 目标库位层
//        crnThread.write(command);
 
//        // 5.回原点  不用发   pass
//        CrnCommand command = new CrnCommand();
//        command.setCrnNo(1); // 堆垛机编号
//        command.setTaskNo((short) 0); // 工作号
//        command.setAckFinish((short) 0);  // 任务完成确认位
//        command.setTaskMode(CrnTaskModeType.GO_ORIGIN); // 任务模式
//        command.setSourcePosX((short) 0);     // 源库位排
//        command.setSourcePosY((short) 0);     // 源库位列
//        command.setSourcePosZ((short) 0);     // 源库位层
//        command.setDestinationPosX((short) 0);     // 目标库位排
//        command.setDestinationPosY((short) 0);     // 目标库位列
//        command.setDestinationPosZ((short) 0);     // 目标库位层
//        crnThread.write(command);
 
//        // 7.坐标移行   目标发   pass
//        CrnCommand command = new CrnCommand();
//        command.setCrnNo(1); // 堆垛机编号
//        command.setTaskNo((short) 0); // 工作号
//        command.setAckFinish((short) 0);  // 任务完成确认位
//        command.setTaskMode(CrnTaskModeType.OFFSET_MOVE); // 任务模式
//        command.setSourcePosX((short) 0);     // 源库位排
//        command.setSourcePosY((short) 0);     // 源库位列
//        command.setSourcePosZ((short) 0);     // 源库位层
//        command.setDestinationPosX((short) 2);     // 目标库位排
//        command.setDestinationPosY((short) 1);     // 目标库位列
//        command.setDestinationPosZ((short) 1);     // 目标库位层
//        crnThread.write(command);
 
        // 只有出现指定异常才进行复位
//        if (crnThread.crnProtocol.getCrnError2().leftTakeNoneErr
//                || crnThread.crnProtocol.getCrnError2().rightTakeNoneErr
//                || crnThread.crnProtocol.getCrnError2().leftPutLoadErr
//                || crnThread.crnProtocol.getCrnError2().rightPutLoadErr) {
//            CrnCommand command = new CrnCommand();
//            command.setCrnNo(1); // 堆垛机编号
//            command.setAckFinish((short) 1);  // 任务完成确认位
//            command.setTaskMode(CrnTaskModeType.NONE); // 任务模式
//            Thread.sleep(3000L);
//            crnThread.write(command);
//        }
 
    }
 
    // 提供一个方法来停止线程
    public void requestStop() {
        isRunning = false;
    }
 
}