ZengTao
2023-12-14 689b10dd99adf31b5e2737f5c949fc5c132f8001
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.example.springboot.component;
import cn.hutool.json.JSONArray;
import com.example.springboot.component.*;
import com.google.common.primitives.Bytes;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class MessageHandler {
    //写入byte
    public void outmesid(String glassid,String address) {
        try {
            List<Byte> glassidlist = new ArrayList<>();
            char ds[]=glassid.toCharArray();
            for (char iditem : ds) {
                glassidlist.add((byte)iditem);
            }
            byte[] bytes = Bytes.toArray(glassidlist);
            System.out.println("outmesidbytes:" + bytes.length);
            S7control.getinstance().WriteByte(address, bytes);
        } catch (Exception e) {
            System.err.println("An error occurred while writing byte to PLC: " + e.getMessage());
        }
    }
 
    //写入bit
    public void writeBitToPLC(JSONArray messageArray, List<String> addresses, int index) {
        try {
            // 检查索引是否有效
            if (messageArray.getJSONArray(index).size() > 0) {
                // 获取消息数组
                JSONArray jsonArray = messageArray.getJSONArray(index);
                // 创建一个布尔值列表
                List<Boolean> sValue = new ArrayList<>();
                // 遍历消息数组
                for (int i = 0; i < jsonArray.size(); i++) {
                    // 获取消息数组中的值
                    Object value = jsonArray.get(i);
                    // 检查值是否有效
                    if (value != null && !value.toString().equals("null")) {
                        try {
                            // 移除非数字和数字字符
                            String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
                            // 解析为布尔值
                            boolean val = "1".equals(cleanedValue.trim());
                            // 将布尔值添加到布尔值列表中
                            sValue.add(val);
                            System.out.println("messageValue: " + Arrays.asList(val) + " added to the list");
                        } catch (NumberFormatException e) {
                            // 如果无法解析为 boolean 类型,则忽略该部分
                            System.err.println("Could not parse value: " + value);
                        }
                    }
                }
                // 检查布尔值列表是否为空
                if (!sValue.isEmpty()) {
 
                    // 调用 S7control.getinstance().WriteBit 方法将布尔值列表写入地址列表
                    S7control.getinstance().WriteBit(addresses, sValue);
                    System.out.println("Values " + sValue + " written to PLC at address " + addresses);
                }
            }
        } catch (Exception e) {
            System.err.println("An error occurred while writing bit to PLC: " + e.getMessage());
        }
    }
 
    //不连续地址 写入Word
    public void WriteWordToPLC(JSONArray messageArray, List<String> addresses, int index) {
        try {
            if (messageArray.getJSONArray(index).size() > 0) {
                JSONArray jsonArray = messageArray.getJSONArray(index);
                List<Short> sValues = new ArrayList<>();
                for (int i = 0; i < jsonArray.size(); i++) {
                    Object value = jsonArray.get(i);
                    if (value != null && !value.toString().equals("null")) {
                        try {
                            String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
                            short val = Short.parseShort(cleanedValue.trim());
                            sValues.add(val);
                            System.out.println("messageValue:" + Arrays.asList(val) + " added to the list");
                        } catch (NumberFormatException e) {
                            System.err.println("Could not parse value: " + value);
                        }
                    }
                }
                if (!sValues.isEmpty()) {
                    S7control.getinstance().WriteWord(addresses, sValues);
                    System.out.println("Values " + sValues + " written to PLC at address " + addresses);
                }
            }
        } catch (Exception e) {
            System.err.println("An error occurred while writing word to PLC: " + e.getMessage());
        }
    }
 
 
 
    public void WriteWordsToPLC(JSONArray jsonArray,  String address,int index) {
        if (jsonArray.getJSONArray(index).size() > 0) {
            Object value = jsonArray.getJSONArray(index).get(0);
            if (value != null && !value.toString().equals("null")) {
                try {
                    String cleanedValue = value.toString().replaceAll("[^0-9-]", "");
                    short sValue = Short.parseShort(cleanedValue.trim());
                    S7control.getinstance().WriteWord(address, Arrays.asList(sValue));
                    System.out.println("messageValue:" + Arrays.asList(sValue) + " written to PLC at address " + address);
                } catch (NumberFormatException e) {
                    // 如果无法解析为 short 类型,则忽略该部分
                    System.err.println("Could not parse value: " + value);
                }
            }
        }
    }
 
 
    //写入String
    public void writeStringToPLC(JSONArray messageArray, String addresses, int index) {
        try {
            if (messageArray.getJSONArray(index).size() > 0) {
                JSONArray jsonArray = messageArray.getJSONArray(index);
                if (!jsonArray.isEmpty()) {
                    String value = (String) jsonArray.get(0);
                    outmesid(value.trim(), addresses);
                    System.out.println("Value " + value + " written to PLC at address " + addresses);
                }
            }
        } catch (Exception e) {
            System.err.println("An error occurred while writing string to PLC: " + e.getMessage());
        }
    }
}