1、Plcaction.java PlcParameter.java 改为循环外读写
| | |
| | | background-color="#222f3e" text-color="#fff" active-text-color="#ffd04b" |
| | | :collapse="isCollapse" :unique-opened="true" |
| | | :collapse-transition="false" :router="true"> |
| | | <el-menu-item index="/" @click="goToHome"> |
| | | <!-- <el-menu-item index="/" @click="goToHome"> |
| | | <template slot="title"> |
| | | <!--图标--> |
| | | |
| | | <i class="el-icon-menu"></i> |
| | | <!--文本--> |
| | | |
| | | <span>{{ $t('home') }}</span> |
| | | </template> |
| | | </el-menu-item> |
| | | </el-menu-item> --> |
| | | <!--一级菜单--> |
| | | <el-submenu :index="item.id.toString()" v-for="item in menuList" :key="item.id"> |
| | | <!--一级菜单模板区域--> |
| | |
| | |
|
| | | .tag i {
|
| | | margin-left: 5px;
|
| | | font-size: 12px;
|
| | | font-size: 5px;
|
| | | cursor: pointer;
|
| | | }
|
| | | </style> |
| | |
| | | <!-- 面包屑导航区域 -->
|
| | | <el-breadcrumb separator-class="el-icon-arrow-right" class="el-breadcrumb">
|
| | | <router-link to="/home" tag="el-button" type="text">{{ $t('langHome') }}</router-link>
|
| | | <el-button type="text">设备管理</el-button>
|
| | | <el-button type="text">Electrical</el-button>
|
| | | <router-link to="/Electrical/Parameter" tag="el-button" type="text">Parameter</router-link>
|
| | | <router-link to="/Electrical/Action" tag="el-button" type="text">Action</router-link>
|
| | | <router-link to="/Electrical/Sign" tag="el-button" type="text">Sign</router-link>
|
| | |
| | |
|
| | | <el-form label-width="100px" style="display: flex; flex-wrap: wrap;" :model="messagepack.data">
|
| | | <div id="btn_div">
|
| | | <el-button type="primary" @click="send()">下发参数</el-button>
|
| | | <el-button type="primary" @click="send()" id="xiafa" :disabled="isButtonDisabled">下发参数</el-button>
|
| | | |
| | | </div>
|
| | | <div class="kuai_div" v-for="(item, index) in record.xyData" :key="index">
|
| | | <el-input style="width: 280px; border:none;" class="in_mc" v-model="item.name" readonly></el-input>
|
| | |
| | |
|
| | | created() {
|
| | | this.initWebSocket();
|
| | | this.isButtonDisabled = true;
|
| | | },
|
| | | methods: {
|
| | | initWebSocket() {
|
| | |
| | |
|
| | | // 收到消息
|
| | | socket.onmessage = (msg) => {
|
| | | if (!msg.data) {
|
| | | return; // 如果收到空数据,则直接返回,不执行后续逻辑
|
| | | }
|
| | | |
| | | let obj = JSON.parse(msg.data);
|
| | | this.record.params[0] = obj.params[0];
|
| | | |
| | | for (let a = 0; a <= this.record.params[0].length - 1; a++) {
|
| | | if (!this.record.xyData[a]) {
|
| | | this.record.xyData[a] = { name: this.record.canshu[a], value: this.record.params[0][a] };
|
| | | } else {
|
| | | this.record.xyData[a].value = this.record.params[0][a];
|
| | | }
|
| | | }
|
| | | |
| | | console.log(this.record.xyData);
|
| | | this.$forceUpdate();
|
| | | };
|
| | | if (!msg.data) {
|
| | | return; // 如果收到空数据,则直接返回,不执行后续逻辑
|
| | | }
|
| | |
|
| | | let obj = JSON.parse(msg.data);
|
| | | this.record.params[0] = obj.params[0];
|
| | | this.record.state = obj.state[0];
|
| | |
|
| | | let isButtonDisabled = this.record.state.includes(1); |
| | | // 判断state中是否存在1
|
| | | console.log(isButtonDisabled)
|
| | | this.isButtonDisabled = isButtonDisabled; // 将判断结果保存到变量isButtonDisabled中
|
| | |
|
| | | for (let a = 0; a <= this.record.params[0].length - 1; a++) {
|
| | | if (!this.record.xyData[a]) {
|
| | | this.record.xyData[a] = { name: this.record.canshu[a], value: this.record.params[0][a] };
|
| | | } else {
|
| | | this.record.xyData[a].value = this.record.params[0][a];
|
| | | }
|
| | | }
|
| | |
|
| | | console.log(this.record.xyData);
|
| | | this.$forceUpdate();
|
| | | };
|
| | |
|
| | | // 关闭事件
|
| | | socket.onclose = function () {
|
| | |
| | | package com.example.springboot.component; |
| | | |
| | | import cn.hutool.json.JSONObject; |
| | | import org.apache.shiro.authz.annotation.RequiresPermissions; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | public class PlcParameter extends Thread { |
| | | private static final int SEND_INTERVAL = 1000; |
| | | private static final int WRITE_INTERVAL = 100; |
| | | |
| | | private short[] params = new short[]{1111, 121, 3232, 0, 1, 0, 1, 0, 1, 0, 1, 0}; |
| | | List<Short> paramList = S7control.getinstance().ReadWord("DB100.DBW", 12); |
| | | |
| | | // 将列表中的值复制到 params 数组中 |
| | | |
| | | |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | // 发送一次消息 |
| | | // sendMessages(); |
| | | |
| | | // 延迟一段时间 |
| | | |
| | | |
| | | 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(); |
| | | } |
| | | } |
| | | |
| | | private void sendMessages() { |
| | | WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Parameter"); |
| | | if (sendwServer != null) { |
| | | List<Short> paramlist = S7control.getinstance().ReadWord("DB100.DBW", 12); |
| | | List<Short> state = S7control.getinstance().ReadWord("DB103.DBW0", 10); |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.append("params", params); |
| | | sendwServer.sendMessage(jsonObject.toString()); |
| | | } |
| | | } |
| | | jsonObject.append("params", new short[]{1111,121, 3232, 0, 1, 0, 1, 0, 1, 0, 1, 0}); |
| | | jsonObject.append("state", new short[]{0, 1, 0, 0, 0, 0, 0,0, 0, 0}); |
| | | WebSocketServer sendwServer = WebSocketServer.sessionMap.get("Parameter"); |
| | | if (sendwServer != null) { |
| | | sendwServer.sendMessage(jsonObject.toString()); |
| | | } |
| | | |
| | | private void writeToPlc() { |
| | | WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("Parameter"); |
| | | if (webSocketServer != null) { |
| | | List<String> messages = webSocketServer.getMessages(); |
| | | WebSocketServer webSocketServer = WebSocketServer.sessionMap.get("Parameter"); |
| | | if (webSocketServer != null) { |
| | | List<String> messages = webSocketServer.getMessages(); |
| | | |
| | | String addressList = "DB100.DBW0"; |
| | | 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(); |
| | | 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(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 替换初始变量的值 |
| | | for (int i = 0; i < messageValues.size(); i++) { |
| | | if (i < params.length) { |
| | | params[i] = messageValues.get(i); |
| | | } |
| | | // 将消息值写入 PLC |
| | | S7control.getinstance().WriteWord(addressList, messageValues); |
| | | System.out.println("messageValues:" + messageValues); |
| | | System.out.println("addressList:" + addressList); |
| | | // 清空消息列表 |
| | | webSocketServer.clearMessages(); |
| | | } |
| | | |
| | | // 将消息值写入 PLC |
| | | S7control.getinstance().WriteWord(addressList, messageValues); |
| | | System.out.println("messageValues:" + messageValues); |
| | | System.out.println("addressList:" + addressList); |
| | | // 清空消息列表 |
| | | webSocketServer.clearMessages(); |
| | | } |
| | | } |
| | | } |
| | |
| | | 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;
|
| | | }
|
| | |
|
| | |
|
| | | // 将消息值转换为布尔列表
|
| | | List<Boolean> messageBooleans = new ArrayList<>();
|
| | | for (Integer value : messageValues) {
|
| | | messageBooleans.add(value == 0 ? true : false);
|
| | | JSONObject jsonObject = new JSONObject();
|
| | | jsonObject.append("params", params);
|
| | | WebSocketServer sendwServer = WebSocketServer.sessionMap.get("action");
|
| | | if (sendwServer != null) {
|
| | | 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<>(); // 假设返回空列表
|
| | | }
|
| | | } |
| | | }
|
| | |
| | | for (short i = 0; i < shuzu1.length; i++) { |
| | | short result = alarmMapper.selectnullti(shuzu[i]); |
| | | if (shuzu1[i] == 1 && result == 0) { |
| | | alarmMapper.Insertalarm(shuzu[i]); |
| | | // alarmMapper.Insertalarm(shuzu[i]); |
| | | } else if (shuzu1[i] == 0 && result > 0) { |
| | | alarmMapper.updatealarm(shuzu[i]); |
| | | // alarmMapper.updatealarm(shuzu[i]); |
| | | } |
| | | } |
| | | |