| | |
| | | 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.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Arrays; |
| | | import java.io.BufferedReader; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.io.UnsupportedEncodingException; |
| | | |
| | | |
| | | import org.apache.commons.io.FileUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | @Component |
| | | public class PlcParameter extends Thread { |
| | | |
| | | // private final MessageSender messageSender; |
| | | // |
| | | // public PlcParameter(MessageSender messageSender) { |
| | | // this.messageSender = messageSender; |
| | | // } |
| | | |
| | | @Override |
| | | public void run() { |
| | | while (this != null) { |
| | | try { |
| | | // try { |
| | | // RabbitMQUtils.sendMessage("Hello, world!", "module_queue"); |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // try { |
| | | // RabbitMQUtils.receiveMessage("module_queue"); |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // String message = "Hello, RabbitMQ!"; |
| | | // messageSender.sendMessage(message); |
| | | Thread.sleep(500); |
| | | |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("key1", "value1"); |
| | | jsonObject.put("key2", 123); |
| | | JSONArray jsonArray = new JSONArray(); |
| | | jsonArray.add("item1"); |
| | | jsonArray.add("item2"); |
| | | jsonObject.put("key3", jsonArray); |
| | | |
| | | try { |
| | | // 将 JSON 对象转换为字符串并发送到队列//RabbitMQUtils.sendJsonMessage(jsonObject, "json_queue"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | try { |
| | | // 从队列接收并解析 JSON 消息为 JSONObject 对象 |
| | | JSONObject receivedJsonObject = RabbitMQUtils.receiveJsonMessage(JSONObject.class, "json_queue"); |
| | | System.out.println(receivedJsonObject.toString()); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | } |