| | |
| | | import java.util.Arrays;
|
| | | import java.util.List;
|
| | |
|
| | |
|
| | | public class Plcaction extends Thread {
|
| | | private static final long SEND_INTERVAL = 2000; // 发送消息的时间间隔
|
| | | private static final long WRITE_INTERVAL = 100; // 写入PLC的时间间隔
|
| | |
|
| | | @Override
|
| | | public void run() {
|
| | | try {
|
| | | // 发送一次消息
|
| | | sendMessages();
|
| | |
|
| | | // 延迟一段时间
|
| | | Thread.sleep(SEND_INTERVAL);
|
| | |
|
| | | while (true) {
|
| | | // 写入PLC
|
| | | writeToPlc();
|
| | | Thread.sleep(WRITE_INTERVAL);
|
| | |
|
| | | // 发送消息
|
| | | sendMessages();
|
| | | Thread.sleep(SEND_INTERVAL);
|
| | | while (this != null) {
|
| | | try {
|
| | | Thread.sleep(1000);
|
| | | } catch (InterruptedException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | } catch (InterruptedException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | | // List<Boolean> paramlist = S7control.getinstance().ReadBits("DB100.DBW", 12);
|
| | | ////
|
| | | // JSONObject jsonObject = new JSONObject();
|
| | | // jsonObject.append("params", new short[]{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0});
|
| | |
|
| | | private void sendMessages() {
|
| | | WebSocketServer sendwServer = WebSocketServer.sessionMap.get("action");
|
| | | if (sendwServer != null) {
|
| | | // List<Boolean> paramlist = S7control.getinstance().ReadBits("DB100.DBW", 12);
|
| | | Boolean[] values = {false, true,false, true,false, true,false, true,false, true,false, true,false, true,false, true,false, true,false, true,false, true};
|
| | | List<Boolean> paramlist = new ArrayList<>(Arrays.asList(values));
|
| | | if (paramlist == null) {
|
| | |
|
| | | List<Boolean> paramlist = S7control.getinstance().ReadBits("DB100.DBW", 12);
|
| | | // new short[]{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
|
| | | // 1, 0}
|
| | | JSONObject jsonObject = new JSONObject();
|
| | | jsonObject.append("params",
|
| | | new short[] { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 });
|
| | | } else {
|
| | | short[] params = new short[paramlist.size()];
|
| | |
|
| | | sendwServer.sendMessage(jsonObject.toString());
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | private void writeToPlc() {
|
| | | WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("action");
|
| | | if (webSocketServer != null) {
|
| | | List<String> messages = webSocketServer.getMessages();
|
| | |
|
| | | String addressList = "DB100.DBW0";
|
| | |
|
| | | 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();
|
| | | }
|
| | | for (int i = 0; i < paramlist.size(); i++) {
|
| | | boolean value = paramlist.get(i);
|
| | | params[i] = value ? (short) 1 : (short) 0;
|
| | | }
|
| | | <<<<<<< HEAD
|
| | | }
|
| | |
|
| | |
|
| | | private void sendMessages() {
|
| | | JSONObject jsonObject = new JSONObject();
|
| | | jsonObject.append("params", params);
|
| | | WebSocketServer sendwServer = WebSocketServer.sessionMap.get("action");
|
| | | if (sendwServer != null) {
|
| | |
|
| | | List<Boolean> paramlist = S7control.getinstance().ReadBits("DB100.DBW", 12);
|
| | | List<Boolean> paramlist2 = new ArrayList<>(Arrays.asList(true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false));
|
| | |
|
| | |
|
| | | List<Integer> binaryList = new ArrayList<>();
|
| | |
|
| | | for (Boolean value : paramlist2) {
|
| | | int binaryValue = value ? 1 : 0;
|
| | | binaryList.add(binaryValue);
|
| | | }
|
| | | // new short[]{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0}
|
| | | JSONObject jsonObject = new JSONObject();
|
| | | jsonObject.append("params", binaryList);
|
| | | =======
|
| | | >>>>>>> 36acd690614dd276428530a59f448c543bac52d4
|
| | |
|
| | | // 将消息值转换为布尔列表
|
| | | List<Boolean> messageBooleans = new ArrayList<>();
|
| | | for (Integer value : messageValues) {
|
| | | messageBooleans.add(value == 0 ? true : false);
|
| | | sendwServer.sendMessage(jsonObject.toString());
|
| | | }
|
| | | }
|
| | |
|
| | | // 将布尔列表写入 PLC
|
| | | S7control.getinstance().WriteBit(addressList, messageBooleans);
|
| | | System.out.println("messageValues:" + messageBooleans);
|
| | | System.out.println("addressList:" + addressList);
|
| | |
|
| | | // 清空消息列表
|
| | | webSocketServer.clearMessages();
|
| | | WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("action");
|
| | | if (webSocketServer != null) {
|
| | |
|
| | | List<String> messages = webSocketServer.getMessages();
|
| | |
|
| | | String addressList = "DB100.DBW0";
|
| | |
|
| | | 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 == 0 ? true : false);
|
| | | }
|
| | |
|
| | | // 将布尔列表写入 PLC
|
| | | S7control.getinstance().WriteBit(addressList, messageBooleans);
|
| | | System.out.println("messageValues:" + messageBooleans);
|
| | | System.out.println("addressList:" + addressList);
|
| | | // 清空消息列表
|
| | | webSocketServer.clearMessages();
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | private List<Boolean> paramList() {
|
| | | // 获取参数列表的逻辑
|
| | |
|
| | | return new ArrayList<>(); // 假设返回空列表
|
| | | }
|
| | | } |
| | | }
|