| | |
| | | import java.util.List;
|
| | |
|
| | | public class PLCManualJog 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");
|
| | |
|
| | | // 用于存储应用程序的配置信息
|
| | | private Configuration config;
|
| | |
|
| | | // 创建一个自定义的 S7 控制器消息处理器对象
|
| | | MessageHandler customS7Control = new MessageHandler();
|
| | |
|
| | |
|
| | | public PLCManualJog() 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;
|
| | | }
|
| | |
|
| | | // 将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();
|
| | | }
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void run() {
|
| | |
| | | // Integer Plccount = this.count;
|
| | |
|
| | | // System.out.println(stt);
|
| | | List<Boolean> arraylist = S7control.getinstance().ReadBits("DB101.0.0", 16);
|
| | | List<Boolean> arraylist2 = S7control.getinstance().ReadBits("DB101.2.6", 8);
|
| | | List<Boolean> arraylist3 = S7control.getinstance().ReadBits("DB101.5.2", 2);
|
| | |
|
| | |
|
| | | List<String> addressList21 = Arrays.asList(config.getProperty("PLCManualJog.button").split(","));
|
| | |
|
| | |
|
| | |
|
| | |
|
| | | List<Boolean> arraylist = S7control.getinstance().readBits(addressList21);
|
| | |
|
| | | // Boolean[] values1 = { false, true, true, true, false, false, true, false,
|
| | | // false, true, true, true, false, false,
|
| | | // true, false };
|
| | |
| | | // Boolean[] values3 = { false, false };
|
| | | // List<Boolean> arraylist3 = new ArrayList<>(Arrays.asList(values3));
|
| | |
|
| | | arraylist.addAll(arraylist2);
|
| | | arraylist.addAll(arraylist3);
|
| | |
|
| | |
|
| | | JSONObject jsonObject = new JSONObject();
|
| | |
|
| | |
| | |
|
| | | jsonObject.append("params", params);
|
| | |
|
| | | WebSocketServer sendwServer = WebSocketServer.sessionMap.get("ManualJog");
|
| | | // System.out.println("messageValue:" + arraylist +"messageValue2:" + arraylist2 );
|
| | | // WebSocketServer sendwServer = WebSocketServer.sessionMap.get("ManualJog");
|
| | | // if (sendwServer != null) {
|
| | | // sendwServer.sendMessage(jsonObject.toString());
|
| | | // }
|
| | |
|
| | | ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("ManualJog");
|
| | | if (sendwServer != null) {
|
| | | sendwServer.sendMessage(jsonObject.toString());
|
| | | }
|
| | | for (WebSocketServer webserver : sendwServer) {
|
| | | webserver.sendMessage(jsonObject.toString());
|
| | |
|
| | | WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("ManualJog");
|
| | | // WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("ManualJog");
|
| | |
|
| | | if (webSocketServer != null) {
|
| | | String addressList1 = "DB101.0.0";
|
| | | String addressList2 = "DB101.2.6";
|
| | | String addressList3 = "DB101.5.2";
|
| | | if (webserver != null) {
|
| | |
|
| | | List<String> messages = webSocketServer.getMessages();
|
| | | if (!messages.isEmpty()) {
|
| | | // 将最后一个消息转换为整数类型的列表
|
| | | String lastMessage = messages.get(messages.size() - 1);
|
| | | // System.out.println("messages:" + messages);
|
| | | String[] parts = lastMessage.split(",");
|
| | | List<Integer> messageValues = new ArrayList<>();
|
| | | for (String part : parts) {
|
| | | try {
|
| | | // 使用正则表达式清除非数字字符
|
| | | String cleanedPart = part.replaceAll("[^0-9-]", "");
|
| | | Integer value = Integer.parseInt(cleanedPart.trim());
|
| | | messageValues.add(value);
|
| | | } catch (NumberFormatException e) {
|
| | | // 如果无法解析为整数类型,则忽略该部分
|
| | | // e.printStackTrace();
|
| | |
|
| | |
|
| | | List<String> messages = webserver.getMessages();
|
| | | if (!messages.isEmpty()) {
|
| | | // 将最后一个消息转换为整数类型的列表
|
| | | String lastMessage = messages.get(messages.size() - 1);
|
| | | // System.out.println("messages:" + messages);
|
| | | String[] parts = lastMessage.split(",");
|
| | | List<Integer> messageValues = new ArrayList<>();
|
| | | for (String part : parts) {
|
| | | try {
|
| | | // 使用正则表达式清除非数字字符
|
| | | String cleanedPart = part.replaceAll("[^0-9-]", "");
|
| | | Integer value = Integer.parseInt(cleanedPart.trim());
|
| | | messageValues.add(value);
|
| | | } catch (NumberFormatException e) {
|
| | | // 如果无法解析为整数类型,则忽略该部分
|
| | | // e.printStackTrace();
|
| | | }
|
| | |
|
| | | }
|
| | | // 将消息值转换为布尔列表
|
| | | List<Boolean> messageBooleans = new ArrayList<>();
|
| | | for (Integer value : messageValues) {
|
| | | messageBooleans.add(value == 1 ? true : false);
|
| | | }
|
| | | // System.out.println(messageBooleans);
|
| | | List<Boolean> bolList = new ArrayList<>();
|
| | |
|
| | |
|
| | | for (int i = 0; i < 26 && i < messageBooleans.size(); i++) {
|
| | | bolList.add(messageBooleans.get(i));
|
| | | }
|
| | |
|
| | |
|
| | | if (!bolList.isEmpty()) {
|
| | | S7control.getinstance().WriteBit(addressList21, bolList);
|
| | | System.out.println("messageValue:" + bolList + " written to PLC at address " + addressList21);
|
| | | }
|
| | |
|
| | | webserver.clearMessages();
|
| | | }
|
| | |
|
| | | }
|
| | | // 将消息值转换为布尔列表
|
| | | List<Boolean> messageBooleans = new ArrayList<>();
|
| | | for (Integer value : messageValues) {
|
| | | messageBooleans.add(value == 1 ? true : false);
|
| | | }
|
| | | // System.out.println(messageBooleans);
|
| | | List<Boolean> bolList = new ArrayList<>();
|
| | | List<Boolean> bolList2 = new ArrayList<>();
|
| | | List<Boolean> bolList3 = new ArrayList<>();
|
| | |
|
| | | for (int i = 0; i < 16 && i < messageBooleans.size(); i++) {
|
| | | bolList.add(messageBooleans.get(i));
|
| | | }
|
| | |
|
| | | for (int i = 16; i < 24 && i < messageBooleans.size(); i++) {
|
| | | bolList2.add(messageBooleans.get(i));
|
| | | }
|
| | |
|
| | | for (int i = 24; i < 26 && i < messageBooleans.size(); i++) {
|
| | | bolList3.add(messageBooleans.get(i));
|
| | | }
|
| | |
|
| | | // System.out.println(bolList);
|
| | | // System.out.println(bolList2);
|
| | | // System.out.println(bolList3);
|
| | | if (!bolList.isEmpty()) {
|
| | | S7control.getinstance().WriteBit(addressList1, bolList);
|
| | | System.out.println("messageValue:" + bolList + " written to PLC at address " + addressList1);
|
| | | }
|
| | | if (!bolList2.isEmpty()) {
|
| | | S7control.getinstance().WriteBit(addressList2, bolList2);
|
| | | System.out.println("messageValue:" + bolList2 + " written to PLC at address " + addressList2);
|
| | | }
|
| | | if (!bolList3.isEmpty()) {
|
| | | S7control.getinstance().WriteBit(addressList3, bolList3);
|
| | | System.out.println("messageValue:" + bolList3 + " written to PLC at address " + addressList3);
|
| | | }
|
| | | }
|
| | |
|