严智鑫
2024-05-10 211cd083bc9fc1871779802650dfe0e607d8ca77
hangzhoumesParent/common/servicebase/src/main/java/com/mes/device/PlcParameterObject.java
@@ -1,6 +1,7 @@
package com.mes.device;
import java.lang.reflect.Array;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@@ -146,9 +147,13 @@
                    Array.setByte(valueList, i, plcValueArray[plcParameterInfo.getAddressIndex() + i]);
                }
                if (plcParameterInfo.getAddressLength() == 2) {
                    plcParameterInfo.setValue(String.valueOf(byte2short(valueList)));
                } else if (plcParameterInfo.getAddressLength() == 14) {
                } else if (plcParameterInfo.getAddressLength() == 4) {
                    plcParameterInfo.setValue(String.valueOf(byte2int(valueList)));
                }
                else if (plcParameterInfo.getAddressLength() >10) {
                    plcParameterInfo.setValue((byteToHexString(valueList)));
                } else {
                    String valuestr = new String(valueList);
@@ -186,9 +191,21 @@
        return l;
    }
    /**
     * byte[]类型转short
     *
     * @param b byte[]类型值
     */
    public static short byte2int(byte[] b) {
        short l = 0;
        for (int i = 0; i < 4; i++) {
            l <<= 8; //<<=和我们的 +=是一样的,意思就是 l = l << 8
            l |= (b[3-i] & 0xff); //和上面也是一样的  l = l | (b[i]&0xff)
        }
        return l;
    }
    public static String byteToHexString(byte[] bytes) {
        String str = new String(bytes, StandardCharsets.UTF_8);
        String str = new String(bytes, StandardCharsets.UTF_8).trim();
        return str;
    }