DESKTOP-SADJPBG\47462 5 anos atrás
pai
commit
ab9350aa53

+ 0 - 1
app/src/main/java/com/siwei/recyclebox/application/AppApplication.java

xqd
@@ -178,7 +178,6 @@ public class AppApplication extends BaseApplication implements CustomActivityOnC
         Log.i("IMEI=",MainViewModel.getDeviceId(this));
         SPUtils.getInstance().put("IMEI",MainViewModel.getDeviceId(this));
 //        myDeviceInfo.deviceName="123456";
-//        SPUtils.getInstance().put("deviceSecret2","4pJyg1tm8VMOXZGwASAaaVgzRzZVMbbs");//存
         String secret = SPUtils.getInstance().getString("deviceSecret2");//读
 //        String secret ="";
         Log.i(TAG,"secret=="+secret);

+ 131 - 12
app/src/main/java/com/siwei/recyclebox/deviceUtils/OtherDevice.java

xqd xqd xqd xqd xqd xqd xqd xqd
@@ -127,7 +127,7 @@ public class OtherDevice extends BaseDeviceEntity {
                 return 1;
             }
 //            MainViewModel.reportDeviceEvent("queryMagDoor");
-            return 0;
+            return 3;
 
         }catch ( Exception e ){
             e.printStackTrace();
@@ -208,7 +208,7 @@ public class OtherDevice extends BaseDeviceEntity {
 
     public void setPower(){
         try {
-            byte[] setPower = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x0A, 0x02, 0x40, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
+            byte[] setPower = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x0A, 0x02, 0x25, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};
             port.write(setPower, 500);
             byte[] response = new byte[12];
             port.read(response,500);
@@ -550,7 +550,7 @@ public class OtherDevice extends BaseDeviceEntity {
 
     /*
     * 称重
-    * */
+    * *//*
     public Integer readWeight(){
         try{
 //            主机发送 : AA BB 0B 01 55 FF FF FF FF FF CC DD
@@ -585,10 +585,10 @@ public class OtherDevice extends BaseDeviceEntity {
             return 0;
         }
     }
-    /*校准流程:1.设置分度值 2.去皮 3.零度校准 4.放砝码5.砝码校准*/
-    /*
+    *//*校准流程:1.设置分度值 2.去皮 3.零度校准 4.放砝码5.砝码校准*//*
+    *//*
     * 设置分度值(几克起跳 精度)
-    * */
+    * *//*
     public void setScale(){
 //        主机发送 : AA BB 0B 01 66 01 61 XX FF FF CC DD   XX是分度值 只能是10 20 50 A0 分别是1g 2g 5g 和10g  (常规:100kg 误差10g ;200kg误差20g )
 //        设备回复:  AA BB 0B 01 66 01 61 00 60 FF CC DD
@@ -608,9 +608,9 @@ public class OtherDevice extends BaseDeviceEntity {
 
     }
 
-    /*
+    *//*
     * 称重清零去皮
-    * */
+    * *//*
     public void clearWeight(){
 //        主机发送 : AA BB 0B 01 66 01 62 63 FF FF CC DD
 //        设备回复:  AA BB 0B 01 66 01 62 00 63 FF CC DD
@@ -629,9 +629,9 @@ public class OtherDevice extends BaseDeviceEntity {
         }
     }
 
-    /*
+    *//*
     * 零度校准
-    * */
+    * *//*
     public void zeroCalibration(){
 //        主机发送 : AA BB 0B 01 66 01 64 65 FF FF CC DD
 //        设备回复: AA BB 0B 01 66 01 64 00 65 FF CC DD
@@ -650,9 +650,9 @@ public class OtherDevice extends BaseDeviceEntity {
         }
     }
 
-    /*
+    *//*
     * 砝码校准
-    * */
+    * *//*
     public void weightCalibration(){
         try{
 //            主机发送 : AA BB 0B 01 66 01 65 XX XX FF CC DD  XX是校准的砝码 只能是单位g 防8kg 就是8000g 十六进制就是1F40
@@ -671,5 +671,124 @@ public class OtherDevice extends BaseDeviceEntity {
             Log.i(TAG,"砝码校准失败");
         }
     }
+*/
+    public synchronized Integer readWeight(){
+        try {
+//            SPUtils.getInstance().put("RunningState","0");//1F 03 00 2A 00 01 A6 7C
+            port.write(new byte[]{0x1F, 0x03, 0x00, 0x2A,0x00, 0x01, (byte) 0xA6, 0x7C},150);//称重器读取数值的请求
+//            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0C ,0x01 ,0x55 , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);//称重器读取数值的请求
+//            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB,0x0C ,0x02 ,0x55 , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD},150);//超声波读取数值的请求
+//            Thread.sleep(100);
+            byte[] response = new byte[7];
+            port.read(response,200);
+            Log.i(TAG,"返回称重"+ByteUtil.bytesToString(response));
+
+            if(response[1]==3) {
+                long curTimeMiles = System.currentTimeMillis();
+                int num=0,a=0;
+                while (System.currentTimeMillis()-curTimeMiles<200){
+                    port.read(response,10);
+                    byte[] weightBytes = new byte[4];
+                    weightBytes[2] = response[3];
+                    weightBytes[3] = response[4];
+                    int weightInt = ByteUtil.bytes2Int(weightBytes);
+//                    weightInt=weightInt*5*2;
+                    num += (weightInt * 5 * 2);
+                    a++;
+                    Log.i("while","********"+a);
+                }
+                Log.i("num",num+"****"+a+"");
+                byte[] weightBytes = new byte[4];
+                weightBytes[2] = response[3];
+                weightBytes[3] = response[4];
+                int weightInt = ByteUtil.bytes2Int(weightBytes);
+                weightInt=weightInt*5*2;
+                System.out.println("称重读数:"+weightInt);
+                int weightNumber=num/a++;
+                if(weightInt>=327670){
+                    weightNumber=0;
+                }
+                return (int) weightNumber;
+            }else {
+                Log.i("返回称重","错误");
+                SPUtils.getInstance().put("RunningState",1);
+                MainViewModel.SendSmsResponse("readWeight称重失灵");
+                return 1;
+            }
+//            int readLen = port.read(response,200);
+//            System.out.println("readweight len:"+readLen+"  bytes;"+ByteUtil.bytesToString(response));
+
+//            return 1;
+//
+        }catch ( Exception e ){
+            e.printStackTrace();
+            SPUtils.getInstance().put("RunningState",1);
+            MainViewModel.SendSmsResponse("readWeight称重设备未检测到");
+
+            Log.e(TAG,"读取称重器读数错误",e);
+            return 1;
+        }
+
+    }
+  /*  *
+     * 读数清零
+     */
+    public synchronized void weightClear(){
+        byte[] cleanOrderBytes = new byte[]{0x1F,0x06 ,0x00 ,0x24,0x00 ,0x00, (byte) 0xCA,0x7F};
+        System.out.println("称重器读数清零!!!!!");
+        try {
+            port.write(cleanOrderBytes,150);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+     /** 设置分度值
+     * */
+    public synchronized void setScale(){
+        byte[] cleanOrderBytes = new byte[]{0x1F, 0x06 ,0x00 ,0x04 ,0x00 ,0x01 ,0x0A ,0x75};
+        try {
+            port.write(cleanOrderBytes,150);
+            Log.i(TAG,"设置分度值");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+   /*  * 空载
+     * */
+    public synchronized void zeroCalibration(){
+        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x08 ,0x00 ,0x02 ,0x04 ,0x00 ,0x00 ,0x00 ,0x00 , (byte) 0x92,0x21 };
+        try {
+            port.write(cleanOrderBytes,150);
+            Log.i(TAG,"空载");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /* * 量程设定
+     * */
+    public synchronized void weightSet(){
+        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x00 ,0x00 ,0x02 ,0x04 , 0x27, 0x10,0x00 ,0x00 , (byte) 0x98, (byte) 0xF6};
+        try {
+            port.write(cleanOrderBytes,150);
 
+            Log.i(TAG,"l量程设定");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+     /** 砝码校准
+     * */
+    public synchronized void weightCalibration(){
+        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x10 ,0x00 ,0x02 ,0x04 ,0x27 ,0x10 ,0x00 ,0x00, (byte) 0x96, (byte) 0xE4};
+        try {
+            port.write(cleanOrderBytes,150);
+            Log.i(TAG,"砝码校准kg");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
 }

+ 9 - 2
app/src/main/java/com/siwei/recyclebox/deviceUtils/SerialPortUtil.java

xqd xqd
@@ -50,6 +50,7 @@ public class SerialPortUtil {
 
         final List<UsbSerialDriver> drivers =
                 UsbSerialProber.getDefaultProber().findAllDrivers(mUsbManager);
+        Log.i("drivers=====",drivers.toString());
 
         for (final UsbSerialDriver driver : drivers) {
             System.out.println("SerialPortUtil:"+driver.toString());
@@ -177,9 +178,15 @@ public class SerialPortUtil {
 
     private BaseDeviceEntity isOtherDevice( UsbSerialDriver driver ){
         OtherDevice otherDevice = new OtherDevice(mUsbManager,driver);
-        Integer infrared = otherDevice.queryInfrared();
+//        Integer infrared = otherDevice.queryInfrared();
+        Integer  magDoor = otherDevice.queryMagDoor();
         Log.i("otherDevice","---------------------------------------");
-        if(infrared == 0) {
+//        if(infrared == 0) {
+//            otherDevice.close();
+//            Log.i("otherDevice","==null");
+//            return null;
+//        }
+        if(magDoor == 3) {
             otherDevice.close();
             Log.i("otherDevice","==null");
             return null;

+ 14 - 3
app/src/main/java/com/siwei/recyclebox/deviceUtils/WeightDevice.java

xqd xqd xqd xqd xqd
@@ -160,6 +160,8 @@ public class WeightDevice extends BaseDeviceEntity {
         System.out.println("称重器读数清零!!!!!");
         try {
             port.write(cleanOrderBytes,150);
+            byte[] response=new byte[8];
+            port.read(response,200);
         } catch (IOException e) {
             e.printStackTrace();
         }
@@ -171,6 +173,8 @@ public class WeightDevice extends BaseDeviceEntity {
         byte[] cleanOrderBytes = new byte[]{0x1F, 0x06 ,0x00 ,0x04 ,0x00 ,0x01 ,0x0A ,0x75};
         try {
             port.write(cleanOrderBytes,150);
+            byte[] response=new byte[8];
+            port.read(response,200);
             Log.i(TAG,"设置分度值");
         } catch (IOException e) {
             e.printStackTrace();
@@ -183,6 +187,8 @@ public class WeightDevice extends BaseDeviceEntity {
         byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x08 ,0x00 ,0x02 ,0x04 ,0x00 ,0x00 ,0x00 ,0x00 , (byte) 0x92,0x21 };
         try {
             port.write(cleanOrderBytes,150);
+            byte[] response=new byte[8];
+            port.read(response,200);
             Log.i(TAG,"空载");
         } catch (IOException e) {
             e.printStackTrace();
@@ -192,10 +198,11 @@ public class WeightDevice extends BaseDeviceEntity {
      * 量程设定
      * */
     public synchronized void weightSet(){
-        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x00 ,0x00 ,0x02 ,0x04 , (byte) 0xff, (byte) 0xff,0x00 ,0x00 , (byte) 0x98, (byte) 0xF6};
+        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x00 ,0x00 ,0x02 ,0x04 , 0x27, 0x10,0x00 ,0x00 , (byte) 0x98, (byte) 0xF6};
         try {
             port.write(cleanOrderBytes,150);
-
+            byte[] response=new byte[8];
+            port.read(response,200);
             Log.i(TAG,"l量程设定");
         } catch (IOException e) {
             e.printStackTrace();
@@ -205,9 +212,13 @@ public class WeightDevice extends BaseDeviceEntity {
      * 砝码校准
      * */
     public synchronized void weightCalibration(){
-        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x10 ,0x00 ,0x02 ,0x04 ,0x27 ,0x10 ,0x00 ,0x00, (byte) 0x96, (byte) 0xE4};
+        //1F 10 00 10 00 02 04 03 E8 00 00 12 FB
+        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x10 ,0x00 ,0x02 ,0x04 ,0x03 , (byte) 0xE8,0x00 ,0x00, (byte) 0x96, (byte) 0xE4};
+//        byte[] cleanOrderBytes = new byte[]{0x1F ,0x10 ,0x00 ,0x10 ,0x00 ,0x02 ,0x04 ,0x27 ,0x10 ,0x00 ,0x00, (byte) 0x96, (byte) 0xE4};
         try {
             port.write(cleanOrderBytes,150);
+            byte[] response=new byte[8];
+            port.read(response,200);
             Log.i(TAG,"砝码校准kg");
         } catch (IOException e) {
             e.printStackTrace();

+ 2 - 1
app/src/main/java/com/siwei/recyclebox/ui/main/MainViewModel.java

xqd xqd
@@ -344,6 +344,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             System.out.println("btnClicksetScale");
 //            SerialPortUtil.getInstance().getOtherDevice().setScale();
             SerialPortUtil.getInstance().getWeightDevice().setScale();
+//            SerialPortUtil.getInstance().getOtherDevice().weightSet();
             SerialPortUtil.getInstance().getWeightDevice().weightSet();
         }
     };
@@ -352,7 +353,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         @Override
         public void onClick(View view) {
             System.out.println("btnClickclearWeight");
-//            SerialPortUtil.getInstance().getOtherDevice().clearWeight();
+//            SerialPortUtil.getInstance().getOtherDevice().weightClear();
             SerialPortUtil.getInstance().getWeightDevice().weightClear();
         }
     };