| | |
| | | package com.example.springboot.component;
|
| | |
|
| | | import cn.hutool.json.JSONObject;
|
| | |
|
| | | import java.util.ArrayList;
|
| | | import java.util.List;
|
| | |
|
| | | public class PlcParameter extends Thread {
|
| | | private static final int SEND_INTERVAL = 10000;
|
| | | private static final int WRITE_INTERVAL = 100;
|
| | |
|
| | | @Override
|
| | |
|
| | | public void run() {
|
| | | try {
|
| | | // 发送一次消息
|
| | | sendMessages();
|
| | |
|
| | | // 延迟一段时间
|
| | | Thread.sleep(SEND_INTERVAL);
|
| | |
|
| | | while (true) {
|
| | | // 写入PLC
|
| | | writeToPlc();
|
| | | Thread.sleep(WRITE_INTERVAL);
|
| | |
|
| | | // 发送消息
|
| | | sendMessages();
|
| | | Thread.sleep(SEND_INTERVAL);
|
| | | }
|
| | |
|
| | | } catch (InterruptedException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | private void sendMessages() {
|
| | | WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Parameter");
|
| | | if (sendwServer != null) {
|
| | | List<Short> paramlist = S7control.getinstance().ReadWord("DB100.DBW", 12);
|
| | | JSONObject jsonObject = new JSONObject();
|
| | | // new short[]{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0}
|
| | | jsonObject.append("params", new short[]{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0});
|
| | | sendwServer.sendMessage(jsonObject.toString());
|
| | | }
|
| | | }
|
| | |
|
| | | private void writeToPlc() {
|
| | | WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("Parameter");
|
| | | if (webSocketServer != null) {
|
| | | List<String> messages = webSocketServer.getMessages();
|
| | |
|
| | | String addressList = "DB100.DBW0";
|
| | |
|
| | | if (!messages.isEmpty()) {
|
| | | // 将最后一个消息转换为 short 类型的列表
|
| | | String lastMessage = messages.get(messages.size() - 1);
|
| | | System.out.println("messages:" + messages);
|
| | | String[] parts = lastMessage.split(",");
|
| | | List<Short> messageValues = new ArrayList<>();
|
| | | for (String part : parts) {
|
| | | try {
|
| | | // 使用正则表达式清除非数字字符
|
| | | String cleanedPart = part.replaceAll("[^0-9-]", "");
|
| | | short value = Short.parseShort(cleanedPart.trim());
|
| | | messageValues.add(value);
|
| | | } catch (NumberFormatException e) {
|
| | | // 如果无法解析为 short 类型,则忽略该部分
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | | // 将消息值写入 PLC
|
| | | S7control.getinstance().WriteWord(addressList, messageValues);
|
| | | System.out.println("messageValues:" + messageValues);
|
| | | System.out.println("addressList:" + addressList);
|
| | | // 清空消息列表
|
| | | webSocketServer.clearMessages();
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | package com.example.springboot.component; |
| | | |
| | | import cn.hutool.json.JSONObject; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | public class PlcParameter extends Thread { |
| | | private static final int SEND_INTERVAL = 10000; |
| | | private static final int WRITE_INTERVAL = 100; |
| | | |
| | | @Override |
| | | |
| | | public void run() { |
| | | try { |
| | | // 发送一次消息 |
| | | sendMessages(); |
| | | |
| | | // 延迟一段时间 |
| | | Thread.sleep(SEND_INTERVAL); |
| | | |
| | | while (true) { |
| | | // 写入PLC |
| | | writeToPlc(); |
| | | Thread.sleep(WRITE_INTERVAL); |
| | | |
| | | // 发送消息 |
| | | sendMessages(); |
| | | Thread.sleep(SEND_INTERVAL); |
| | | } |
| | | |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | private void sendMessages() { |
| | | WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Parameter"); |
| | | if (sendwServer != null) { |
| | | List<Short> paramlist = S7control.getinstance().ReadWord("DB100.DBW", 12); |
| | | JSONObject jsonObject = new JSONObject(); |
| | | // new short[]{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0} |
| | | jsonObject.append("params", new short[]{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0}); |
| | | sendwServer.sendMessage(jsonObject.toString()); |
| | | } |
| | | } |
| | | |
| | | private void writeToPlc() { |
| | | WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("Parameter"); |
| | | if (webSocketServer != null) { |
| | | List<String> messages = webSocketServer.getMessages(); |
| | | |
| | | String addressList = "DB100.DBW0"; |
| | | |
| | | if (!messages.isEmpty()) { |
| | | // 将最后一个消息转换为 short 类型的列表 |
| | | String lastMessage = messages.get(messages.size() - 1); |
| | | System.out.println("messages:" + messages); |
| | | String[] parts = lastMessage.split(","); |
| | | List<Short> messageValues = new ArrayList<>(); |
| | | for (String part : parts) { |
| | | try { |
| | | // 使用正则表达式清除非数字字符 |
| | | String cleanedPart = part.replaceAll("[^0-9-]", ""); |
| | | short value = Short.parseShort(cleanedPart.trim()); |
| | | messageValues.add(value); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | // 将消息值写入 PLC |
| | | S7control.getinstance().WriteWord(addressList, messageValues); |
| | | System.out.println("messageValues:" + messageValues); |
| | | System.out.println("addressList:" + addressList); |
| | | // 清空消息列表 |
| | | webSocketServer.clearMessages(); |
| | | } |
| | | } |
| | | } |
| | | } |