| | |
| | | import java.util.List;
|
| | |
|
| | | public class PlcManualonePosition2 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 PlcManualonePosition2() 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("ServoManualone");
|
| | | 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() {
|
| | |
| | | params3[i] = value ? (short) 1 : (short) 0;
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | | //A01启动地址
|
| | | List<String> A01start = Arrays.asList(config.getProperty("mlp2.A01start"));
|
| | | //A02启动地址
|
| | | List<String> A02start = Arrays.asList(config.getProperty("mlp2.A02start"));
|
| | |
|
| | |
|
| | |
|
| | | List<Boolean> A01readstart = S7control.getinstance().readBits(A01start);
|
| | | List<Boolean> A02readstart = S7control.getinstance().readBits(A02start);
|
| | |
|
| | |
|
| | | short[] combinedBinaryShortArray = PlcManualonePosition2.PLCBooleanConverter.convertListsToBinaryShortArray(
|
| | | A01readstart, A02readstart
|
| | | );
|
| | |
|
| | | JSONObject jsonObject = new JSONObject();
|
| | |
|
| | | jsonObject.append("zuhe1", arraylist);
|
| | | jsonObject.append("zuhe2", arraylist2);
|
| | | jsonObject.append("guzhang", params2);
|
| | | jsonObject.append("weihuiling", params3);
|
| | | jsonObject.append("qidong", combinedBinaryShortArray);
|
| | | // System.out.println(jsonObject);
|
| | | WebSocketServer sendwServer = WebSocketServer.sessionMap.get("ManualonePosition2");
|
| | | 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);
|
| | | }
|
| | | }
|