From 6877ca889fcc8e9963bb12fe27bff106bcc79ebd Mon Sep 17 00:00:00 2001
From: wu <731351411@qq.com>
Date: 星期二, 28 五月 2024 09:06:57 +0800
Subject: [PATCH] Merge branch 'master' of http://10.153.19.25:10101/r/HangZhouMes

---
 hangzhoumesParent/common/servicebase/src/main/java/com/mes/device/PlcParameterObject.java |   43 ++++++++++++++++++++++++++++++++++++-------
 1 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/hangzhoumesParent/common/servicebase/src/main/java/com/mes/device/PlcParameterObject.java b/hangzhoumesParent/common/servicebase/src/main/java/com/mes/device/PlcParameterObject.java
index b32c593..dae4ca1 100644
--- a/hangzhoumesParent/common/servicebase/src/main/java/com/mes/device/PlcParameterObject.java
+++ b/hangzhoumesParent/common/servicebase/src/main/java/com/mes/device/PlcParameterObject.java
@@ -1,5 +1,8 @@
 package com.mes.device;
 
+import com.github.xingshuangs.iot.utils.IntegerUtil;
+import com.github.xingshuangs.iot.utils.ShortUtil;
+
 import java.lang.reflect.Array;
 import java.nio.charset.StandardCharsets;
 import java.util.*;
@@ -156,15 +159,14 @@
                     Array.setByte(valueList, i, plcValueArray[plcParameterInfo.getAddressIndex() + i]);
                 }
                 if (plcParameterInfo.getAddressLength() == 2) {
-                    plcParameterInfo.setValue(String.valueOf(byte2short(valueList)));
+                    plcParameterInfo.setValue(String.valueOf(ShortUtil.toUInt16(valueList)));
                 } else if (plcParameterInfo.getAddressLength() == 4) {
-                    plcParameterInfo.setValue(String.valueOf(byte2int(valueList)));
+                    plcParameterInfo.setValue(String.valueOf(IntegerUtil.toUInt32(valueList)));
                 }
                 else if (plcParameterInfo.getAddressLength() >10) {
                     plcParameterInfo.setValue((byteToHexString(valueList)));
                 } else {
-                    String valuestr = new String(valueList);
-                    plcParameterInfo.setValue(valuestr);
+                    plcParameterInfo.setValue((byteToHexString(valueList)));
                 }
             }
         }
@@ -194,6 +196,25 @@
             }
         }*/
     }
+    /**
+     * 鎶婂啓鍏ュ�艰浆鍖栦负byte[]
+     * @param param 鍙傛暟瀹炰緥
+     * @param data 鍐欏叆鍊肩殑瀛楃绫诲瀷
+     */
+    public byte[] setValueToBytes(PlcParameterInfo param, String data) {
+        if (param.getAddressLength() == 2) {
+            return ShortUtil.toByteArray(Integer.parseInt(data));
+
+        } else if (param.getAddressLength() == 4) {
+
+            return IntegerUtil.toByteArray(Long.parseLong(data));
+        }
+        else if (param.getAddressLength() >10) {
+           return data.getBytes();
+        } else {
+            return data.getBytes();
+        }
+    }
 
     /**
      * short绫诲瀷杞琤yte[]
@@ -203,7 +224,7 @@
     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涓瓧鑺傦紝鎵�浠ヨ璁$畻鍋忕Щ閲�
+            int offset = 16 - (i + 1) * 8; //璁$畻鍋忕Щ閲�
             b[i] = (byte) ((s >> offset) & 0xff); //鎶�16浣嶅垎涓�2涓�8浣嶈繘琛屽垎鍒瓨鍌�
         }
         return b;
@@ -228,14 +249,22 @@
      *
      * @param b byte[]绫诲瀷鍊�
      */
-    public static short byte2int(byte[] b) {
-        short l = 0;
+    public static int byte2int(byte[] b) {
+        int 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 byte[] int2byte(int s){
+        byte[] b = new byte[2];
+        for(int i = 0; i < 4; i++){
+            int offset = 16 - (i+1)*8; //鍥犱负byte鍗�4涓瓧鑺傦紝鎵�浠ヨ璁$畻鍋忕Щ閲�
+            b[i] = (byte)((s >> offset)&0xff); //鎶�32浣嶅垎涓�4涓�8浣嶈繘琛屽垎鍒瓨鍌�
+        }
+        return b;
+    }
     public static String byteToHexString(byte[] bytes) {
         String str = new String(bytes, StandardCharsets.UTF_8).trim();
         return str;

--
Gitblit v1.8.0