| | |
| | | import java.util.List; |
| | | |
| | | public class PlcManualonePosition extends Thread { |
| | | String name = ""; |
| | | Integer count = 0; |
| | | private Configuration config; |
| | | |
| | | public static String readFileToString(String filePath) throws IOException { |
| | | File file = new File(filePath); |
| | | return FileUtils.readFileToString(file, "UTF-8"); |
| | | public PlcManualonePosition() throws IOException { |
| | | config = new Configuration("config.properties"); |
| | | } |
| | | |
| | | 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; |
| | | public static class PLCBooleanConverter { |
| | | |
| | | public static short[] convertListsToBinaryShortArray(List<Boolean>... lists) { |
| | | // 计算所有列表中元素的总数 |
| | | int totalSize = 0; |
| | | for (List<Boolean> list : lists) { |
| | | totalSize += list.size(); |
| | | } |
| | | |
| | | // 将str字符串格式转为json |
| | | JSONObject jsonObject = new JSONObject(str); |
| | | // 创建一个足够大的数组来存放所有的二进制值 |
| | | short[] binaryShortArray = new short[totalSize]; |
| | | |
| | | // 获取json中的值 |
| | | JSONArray address = jsonObject.getJSONArray("ManualonePosition"); |
| | | for (int i = 0; i < address.size(); i++) { |
| | | JSONObject ress = (JSONObject) address.get(i); |
| | | // 当前数组填充位置指针 |
| | | int currentIndex = 0; |
| | | |
| | | this.name = ress.getStr("name"); |
| | | this.count = ress.getInt("count"); |
| | | |
| | | // 遍历所有的列表 |
| | | for (List<Boolean> list : lists) { |
| | | // 将每个布尔值转换为 0 或 1 并填入数组 |
| | | for (Boolean value : list) { |
| | | binaryShortArray[currentIndex++] = (short) (value ? 1 : 0); |
| | | } |
| | | } |
| | | |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | return binaryShortArray; |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | @Override |
| | | public void run() { |
| | |
| | | addresses10.add("DB104.9.4"); |
| | | addresses10.add("DB104.9.5"); |
| | | List<Boolean> arraylist10 = S7control.getinstance().readBits(addresses10); |
| | | |
| | | //A01启动地址 |
| | | List<String> A01start = Arrays.asList(config.getProperty("mlp.A01start")); |
| | | //A02启动地址 |
| | | List<String> A02start = Arrays.asList(config.getProperty("mlp.A02start")); |
| | | |
| | | List<String> B01start = Arrays.asList(config.getProperty("mlp.B01start")); |
| | | |
| | | List<String> B02start = Arrays.asList(config.getProperty("mlp.B02start")); |
| | | |
| | | List<Boolean> A01readstart = S7control.getinstance().readBits(A01start); |
| | | List<Boolean> A02readstart = S7control.getinstance().readBits(A02start); |
| | | List<Boolean> B01readstart = S7control.getinstance().readBits(B01start); |
| | | List<Boolean> B02readstart = S7control.getinstance().readBits(B02start); |
| | | |
| | | short[] combinedBinaryShortArray = PlcManualonePosition.PLCBooleanConverter.convertListsToBinaryShortArray( |
| | | A01readstart, A02readstart,B01readstart, B02readstart |
| | | ); |
| | | |
| | | // Short[] values1 = { 1, 2, 3 }; |
| | | // List<Short> arraylist = new ArrayList<>(Arrays.asList(values1)); |
| | | // Short[] values2 = { 1, 2, 3 }; |
| | |
| | | jsonObject.append("shishi1", arraylist5); |
| | | jsonObject.append("guzhang", params2); |
| | | jsonObject.append("weihuiling", params3); |
| | | // jsonObject.append("weihuiling", arraylist8); |
| | | jsonObject.append("qidong", combinedBinaryShortArray); |
| | | |
| | | WebSocketServer sendwServer = WebSocketServer.sessionMap.get("ManualonePosition"); |
| | | if (sendwServer != null) { |
| | |
| | | } |
| | | if (!sValue.isEmpty()) { |
| | | S7control.getinstance().WriteWord(waddresses1, sValue); |
| | | if (A01readstart != null && !A01readstart.isEmpty()) { |
| | | // 创建一个新的列表用于写入新值 |
| | | List<Boolean> a01startval = new ArrayList<>(); |
| | | // 遍历所有读取到的位值 |
| | | for (Boolean bit : A01readstart) { |
| | | // 取反当前位的值并添加到新的列表中 |
| | | a01startval.add(!bit); |
| | | } |
| | | // 写入新的位值列表 |
| | | S7control.getinstance().WriteBit(A01start, a01startval); |
| | | } |
| | | |
| | | |
| | | |
| | | System.out.println("Values " + sValue + " written to PLC at address " + waddresses1); |
| | | } |
| | | } |
| | |
| | | } |
| | | if (!sValue2.isEmpty()) { |
| | | S7control.getinstance().WriteWord(waddresses2, sValue2); |
| | | if (A02readstart != null && !A02readstart.isEmpty()) { |
| | | // 创建一个新的列表用于写入新值 |
| | | List<Boolean> a02startval = new ArrayList<>(); |
| | | // 遍历所有读取到的位值 |
| | | for (Boolean bit : A02readstart) { |
| | | // 取反当前位的值并添加到新的列表中 |
| | | a02startval.add(!bit); |
| | | } |
| | | // 写入新的位值列表 |
| | | S7control.getinstance().WriteBit(A02start, a02startval); |
| | | } |
| | | |
| | | |
| | | |
| | | System.out.println("Values " + sValue2 + " written to PLC at address " + waddresses2); |
| | | } |
| | | } |
| | |
| | | } |
| | | if (!sValue2.isEmpty()) { |
| | | S7control.getinstance().WriteWord(waddresses3, sValue2); |
| | | if (B01readstart != null && !B01readstart.isEmpty()) { |
| | | // 创建一个新的列表用于写入新值 |
| | | List<Boolean> b01startval = new ArrayList<>(); |
| | | // 遍历所有读取到的位值 |
| | | for (Boolean bit : B01readstart) { |
| | | // 取反当前位的值并添加到新的列表中 |
| | | b01startval.add(!bit); |
| | | } |
| | | // 写入新的位值列表 |
| | | S7control.getinstance().WriteBit(B01start, b01startval); |
| | | } |
| | | System.out.println("Values " + sValue2 + " written to PLC at address " + waddresses3); |
| | | } |
| | | } |
| | |
| | | } |
| | | if (!sValue2.isEmpty()) { |
| | | S7control.getinstance().WriteWord(waddresses4, sValue2); |
| | | if (B02readstart != null && !B02readstart.isEmpty()) { |
| | | // 创建一个新的列表用于写入新值 |
| | | List<Boolean> b02startval = new ArrayList<>(); |
| | | // 遍历所有读取到的位值 |
| | | for (Boolean bit : B02readstart) { |
| | | // 取反当前位的值并添加到新的列表中 |
| | | b02startval.add(!bit); |
| | | } |
| | | // 写入新的位值列表 |
| | | S7control.getinstance().WriteBit(B02start, b02startval); |
| | | System.out.println("Values " + b02startval + " written to PLC at address " + B02start); |
| | | } |
| | | System.out.println("Values " + sValue2 + " written to PLC at address " + waddresses4); |
| | | } |
| | | } |