ZengTao
2024-04-29 7687eb6cc76ab8ca20b39e7567255939abc4ff66
springboot-vue3/src/main/java/com/example/springboot/component/PlcParameter2.java
@@ -14,7 +14,6 @@
import java.util.Collections;
import java.util.List;
public class PlcParameter2 extends Thread {
    @Autowired
    StorageCageService storageCageService;
@@ -22,7 +21,6 @@
    MessageHandler customS7Control = new MessageHandler();
    // 用于存储应用程序的配置信息
    private Configuration config;
    public PlcParameter2() throws IOException {
        config = new Configuration("config.properties");
@@ -33,10 +31,6 @@
        while (this != null) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            storageCageService = WebSocketServer.applicationContext.getBean(StorageCageService.class);
            //  id
            List<String> addressList1 = Arrays.asList(config.getProperty("Parameter2.id").split(","));
@@ -63,19 +57,20 @@
            List<String> addresses4 = Arrays.asList(config.getProperty("Parameter2.Currentcell").split(","));
            List<Short> data4 = S7control.getinstance().readWords(addresses4);
            //System.out.println(data);
//      List<Long> TIME2 = Collections.singletonList(S7control.getinstance().readtime("DB100.194"));
                // List<Long> TIME2 =
                // Collections.singletonList(S7control.getinstance().readtime("DB100.194"));
//      System.out.println("addressList:" + TIME2);
            //
//
//      String[] values6 = { ".x1 ","x2 ","x3",";;x4","x5","x6","x7","x8","x9","x10","x11"};
                // String[] values6 = { ".x1 ","x2
                // ","x3",";;x4","x5","x6","x7","x8","x9","x10","x11"};
//      List<String> paramlist = new ArrayList<>(Arrays.asList(values6));
//      Short[] values1 = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1};
//     List<Short> data = new ArrayList<>(Arrays.asList(values1));
//      Boolean[] value4 = { false, false, false, false, false, false, false, false, false,
                // Boolean[] value4 = { false, false, false, false, false, false, false, false,
                // false,
//              false};
//     List<Boolean> data2 = new ArrayList<>(Arrays.asList(value4));
//      String[] values3 = { ".x11 ","x21 "};
@@ -108,8 +103,8 @@
            jsonObject.append("jiting", anniuparams);
            jsonObject.append("timeout", readtimeout);
//      WebSocketServer sendwServer = WebSocketServer.getWebSocketServer("Parameter2");
                // WebSocketServer sendwServer =
                // WebSocketServer.getWebSocketServer("Parameter2");
//      if (sendwServer != null) {
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.set("message", "Hello, Parameter2!");
@@ -122,7 +117,6 @@
                    webserver.sendMessage(jsonObject.toString());
                    if (webserver != null) {
                        List<String> messages = webserver.getMessages();
@@ -134,7 +128,6 @@
                            JSONArray messageArray = new JSONArray(lastMessage);
                            //清除ID
                            // customS7Control.writeBitToPLC(messageArray, addressList0, 0);
                            try {
                                // 检查索引是否有效
@@ -156,8 +149,9 @@
                                                boolean val = "1".equals(cleanedValue.trim());
                                                // 将布尔值添加到布尔值列表中
                                                sValue.add(val);
                                                System.out.println("messageValue: " + Arrays.asList(val) + " added to the list");
                                                    System.out.println(
                                                            "messageValue: " + Arrays.asList(val)
                                                                    + " added to the list");
                                            } catch (NumberFormatException e) {
                                                // 如果无法解析为 boolean 类型,则忽略该部分
@@ -194,13 +188,14 @@
                                                }
                                            }
                                        // 检查布尔值列表是否为空
                                        if (!sValue.isEmpty()) {
                                            // 调用 S7control.getinstance().WriteBit 方法将布尔值列表写入地址列表
                                            S7control.getinstance().WriteBit(addressList0, sValue);
                                            System.out.println("Values " + sValue + " written to PLC at address " + addressList0);
                                                    System.out
                                                            .println("Values " + sValue + " written to PLC at address "
                                                                    + addressList0);
                                        }
                                        }
                                    }
@@ -208,7 +203,6 @@
                            } catch (Exception e) {
                                System.err.println("An error occurred while writing bit to PLC: " + e.getMessage());
                            }
                            //急停
//                            customS7Control.writeBitToPLC(messageArray, niuanaddressList3, 1);
@@ -233,8 +227,9 @@
                                                boolean val = "1".equals(cleanedValue.trim());
                                                // 将布尔值添加到布尔值列表中
                                                sValue.add(val);
                                                System.out.println("messageValue: " + Arrays.asList(val) + " added to the list");
                                                    System.out.println(
                                                            "messageValue: " + Arrays.asList(val)
                                                                    + " added to the list");
                                            } catch (NumberFormatException e) {
                                                // 如果无法解析为 boolean 类型,则忽略该部分
@@ -259,17 +254,17 @@
                                        // 调用 S7control.getinstance().WriteBit 方法将布尔值列表写入地址列表
                                        S7control.getinstance().WriteBit(niuanaddressList3, sValue);
                                        System.out.println("Values " + sValue + " written to PLC at address " + niuanaddressList3);
                                            System.out.println(
                                                    "Values " + sValue + " written to PLC at address "
                                                            + niuanaddressList3);
                                    }
                                }
                            } catch (Exception e) {
                                System.err.println("An error occurred while writing bit to PLC: " + e.getMessage());
                            }
                            //存片等待延迟
                            customS7Control.Writetime(messageArray, timeout2, 2);
                            // 清空消息列表
                            webserver.clearMessages();
@@ -277,7 +272,10 @@
                    }
                }
            }
        }
    }
            } catch (InterruptedException e) {
                e.printStackTrace();
}
        }
    }
}