wuyouming666
2024-04-11 94c5d454d1c30bdea54dabe0843cc935ccb68064
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package com.mes.common;
 
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
public class PlcParameterObject {
 
    // 该模块数据类型,数据起始位置
    private String plcAddressBegin;
    // 数据地址长度:第一参数到最后一个参数的长度
    private int plcAddressLength;
    private ArrayList<PlcParameterInfo> plcParameterList;
 
    /**
     * @return 数据区开始地址
     */
    public String getPlcAddressBegin() {
        return plcAddressBegin;
    }
 
    /**
     * @param plcAddressBegin 设置数据区开始地址
     */
    public void setPlcAddressBegin(String plcAddressBegin) {
        this.plcAddressBegin = plcAddressBegin;
    }
 
    /**
     * @return 数据区 读取所有数据所需的长度(以byte类型为基准)
     */
    public int getPlcAddressLength() {
        return plcAddressLength;
    }
 
    /**
     * @return 设置:数据区 读取所有数据所需的长度(以byte类型为基准)
     */
    public void setPlcAddressLength(int plcAddressLength) {
        this.plcAddressLength = plcAddressLength;
    }
 
    /**
     * @return 获取参数实例集合
     */
    public ArrayList<PlcParameterInfo> getPlcParameterList() {
        return plcParameterList;
    }
 
    /**
     * 根据参数标识 获取某个参数实例
     * 
     * @param codeid 参数标识
     * @return 获取某个参数实例
     */
    public PlcParameterInfo getPlcParameter(String codeid) {
        if (plcParameterList != null) {
            for (PlcParameterInfo plcParameterInfo : plcParameterList) {
                if (plcParameterInfo.getCodeId().equals(codeid))
                    return plcParameterInfo;
            }
            return null;
        } else
            return null;
    }
 
 
 
    /**
     * 根据参数标识 获取某个参数实例
     * 
     * @param codeids 参数标识
     * @return 获取某个参数实例
     */
    public List<String> getPlcParameterValues(List<String> codeids) {
        List<String> arrayList = new ArrayList<>();
        if (plcParameterList != null) {
            Map<String, PlcParameterInfo> resultMap = new LinkedHashMap<>(); // 使用 LinkedHashMap 保留插入顺序
            for (PlcParameterInfo plcParameterInfo : plcParameterList) {
                if (codeids.contains(plcParameterInfo.getCodeId())) {
                    resultMap.put(plcParameterInfo.getCodeId(), plcParameterInfo);
                }
            }
            for (String codeId : codeids) { // 按照传入参数的顺序遍历
                PlcParameterInfo plcParameterInfo = resultMap.get(codeId);
                if (plcParameterInfo != null) {
                    arrayList.add(plcParameterInfo.getValue());
                } else {
                    arrayList.add(null); // 如果找不到对应的值,添加 null
                }
            }
        }
        return arrayList;
    }
 
 
    public List<String> getAddressListByCodeId(List<String> codeIdList) {
        List<String> addressList = new ArrayList<>();
        for (String codeId : codeIdList) {
            for (PlcParameterInfo plcParameterInfo : plcParameterList) {
                if (plcParameterInfo.getCodeId().equals(codeId)) {
                    int index = plcParameterInfo.getAddressIndex();
                    String address = plcParameterInfo.getAddress(index);
                    if (address != null) {
                        addressList.add(address);
                    }
                }
            }
        }
        return addressList;
    }
 
 
 
 
 
 
    /**
     * 添加参数实例
     * 
     * @param param 参数实例
     */
    public void addPlcParameter(PlcParameterInfo param) {
        if (plcParameterList != null)
            plcParameterList.add(param);
        else {
            plcParameterList = new ArrayList<PlcParameterInfo>();
            plcParameterList.add(param);
        }
    }
 
    /**
     * 根据PLC返回的数据 给参数实例赋值
     * 
     * @param plcValueArray PLC读取回来的byte类型数据集合
     */
    public void setPlcParameterList(byte[] plcValueArray) {
        if (plcParameterList != null) {
          
            for (PlcParameterInfo plcParameterInfo : plcParameterList) {
               
                byte[] valueList = new byte[plcParameterInfo.getAddressLength()];
 
//                System.out.println(plcParameterInfo.getAddressLength());
 
                for (int i = 0; i < plcParameterInfo.getAddressLength(); i++) {
                    Array.setByte(valueList, i, plcValueArray[plcParameterInfo.getAddressIndex() + i]);
 
                }
                if (plcParameterInfo.getAddressLength()==2) { 
                      plcParameterInfo.setValue(String.valueOf(byte2short(valueList)));
                }
                else if (plcParameterInfo.getAddressLength()==14) {
                    plcParameterInfo.setValue((byteToHexString(valueList)));
                }
 
                else
                {
                    String valuestr = new String(valueList);
                    plcParameterInfo.setValue(valuestr);
                }
            }
        }
    }
     /**
     * short类型转byte[]
     * 
     * @param s short类型值
     */
    public static byte[] short2byte(short s){
        byte[] b = new byte[2]; 
        for(int i = 0; i < 2; i++){
            int offset = 16 - (i+1)*8; //因为byte占4个字节,所以要计算偏移量
            b[i] = (byte)((s >> offset)&0xff); //把16位分为2个8位进行分别存储
        }
        return b;
   }
     /**
     * byte[]类型转short
     * 
     * @param b byte[]类型值
     */
   public static short byte2short(byte[] b){
       short l = 0;
       for (int i = 0; i < 2; i++) {
           l<<=8; //<<=和我们的 +=是一样的,意思就是 l = l << 8 
           l |= (b[i] & 0xff); //和上面也是一样的  l = l | (b[i]&0xff)
       }
       return l;
   }
 
    public static String byteToHexString(byte[] bytes) {
 
        String str = new String(bytes, StandardCharsets.UTF_8);
        return str;
    }
 
 
}