huang
2025-09-03 6c9369ab9b7da99687857004470e8d7824eb69ae
修改plc连接读取失败导致无法重新链接
2个文件已修改
139 ■■■■■ 已修改文件
JiuMuMesParent/common/servicebase/src/main/java/com/mes/service/ModbusTcp.java 15 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
JiuMuMesParent/moduleService/DeviceInteractionModule/src/main/java/com/mes/plcTaskThread/MachineLoad.java 124 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
JiuMuMesParent/common/servicebase/src/main/java/com/mes/service/ModbusTcp.java
@@ -38,12 +38,19 @@
        }
    }
    //连接
    public void connect(){
    public boolean connect() {
        try {
            this.socket=new Socket(Ip,Port);
            if (this.socket != null && !this.socket.isClosed()) {
                this.socket.close();
            }
            this.socket = new Socket(Ip, Port);
            this.socket.setSoTimeout(300);
        }catch (Exception e) {
            log.info("The IP address of the host cannot be determined:{}",e.getMessage());
            log.info("PLC连接成功");
            return true;
        } catch (Exception e) {
            log.error("PLC连接失败", e);
            this.socket = null;
            return false;
        }
    }
    //关闭连接
JiuMuMesParent/moduleService/DeviceInteractionModule/src/main/java/com/mes/plcTaskThread/MachineLoad.java
@@ -20,7 +20,7 @@
import java.util.Map;
@Slf4j
public class MachineLoad extends Thread{
public class MachineLoad extends Thread {
    TaskingMapper taskingMapper;
@@ -36,7 +36,8 @@
    public Machine thisMachine;
    //public Long machineId = 2L;
    public List<Map> sendRecords = new ArrayList<>();
    public MachineLoad(Machine machine,PrimitiveTaskMapper primitiveTaskMapper,TaskingMapper taskingMapper, MachineMapper machineMapper, TaskingService taskingService, PrimitiveTaskService primitiveTaskService, LineConfigurationService lineConfigurationService){
    public MachineLoad(Machine machine, PrimitiveTaskMapper primitiveTaskMapper, TaskingMapper taskingMapper, MachineMapper machineMapper, TaskingService taskingService, PrimitiveTaskService primitiveTaskService, LineConfigurationService lineConfigurationService) {
        this.thisMachine = machine;
        this.primitiveTaskMapper = primitiveTaskMapper;
        this.taskingMapper = taskingMapper;
@@ -45,70 +46,76 @@
        this.primitiveTaskService = primitiveTaskService;
        this.lineConfigurationService = lineConfigurationService;
    }
    public void plcStart() {
        Machine machine=machineMapper.selectById(thisMachine.getId());
        ModbusTcp modbusTcp1 =new ModbusTcp(machine.getIp(),machine.getPort(),machine.getFileName());
        modbusTcp1.connect();
        PlcAgreement plcAgreement=modbusTcp1.getPlcAgreement(machine.getFileName());
        ModbusTcp modbusTcp1 = null;
        try {
            if(!modbusTcp1.read(plcAgreement)){
            Machine machine = machineMapper.selectById(thisMachine.getId());
            modbusTcp1 = new ModbusTcp(machine.getIp(), machine.getPort(), machine.getFileName());
            if (!modbusTcp1.connect()) {
                log.error("连接PLC失败,退出本次操作");
                return;
            }
            PlcAgreement plcAgreement = modbusTcp1.getPlcAgreement(machine.getFileName());
            if (!modbusTcp1.read(plcAgreement)) {
                log.info("通讯读取新数据失败");
                if(!"关机".equals(machine.getStatePowerOn())){
                if (!"关机".equals(machine.getStatePowerOn())) {
                    machine.setStatePowerOn("关机");
                    machineMapper.updateById(machine);
                }
                modbusTcp1.close();
                return;
            }
            if(!"开机".equals(machine.getStatePowerOn())){
            if (!"开机".equals(machine.getStatePowerOn())) {
                machine.setStatePowerOn("开机");
                machineMapper.updateById(machine);
            }
            PlcParameter plcRequest =plcAgreement.getPlcParameter("plcRequest");//请求字
            PlcParameter mesSend =plcAgreement.getPlcParameter("mesSend");//发送字
            PlcParameter mesSendCount =plcAgreement.getPlcParameter("mesSendCount");//发送数量
            PlcParameter plcRequest = plcAgreement.getPlcParameter("plcRequest");//请求字
            PlcParameter mesSend = plcAgreement.getPlcParameter("mesSend");//发送字
            PlcParameter mesSendCount = plcAgreement.getPlcParameter("mesSendCount");//发送数量
            PlcParameter plcReport =plcAgreement.getPlcParameter("plcReport");//汇报字
            PlcParameter plcReportID =plcAgreement.getPlcParameter("plcReportID");//汇报ID
            PlcParameter mesConfirm =plcAgreement.getPlcParameter("mesConfirm");//确认字
            PlcParameter mesConfirmID =plcAgreement.getPlcParameter("mesConfirmID");//确认ID
            if (machine.getIsLog()>0){
            PlcParameter plcReport = plcAgreement.getPlcParameter("plcReport");//汇报字
            PlcParameter plcReportID = plcAgreement.getPlcParameter("plcReportID");//汇报ID
            PlcParameter mesConfirm = plcAgreement.getPlcParameter("mesConfirm");//确认字
            PlcParameter mesConfirmID = plcAgreement.getPlcParameter("mesConfirmID");//确认ID
            if (machine.getIsLog() > 0) {
                modbusTcp1.consoleLogInfo(plcAgreement);
            }
            if (0==plcRequest.getValueInt()&&1==mesSend.getValueInt()) {
            if (0 == plcRequest.getValueInt() && 1 == mesSend.getValueInt()) {
                //发送字置0
                String send= HexUtil.intTo2ByteHex(0);
                modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()/2));
                log.info("1.发送字置零 发送字 清空成功;内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()));
                String send = HexUtil.intTo2ByteHex(0);
                modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesSend.getAddressStart() / 2));
                log.info("1.发送字置零 发送字 清空成功;内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesSend.getAddressStart()));
                modbusTcp1.close();
                return;
            }
            if (0==plcReport.getValueInt()&&0!=mesConfirm.getValueInt()) {
            if (0 == plcReport.getValueInt() && 0 != mesConfirm.getValueInt()) {
                //确认字置零
                String send=HexUtil.intTo2ByteHex(0);
                modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()/2));
                log.info("2.确认字置零 任务完成 清空成功;内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
                String send = HexUtil.intTo2ByteHex(0);
                modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
                log.info("2.确认字置零 任务完成 清空成功;内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
                modbusTcp1.close();
                return;
            }
            if(1==plcReport.getValueInt()&&0==mesConfirm.getValueInt()){
                int finishCount=plcReportID.getValueInt();
                if(finishCount>0) {//有任务
                    if(machine.getMode()==1){
            if (1 == plcReport.getValueInt() && 0 == mesConfirm.getValueInt()) {
                int finishCount = plcReportID.getValueInt();
                if (finishCount > 0) {//有任务
                    if (machine.getMode() == 1) {
                        //标准模式  找已领取未完成的第一条任务
                        LineConfiguration lineConfiguration=lineConfigurationService.machineLineConfiguration(machine);
                        List<PrimitiveTask> findPrimitiveTasking=primitiveTaskService.findPrimitiveTasking(machine,lineConfiguration.getLineId());
                        for(PrimitiveTask primitiveTask:findPrimitiveTasking){
                            primitiveTask.setLoadCount(primitiveTask.getLoadCount()+1);
                        LineConfiguration lineConfiguration = lineConfigurationService.machineLineConfiguration(machine);
                        List<PrimitiveTask> findPrimitiveTasking = primitiveTaskService.findPrimitiveTasking(machine, lineConfiguration.getLineId());
                        for (PrimitiveTask primitiveTask : findPrimitiveTasking) {
                            primitiveTask.setLoadCount(primitiveTask.getLoadCount() + 1);
                            primitiveTaskMapper.updateById(primitiveTask);
                            break;
                        }
                    }else if(machine.getMode()==3){
                        machine.setFinshCount(machine.getFinshCount()+plcReportID.getValueInt());
                    } else if (machine.getMode() == 3) {
                        machine.setFinshCount(machine.getFinshCount() + plcReportID.getValueInt());
                        machineMapper.updateById(machine);
                    }
                    String send = HexUtil.intTo2ByteHex(1);
                    modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()/ 2));
                    modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart() / 2));
                    log.info("任务完成 成功 内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesConfirm.getAddressStart()));
                    modbusTcp1.close();
                    return;
@@ -119,41 +126,46 @@
                modbusTcp1.close();
                return;
            }
            if((1==plcRequest.getValueInt()&&0==mesSend.getValueInt())
                    &&"开工".equals(machine.getState())){
                int loadCount=0;
                if(machine.getMode()==1){
            if ((1 == plcRequest.getValueInt() && 0 == mesSend.getValueInt())
                    && "开工".equals(machine.getState())) {
                int loadCount = 0;
                if (machine.getMode() == 1) {
                    //标准模式  找已领取未完成的第一条任务
                    LineConfiguration lineConfiguration=lineConfigurationService.machineLineConfiguration(machine);
                    List<PrimitiveTask> findPrimitiveTasking=primitiveTaskService.findPrimitiveTasking(machine,lineConfiguration.getLineId());
                    for(PrimitiveTask primitiveTask:findPrimitiveTasking){
                    LineConfiguration lineConfiguration = lineConfigurationService.machineLineConfiguration(machine);
                    List<PrimitiveTask> findPrimitiveTasking = primitiveTaskService.findPrimitiveTasking(machine, lineConfiguration.getLineId());
                    for (PrimitiveTask primitiveTask : findPrimitiveTasking) {
                        //任务数-已上数量>0  返回1
                        if((primitiveTask.getTaskQuantity()-primitiveTask.getLoadCount())>0){
                            loadCount=1;
                        if ((primitiveTask.getTaskQuantity() - primitiveTask.getLoadCount()) > 0) {
                            loadCount = 1;
                        }
                        break;
                    }
                }else if(machine.getMode()==2){
                } else if (machine.getMode() == 2) {
                    //定制模式
                    loadCount=1;
                }else if (machine.getMode()==3){
                    loadCount=machine.getTaskCount()-machine.getFinshCount();
                    loadCount=(loadCount>0?1:0);
                    loadCount = 1;
                } else if (machine.getMode() == 3) {
                    loadCount = machine.getTaskCount() - machine.getFinshCount();
                    loadCount = (loadCount > 0 ? 1 : 0);
                }
                if(loadCount>0){
                    log.info("4、发送任务  任务数据:{}, 设备状态:{}, ",loadCount,machine);
                    String send=HexUtil.intTo2ByteHex(1)+ HexUtil.intTo2ByteHex(loadCount);
                    modbusTcp1.writeString(send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()/2));
                    log.info("发送任务发送报文内容:{},发送起始地址:{}",send,HexUtil.intTo2ByteHex(mesSend.getAddressStart()/2));
                if (loadCount > 0) {
                    log.info("4、发送任务  任务数据:{}, 设备状态:{}, ", loadCount, machine);
                    String send = HexUtil.intTo2ByteHex(1) + HexUtil.intTo2ByteHex(loadCount);
                    modbusTcp1.writeString(send, HexUtil.intTo2ByteHex(mesSend.getAddressStart() / 2));
                    log.info("发送任务发送报文内容:{},发送起始地址:{}", send, HexUtil.intTo2ByteHex(mesSend.getAddressStart() / 2));
                    modbusTcp1.close();
                    return;
                }
            }
        } catch (Exception e) {
            log.info("交互逻辑错误");
        } finally {
            // 确保连接关闭
            if (modbusTcp1 != null) {
                modbusTcp1.close();
            }
        }
        modbusTcp1.close();
    }
    @Override
    public void run() {
        while (this != null) {