|
@@ -4,10 +4,24 @@ import android.util.Log;
|
|
|
|
|
|
import com.wdkl.ncs.android.middleware.common.Constant;
|
|
|
|
|
|
+import java.nio.ByteBuffer;
|
|
|
+
|
|
|
import serialporttest.utils.SerialPortUtil;
|
|
|
|
|
|
public class SerialPortHelper {
|
|
|
|
|
|
+ //电源控制板关闭指令集,数组索引代表该索引+1路电路关闭指令。索引0 关闭第一路,1关闭第二路。
|
|
|
+ static String[] POWER_CONTROL_STATUS=new String[]{
|
|
|
+ "FFFFFFFE","FFFFFFFD","FFFFFFFB","FFFFFFF7",
|
|
|
+ "FFFFFFEF","FFFFFFDF","FFFFFFBF","FFFFFF7F",
|
|
|
+ "FFFFFEFF","FFFFFDFF","FFFFFBFF","FFFFF7FF",
|
|
|
+ "FFFFEFFF","FFFFDFFF","FFFFBFFF","FFFF7FFF",
|
|
|
+ "FFFEFFFF","FFFDFFFF","FFFBFFFF","FFF7FFFF",
|
|
|
+ "FFEFFFFF","FFDFFFFF","FFBFFFFF","FF7FFFFF",
|
|
|
+ "FEFFFFFF","FDFFFFFF","FBFFFFFF","F7FFFFFF",
|
|
|
+ "EFFFFFFF","DFFFFFFF","BFFFFFFF","7FFFFFFF"
|
|
|
+ };
|
|
|
+
|
|
|
//重置设备
|
|
|
public static void resetDevice() {
|
|
|
SerialPortUtil.getInstance().sendCommand(SerialPortUtil.NET_STATUS , "1", "F");
|
|
@@ -107,4 +121,82 @@ public class SerialPortHelper {
|
|
|
public static void changeCallingMode(String mode) {
|
|
|
SerialPortUtil.getInstance().send(SerialPortUtil.C_HEARD + "F" + SerialPortUtil.C_SEPARATE + "FFFF" + mode + SerialPortUtil.C_END);
|
|
|
}
|
|
|
+
|
|
|
+ public static void s485PowerControl(Integer controlAddress,Integer lineNumber,Boolean off){
|
|
|
+ if(lineNumber<0||lineNumber>32){ //传入线路序号小于0或者大于28,认为无效线路,如果是0认为是控制所有线路
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String address = String.format("%02x",controlAddress&0xFF);
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("24").append(address).append("2C");
|
|
|
+//
|
|
|
+ byte[] bytes = new byte[9];
|
|
|
+// bytes[0]=0x24;//$
|
|
|
+// bytes[1]=; //控制板地址 0-7
|
|
|
+// bytes[2]=0x2c;//,
|
|
|
+// bytes[3]=0x00;//指令高位
|
|
|
+// bytes[4]=0x00;
|
|
|
+// bytes[5]=0x00;
|
|
|
+// bytes[6]=0x00;//指令低位
|
|
|
+// bytes[7]=0x46;//F
|
|
|
+// bytes[8]=0x23;//#
|
|
|
+ String controlStatus = "FFFFFFFF";
|
|
|
+ if(lineNumber>0){
|
|
|
+ controlStatus=POWER_CONTROL_STATUS[lineNumber-1];
|
|
|
+ }
|
|
|
+ if(off){//关闭电路
|
|
|
+ if(lineNumber==0){
|
|
|
+ controlStatus="00000000"; //关闭所有
|
|
|
+ }
|
|
|
+
|
|
|
+ }else{ //打开电路
|
|
|
+ controlStatus = "FFFFFFFF";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ byte[] commandBytes = hexStringToByteArray(controlStatus);
|
|
|
+
|
|
|
+ for (int i = 0; i < commandBytes.length; i++) { //与协议相反,按位取反
|
|
|
+ commandBytes[i]=(byte)~commandBytes[i];
|
|
|
+ }
|
|
|
+ sb.append(byteArrToHexString(commandBytes)).append("4623");
|
|
|
+ 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);
|
|
|
+ bb.putInt(a);
|
|
|
+ return bb.array();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 16进制表示的字符串转换为字节数组
|
|
|
+ public static byte[] hexStringToByteArray(String hexString) {
|
|
|
+ hexString = hexString.replaceAll(" ", "");
|
|
|
+ int len = hexString.length();
|
|
|
+ byte[] bytes = new byte[len / 2];
|
|
|
+ for (int i = 0; i < len; i += 2) {
|
|
|
+ // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节
|
|
|
+ bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
|
|
|
+ .digit(hexString.charAt(i + 1), 16));
|
|
|
+ }
|
|
|
+ return bytes;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组转16进制字符串
|
|
|
+ * @param b
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String byteArrToHexString(byte[] b){
|
|
|
+ String result="";
|
|
|
+ for (int i=0; i < b.length; i++) {
|
|
|
+ result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring(1);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|