#
18516761980
2022-03-17 a0adee2f689c85399ca311ef7691cd911a00fd3b
src/main/java/com/zy/core/thread/MelsecCrnThread.java
@@ -24,6 +24,7 @@
import com.zy.core.model.protocol.CrnProtocol;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.text.MessageFormat;
import java.util.Date;
@@ -50,6 +51,11 @@
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        this.connect();
//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        while (true) {
            try {
                int step = 1;
@@ -137,7 +143,7 @@
            log.error("堆垛机plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            initCrn();
        }
        melsecMcNet.ConnectClose();
//        melsecMcNet.ConnectClose();
        return result;
    }
@@ -146,9 +152,11 @@
     */
    private void readStatus(){
        try {
            if (this.slave.getId() == 1) {
                System.out.println("===");
            }
//            if (this.slave.getId() == 1 && flag1.equals(0)) {
//                Thread.sleep(3000);
//                flag1=1;
//                System.out.println("===");
//            }
            OperateResultExOne<byte[]> result = melsecMcNet.Read("D20", (short) 70);
            if (result.IsSuccess) {
                if (null == crnProtocol) {
@@ -195,6 +203,7 @@
                BasCrnp basCrnp = new BasCrnp();
                basCrnp.setCrnErr(crnProtocol.getAlarm()==null?0:crnProtocol.getAlarm().longValue());
                basCrnp.setCrnNo(slave.getId());
                basCrnp.setCrnSts((int)crnProtocol.getMode());
                if (!basCrnpService.updateById(crnProtocol.toSqlModel(basCrnp))){
                    log.error("堆垛机plc数据库更新失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
                }
@@ -220,24 +229,73 @@
            log.error("堆垛机写入命令为空");
            return false;
        }
        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.getCommand();
        OperateResult result = melsecMcNet.Write("D0", array);
        if (command.getAckFinish() == 0) {
            short commandFinish = 1;
            result = melsecMcNet.Write("D9", commandFinish);
        OperateResult result = null;
        try{
            Integer exeCount=0;
            do{
                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.getCommand();
                result = melsecMcNet.Write("D0", array);
                if(result.IsSuccess) {
                    if (command.getAckFinish() == 0) {
                        short commandFinish = 1;
                        result = melsecMcNet.Write("D9", commandFinish);
                        if(result.IsSuccess){
                            //写任务确认返回成功后,查询确认一次
                            Thread.sleep(200);
                            OperateResultExOne<byte[]> result1 = melsecMcNet.Read("D9", (short)2);
                            if(result1.IsSuccess){
                                short commandVal = melsecMcNet.getByteTransform().TransInt16(result1.Content,2);
                                if(commandVal==1){
                                    break;
                                } else {
                                    exeCount++;
                                }
                            } else{
                                exeCount++;
                            }
                        }else{
                            exeCount++;
                        }
                    }
                }else{
                    exeCount++;
                }
                Thread.sleep(200);
            }while(exeCount>3);
        }catch (Exception e){
        }
//        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.getCommand();
//        OperateResult result = melsecMcNet.Write("D0", array);
//
//        if (command.getAckFinish() == 0) {
//            short commandFinish = 1;
//            result = melsecMcNet.Write("D9", commandFinish);
//        }
        try {
            // 日志记录
@@ -262,7 +320,7 @@
            bean.insert(basCrnOpt);
        } catch (Exception ignore) {}
        if (result.IsSuccess) {
        if (result != null && result.IsSuccess) {
            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;