| | |
| | | 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() { |
| | | while (this != null) { |
| | | try { |
| | | // 发送一次消息 |
| | | sendMessages(); |
| | | |
| | | // 延迟一段时间 |
| | | Thread.sleep(SEND_INTERVAL); |
| | | |
| | | while (true) { |
| | | // 写入PLC |
| | | writeToPlc(); |
| | | Thread.sleep(WRITE_INTERVAL); |
| | | |
| | | // 发送消息 |
| | | sendMessages(); |
| | | Thread.sleep(SEND_INTERVAL); |
| | | } |
| | | |
| | | Thread.sleep(1000); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | private void sendMessages() { |
| | | 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", 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) { |
| | | 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(); |
| | |
| | | messageValues.add(value); |
| | | } catch (NumberFormatException e) { |
| | | // 如果无法解析为 short 类型,则忽略该部分 |
| | | |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | } |
| | | } |