Explorar o código

设置控制地址

wuyunfeng %!s(int64=2) %!d(string=hai) anos
pai
achega
89fd44ccf6

+ 17 - 17
app/src/main/java/com/wdkl/ncs/app/powercontroltest/MainActivity.java

@@ -21,6 +21,7 @@ import com.wdkl.ncs.app.powercontroltest.common.Constants;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
@@ -120,19 +121,19 @@ public class MainActivity extends SerialPortActivity {
         bytes[8] =0x23;
 
 //        sb.append(str2HexStr("$")).append("0").append(str2HexStr(","));
-//        if(s.isChecked()){ //开
-//            int i = ~status[index];
-//            byte[] intByte = intToBytes(i);
-//            for (int j = 0; j < intByte.length; j++) {
-//                bytes[j+3]=intByte[j];
-//            }
-//        }else{
-//            byte[] intByte = intToBytes(status[index]);
-//            for (int j = 0; j < intByte.length; j++) {
-//                bytes[j+3]=intByte[j];
-//            }
-////            sb.append(String.format("%08X", ));
-//        }
+        if(s.isChecked()){ //开
+            int i = ~status[index];
+            byte[] intByte = intToBytes(i);
+            for (int j = 0; j < intByte.length; j++) {
+                bytes[j+3]=intByte[j];
+            }
+        }else{
+            byte[] intByte = intToBytes(status[index]);
+            for (int j = 0; j < intByte.length; j++) {
+                bytes[j+3]=intByte[j];
+            }
+//            sb.append(String.format("%08X", ));
+        }
 
 //        sb.append("F").append(str2HexStr("#"));
         Log.i(TAG, "control_click: "+new String(bytes));
@@ -197,10 +198,9 @@ public class MainActivity extends SerialPortActivity {
     }
 
     public  byte[] intToBytes(int a){
-        byte[] ans=new byte[4];
-        for(int i=0;i<4;i++)
-            ans[i]=(byte)(a>>(i*8));//截断 int 的低 8 位为一个字节 byte,并存储起来
-        return ans;
+        ByteBuffer bb = ByteBuffer.allocate(4);
+        bb.putInt(a);
+        return bb.array();
     }