| | |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | |
| | | |
| | | public class PlcParameter2 extends Thread { |
| | | @Autowired |
| | | StorageCageService storageCageService; |
| | |
| | | MessageHandler customS7Control = new MessageHandler(); |
| | | // 用于存储应用程序的配置信息 |
| | | private Configuration config; |
| | | |
| | | |
| | | public PlcParameter2() throws IOException { |
| | | config = new Configuration("config.properties"); |
| | |
| | | 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(",")); |
| | |
| | | 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 "}; |
| | |
| | | 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!"); |
| | |
| | | |
| | | webserver.sendMessage(jsonObject.toString()); |
| | | |
| | | |
| | | if (webserver != null) { |
| | | |
| | | List<String> messages = webserver.getMessages(); |
| | |
| | | JSONArray messageArray = new JSONArray(lastMessage); |
| | | //清除ID |
| | | // customS7Control.writeBitToPLC(messageArray, addressList0, 0); |
| | | |
| | | |
| | | try { |
| | | // 检查索引是否有效 |
| | |
| | | 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 类型,则忽略该部分 |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | // 检查布尔值列表是否为空 |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | |
| | | } catch (Exception e) { |
| | | System.err.println("An error occurred while writing bit to PLC: " + e.getMessage()); |
| | | } |
| | | |
| | | |
| | | //急停 |
| | | // customS7Control.writeBitToPLC(messageArray, niuanaddressList3, 1); |
| | |
| | | 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 类型,则忽略该部分 |
| | |
| | | |
| | | // 调用 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(); |
| | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |