Kaynağa Gözat

增加多路电源合并成一路控制

weizhengliang 1 yıl önce
ebeveyn
işleme
f74ccba430

+ 24 - 7
conversion_box/src/main/java/com/wdkl/app/ncs/conversion_box/activity/MainActivity.kt

@@ -1666,21 +1666,38 @@ class MainActivity :BaseActivity<MainActivityPresenter, MainActivityLayoutBindin
                         }else if(tcpModel.action==TcpAction.DeviceAction.OPEN_DEBUG){ //打开设备网络调试
                             AppUpdateHelper.openNetWorkDebug()
                         }else if (tcpModel.action==TcpAction.DeviceAction.S485_POWER_RESET){ //控制485线路板通断 0_1 重启第一路 0_1_0 关闭第一路 0_1_1 打开第一路 0_0_0关闭板上所有电路
-
+                            if (tcpModel.data == null) {
+                                return
+                            }
 
                             val pwcontrol = tcpModel.data.toString().split('_')
                             DeviceUtil.s485PowerResetSuccess(Constant.DEVICE_ID,tcpModel.data.toString(),tcpModel.tid)
                             if(pwcontrol.size==2){
-                                PowerControlUtil.controlPowerControl(pwcontrol[0].toInt(),pwcontrol[1].toInt(),true)
-                                AppTool.Time.delay(3000){
-                                    PowerControlUtil.controlPowerControl(pwcontrol[0].toInt(),pwcontrol[1].toInt(),false)
+                                val lines = pwcontrol[1].split(",")
+                                if (lines.size > 1) {
+                                    //多路并成一路控制
+                                    PowerControlUtil.controlPowerControlList(pwcontrol[0].toInt(), lines, true)
+                                    AppTool.Time.delay(3000){
+                                        PowerControlUtil.controlPowerControlList(pwcontrol[0].toInt(), lines,false)
+                                    }
+                                } else if (lines.size == 1) {
+                                    //单路控制
+                                    PowerControlUtil.controlPowerControl(pwcontrol[0].toInt(), lines[0].toInt(), true)
+                                    AppTool.Time.delay(3000){
+                                        PowerControlUtil.controlPowerControl(pwcontrol[0].toInt(), lines[0].toInt(),false)
+                                    }
                                 }
                             }else if(pwcontrol.size==3){
-                                    PowerControlUtil.controlPowerControl(pwcontrol[0].toInt(),pwcontrol[1].toInt(),pwcontrol[2].toInt()==0)
-
+                                val lines = pwcontrol[1].split(",")
+                                if (lines.size > 1) {
+                                    //多路并成一路控制
+                                    PowerControlUtil.controlPowerControlList(pwcontrol[0].toInt(), lines, pwcontrol[2].toInt() == 0)
+                                } else if (lines.size == 1) {
+                                    //单路控制
+                                    PowerControlUtil.controlPowerControl(pwcontrol[0].toInt(), lines[0].toInt(), pwcontrol[2].toInt() == 0)
+                                }
                             }
 
-
                         }
                     } /*else if (tcpModel.type == TcpType.BROADCAST) {
                         if (tcpModel.action == TcpAction.BroadcastAction.START) {

+ 17 - 1
conversion_box/src/main/java/com/wdkl/app/ncs/conversion_box/helper/PowerControlUtil.java

@@ -2,9 +2,11 @@ package com.wdkl.app.ncs.conversion_box.helper;
 
 import android.os.CountDownTimer;
 
+import java.util.List;
+
 public class PowerControlUtil {
 
-    public static void controlPowerControl(Integer address,Integer lineNumber,Boolean off){
+    public static void controlPowerControl(final Integer address, final Integer lineNumber, final Boolean off){
         new CountDownTimer(3000,1000){
 
             @Override
@@ -19,4 +21,18 @@ public class PowerControlUtil {
     }
 
 
+    public static void controlPowerControlList(final Integer address, final List<String> lines, final Boolean off){
+        new CountDownTimer(3000,1000){
+
+            @Override
+            public void onTick(long l) { //每隔1秒发送一次控制指令,总共下发3次
+                SerialPortHelper.s485PowerControlLines(address, lines, off);
+            }
+            @Override
+            public void onFinish() {
+
+            }
+        }.start();
+    }
+
 }

+ 42 - 0
conversion_box/src/main/java/com/wdkl/app/ncs/conversion_box/helper/SerialPortHelper.java

@@ -1,10 +1,12 @@
 package com.wdkl.app.ncs.conversion_box.helper;
 
+import android.text.TextUtils;
 import android.util.Log;
 
 import com.wdkl.ncs.android.middleware.common.Constant;
 
 import java.nio.ByteBuffer;
+import java.util.List;
 
 import serialporttest.utils.SerialPortUtil;
 
@@ -165,6 +167,46 @@ public class SerialPortHelper {
         SerialPortUtil.getInstance().send(bytes);
     }
 
+    public static void s485PowerControlLines(Integer controlAddress, List<String> lineNumbers, Boolean off){
+        if(lineNumbers.size() < 1){
+            return;
+        }
+        String address = String.format("%02x",controlAddress&0xFF);
+        StringBuilder sb = new StringBuilder();
+        sb.append("24").append(address).append("2C");
+
+        String controlStatus = "FFFFFFFF";  //默认所有电路开启
+        byte[] commandBytes = hexStringToByteArray(controlStatus);
+        if (off) {
+            for (String num : lineNumbers) {
+                if (TextUtils.isEmpty(num)) {
+                    continue;
+                }
+                int line = Integer.parseInt(num);
+                if (line < 0 || line > 32) { //传入线路序号小于0或者大于28,认为无效线路,如果是0认为是控制所有线路
+                    continue;
+                }
+
+                if (line > 0) {
+                    controlStatus = POWER_CONTROL_STATUS[line - 1];
+                    byte[] temp = hexStringToByteArray(controlStatus);
+                    //多路控制,按位取&
+                    for (int i = 0; i < commandBytes.length; i++) {
+                        commandBytes[i] = (byte) (temp[i] & commandBytes[i]);
+                    }
+                }
+            }
+        }
+
+        for (int i = 0; i < commandBytes.length; i++) { //与协议相反,按位取反
+            commandBytes[i]=(byte)~commandBytes[i];
+        }
+        sb.append(byteArrToHexString(commandBytes)).append("4623");
+        byte[] bytes=hexStringToByteArray(sb.toString());
+        Log.i("s485PowerControl", "s485PowerControl: "+bytes[0]+","+bytes[1]+","+bytes[2]+","+bytes[3]+","+bytes[4]+","+bytes[5]+","+bytes[6]+","+bytes[7]+","+bytes[8]+",");
+        SerialPortUtil.getInstance().send(bytes);
+    }
+
 
     public static byte[] intToBytes(int a){
         ByteBuffer bb = ByteBuffer.allocate(4);