wu
2023-12-18 37f7f87968cfd2c400283fbf83851c39c2c4d456
springboot-vue3/src/main/java/com/example/springboot/component/PlcParameter1.java
@@ -10,52 +10,8 @@
import java.util.List;
public class PlcParameter1 extends Thread {
  String name = "";
  Integer count = 0;
  public static String readFileToString(String filePath) throws IOException {
    File file = new File(filePath);
    return FileUtils.readFileToString(file, "UTF-8");
  }
  public void readValue() {
    String str = "";
    BufferedReader bufferedReader = null;
    FileInputStream fileInputStream;
    try {
      // 从文件中读取字节数据存入 fileInputStream
      fileInputStream = new FileInputStream("CanadaMes-ui/src/configuration/address.json");
      // 读取 fileInputStream 中字节并将其解码为字符
      InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
      // 提高读取效率,在 BufferedReader 内包装 InputStreamReader
      bufferedReader = new BufferedReader(inputStreamReader);
      String line = null;
      // 将 bufferedReader 内容一行一行赋值给str
      while ((line = bufferedReader.readLine()) != null) {
        str += line;
      }
      // 将str字符串格式转为json
      JSONObject jsonObject = new JSONObject(str);
      // 获取json中的值
      JSONArray address = jsonObject.getJSONArray("address");
      for (int i = 0; i < address.size(); i++) {
        JSONObject ress = (JSONObject) address.get(i);
        this.name = ress.getStr("name");
        this.count = ress.getInt("count");
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
    MessageHandler customS7Control = new MessageHandler();
  @Override
  public void run() {
@@ -66,9 +22,7 @@
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      // this.readValue();
      // String PlcAddress = this.name;
      // Integer Plccount = this.count;
      // System.out.println(stt);
      List<String> addresses = new ArrayList<>();
@@ -159,38 +113,12 @@
              }
              // 写入第一个地址
              if (messageArray.getJSONArray(3).size() > 0) {
                Object value = messageArray.getJSONArray(3).get(0);
                if (value != null && !value.toString().equals("null")) {
                  try {
                    String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
                    short sValue = Short.parseShort(cleanedValue.trim());
                    S7control.getinstance().WriteWord(addressList2, Arrays.asList(sValue));
                    System.out
                            .println("messageValue:" + Arrays.asList(sValue) + " written to PLC at address " + addressList2);
                  } catch (NumberFormatException e) {
                    // 如果无法解析为 short 类型,则忽略该部分
                    System.err.println("Could not parse value: " + value);
                  }
                }
              }
                            customS7Control.WriteWordsToPLC(messageArray, addressList2, 3);
              // 写入第二个地址
              if (messageArray.getJSONArray(4).size() > 0) {
                Object value = messageArray.getJSONArray(4).get(0);
                if (value != null && !value.toString().equals("null")) {
                  try {
                    String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
                    short sValue = Short.parseShort(cleanedValue.trim());
                    S7control.getinstance().WriteWord(addressList3, Arrays.asList(sValue));
                    System.out
                            .println("messageValue:" + Arrays.asList(sValue) + " written to PLC at address " + addressList3);
                  } catch (NumberFormatException e) {
                    // 如果无法解析为 short 类型,则忽略该部分
                    System.err.println("Could not parse value: " + value);
                  }
                }
              }
                            customS7Control.WriteWordsToPLC(messageArray, addressList3, 4);
              // 写入第三个地址
              if (!mergedList.isEmpty()) {