package com.siwei.recyclebox.deviceUtils; import android.hardware.usb.UsbManager; import android.speech.tts.TextToSpeech; import android.util.Log; import com.aliyuncs.exceptions.ClientException; import com.siwei.recyclebox.ui.main.MainViewModel; import com.hoho.android.usbserial.driver.UsbSerialDriver; import com.hoho.android.usbserial.driver.UsbSerialPort; import android.os.Handler; import java.io.IOException; import java.util.Spliterator; import java.util.logging.LogRecord; import me.goldze.mvvmhabit.utils.SPUtils; import me.goldze.mvvmhabit.utils.Utils; /** * 温度传感器、红外传感器、距离、锁。 */ public class OtherDevice extends BaseDeviceEntity { private static final String TAG = "OtherDevice"; public OtherDevice(UsbManager usbManager, UsbSerialDriver driver) { super(usbManager, driver); } /** * 串口的连接参数: * baudRate: 波特率 * dataBits: 数据位 一般为8 * stopBits: 停止位 一般为1 * parity: 奇偶校验 UsbSerialPort.PARITY_ODD、UsbSerialPort.PARITY_EVEN、UsbSerialPort.PARITY_NONE */ @Override int getBaudRate() { // return 9600; return 115200; } @Override int getDataBits() { return UsbSerialPort.DATABITS_8; } @Override int getStopBits() { return UsbSerialPort.STOPBITS_1; } @Override int getParity() { return UsbSerialPort.PARITY_NONE; } @Override int getVendorId() { return 6790; } @Override void onDataReceived(byte[] response) { //设备回复: AA BB 0C 01 55 01 00 DB FF FF CC DD //距离感应器 if(response.length != 12){ //回报长度不正确 return; } if(response[0] == 0xAA && response[1] == 0xBB && response[10] == 0xCC && response[11] == 0xDD){ //正确的包头包尾 switch (response[2]){ case 0x0A://电控锁 break; case 0x01://距离/温度 } } } public void getRodOpenNumber(){//计算开门次数 String rodNumberStr= SPUtils.getInstance().getString("RodNumber"); if(rodNumberStr.equals("")){ rodNumberStr="0"; } int rodNumberInt=Integer.parseInt(rodNumberStr)+1; rodNumberStr=String.valueOf(rodNumberInt); SPUtils.getInstance().put("RodNumber",rodNumberStr); Log.i(TAG,"开门次数"+rodNumberStr); } public void getLockOpenNumber(){//计算开门次数 String lockNumberStr=SPUtils.getInstance().getString("LockNumber"); if(lockNumberStr.equals("")){ lockNumberStr="0"; } int lockNumberInt=Integer.parseInt(lockNumberStr)+1; lockNumberStr=String.valueOf(lockNumberInt); SPUtils.getInstance().put("LockNumber",lockNumberStr); Log.i(TAG,"开锁次数"+lockNumberStr); } private Handler mHandler=new Handler(msg -> { return true; }); /** * 查询门磁数值 */ public Integer queryMagDoor(){ try { byte[] pushRodOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(pushRodOrderBytes, 150);// byte[] response = new byte[12]; int readLen = port.read(response,200); System.out.println("读取门磁命令:"+ByteUtil.bytesToString(pushRodOrderBytes)); System.out.println("读取门磁返回:"+ByteUtil.bytesToString(response)); if(readLen == 12 && response[5]==4&&response[6]==1/*&& response[0] == 0xAA&&response[1]==0xBB&& response[2]==0x0A&&response[3]==0x01&&response[4]==0x55 && response[5] == 0x03*/){ Log.i(TAG,"response[6] 门磁读取到"+response[6]); return 0; } if(readLen == 12 && response[5]==4&&response[6]==2/*&& response[0] == 0xAA&&response[1]==0xBB&& response[2]==0x0A&&response[3]==0x01&&response[4]==0x55 && response[5] == 0x03*/){ Log.i(TAG,"response[6] 门磁读取到"+response[6]); return 1; } // MainViewModel.reportDeviceEvent("queryMagDoor"); return 3; }catch ( Exception e ){ e.printStackTrace(); SPUtils.getInstance().put("RunningState",1); MainViewModel.SendSmsResponse("其他设备未检测到"); Log.e(TAG,"读取门磁错误",e); return 3; } } /* * 查询电流过大标志 00 没有 01 有 * */ public Integer queryCurrent(){ try{ // AA BB 0A 01 55 05 FF FF FF FF CC DD 查询电流过大标志 回复00 没有 01 有 port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x05, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}, 150);// byte[] response = new byte[12]; int readLen=port.read(response,200); if(readLen == 12&&response[5]==5){ Log.i(TAG,"电流过大返回值response[6]========"+response[6]); return (int) response[6]; } // MainViewModel.reportDeviceEvent("queryCurrent"); return 0; }catch (Exception e){ e.printStackTrace(); // MainViewModel.reportDeviceEvent("queryCurrent"); Log.e(TAG,"查询电流过大失败"); return 3; } } /* * 电流过大标志清零 * */ public void clearCurrent(){ try{ port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x06, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}, 150);// }catch (Exception e){ e.printStackTrace(); Log.i(TAG,"电流过大标志清零失败"); } } /** * 查询红外感应器数值 */ public Integer queryInfrared(){ try { port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}, 100);// byte[] response = new byte[12]; int readLen = port.read(response,200); System.out.println("读取红外返回:"+ByteUtil.bytesToString(response)); if(readLen == 12 &&response[5]==3&&response[6]==1/*&& response[0] == 0xAA&&response[1]==0xBB&& response[2]==0x0A&&response[3]==0x01&&response[4]==0x55 && response[5] == 0x03*/){ Log.i(TAG,"response[6]"+response[6]); return (int) 1; } if(readLen == 12 &&response[5]==3&&response[6]==2/*&& response[0] == 0xAA&&response[1]==0xBB&& response[2]==0x0A&&response[3]==0x01&&response[4]==0x55 && response[5] == 0x03*/){ Log.i(TAG,"response[6]"+response[6]); return (int) 2; } int num=SPUtils.getInstance().getInt("queryInfraredError"); SPUtils.getInstance().put("queryInfraredError",num+1); return 0; }catch ( Exception e ){ Log.e(TAG,"读取红外线读数错误",e); SPUtils.getInstance().put("RunningState",1); MainViewModel.SendSmsResponse("其他设备未检测到"); e.printStackTrace(); return 0; } } /* * 设置电流阈值 * */ public void setPower(){ try { // byte[] setPower = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x0A, 0x02, 0x60, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; byte[] setPower = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x0A, 0x02, 0x44, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(setPower, 500); byte[] response = new byte[12]; port.read(response,500); Log.i(TAG,ByteUtil.bytesToString(response)+"设置电流"); }catch(Exception e){ e.printStackTrace(); } } public void setPowerBigger(){ try { // byte[] setPower = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x0A, 0x02, 0x60, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; byte[] setPower = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x0A, 0x03, 0x60, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(setPower, 500); byte[] response = new byte[12]; port.read(response,500); Log.i(TAG,ByteUtil.bytesToString(response)+"设置电流"); }catch(Exception e){ e.printStackTrace(); } } public void queryPower(){ try{ byte[] readPushOrderBytes=new byte[]{(byte)0xAA, (byte) 0xBB,0x0A, 0x01, 0x55 ,0x01 , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(readPushOrderBytes, 100); byte[] responseRead = new byte[ 12]; port.read(responseRead,200); System.out.println("Read response:"+ByteUtil.bytesToString(responseRead)); }catch (Exception e){ e.printStackTrace(); } } /** * 伸杆关门 */ public void pushRod(){//关门 Thread thread = new Thread(new Runnable() { @Override public void run() { try { SPUtils.getInstance().put("RodSwitch","0"); //门 66后面 01 表示杆 01表示伸杆 1c表示多少秒 02 1c 表示电流 // System.out.println("------------push rod------------------------"); SPUtils.getInstance().put("queryInfraredError",0); // byte[] setPower = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x0A, 0x02, 0x1C, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; // port.write(setPower, 100); byte[] pushRodOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x01,(byte)0x0B, (byte) 0xff, (byte) 0xff, (byte) 0xCC, (byte) 0xDD}; port.write(pushRodOrderBytes, 100); System.out.println("bytes:"+ByteUtil.bytesToString(pushRodOrderBytes)); // byte[] pushRodOrderBytes2 = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x01,0x1c, (byte) 0xFF,0x1c, (byte) 0xCC, (byte) 0xDD}; // port.write(pushRodOrderBytes2, 100); getRodOpenNumber(); byte[] response = new byte[ 12]; port.read(response,200); mHandler.postDelayed(new Runnable() { @Override public void run() { byte[] readPushOrderBytes=new byte[]{(byte)0xAA, (byte) 0xBB,0x0A, 0x01, 0x55 ,0x01 , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; try { port.write(readPushOrderBytes, 100); byte[] responseRead = new byte[ 12]; port.read(responseRead,200); System.out.println("pushRead response:"+ByteUtil.bytesToString(responseRead)); } catch (IOException e) { e.printStackTrace(); } } },1000); System.out.println("push response:"+ByteUtil.bytesToString(response)); SPUtils.getInstance().getInt("infraredNum1",0); long curr=System.currentTimeMillis(); Thread.sleep(500); while (System.currentTimeMillis() - curr < 6000) { Thread.sleep(300); Integer infrared = queryInfrared(); int ErrorNum=SPUtils.getInstance().getInt("queryInfraredError"); if(ErrorNum>15){ MainViewModel.SendSmsResponse("红外读取失灵"); } if (infrared == 1) {//有障碍物 //停止关门 Log.i(TAG, "有障碍物,停止关门"); port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x03, (byte) 0x05, (byte) 0xff, (byte) 0xff, (byte) 0xCC, (byte) 0xDD}, 200); byte[] response3 = new byte[ 12]; port.read(response3,200); Log.i("停止关门",response3+""); int num=SPUtils.getInstance().getInt("infraredNum1",0)+1; SPUtils.getInstance().put("infraredNum1",num); if(num<3){ mHandler.postDelayed(new Runnable() { @Override public void run() { pushRod(); } },5000); break; }else { mHandler.postDelayed(new Runnable() { @Override public void run() { try { port.write(pushRodOrderBytes, 100); } catch (IOException e) { e.printStackTrace(); } } },5000); break; } } } }catch (Exception e ){ e.printStackTrace(); SPUtils.getInstance().put("RunningState",1); MainViewModel.SendSmsResponse("其他设备未检测到"); Log.i(TAG,"关门失败"); } } }); thread.start(); } /** * 伸杆开门 */ public void pullRod(){//开门 Thread thread1=new Thread(new Runnable() { @Override public void run() { try { SPUtils.getInstance().put("infraredNum1",0); SPUtils.getInstance().put("RodSwitch","1"); //伸杆 66后面 01 表示杆 02表示缩杆 1c表示多少秒 02 1c 表示电流 byte[] pullRodOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x02,0x0B, (byte) 0xff, (byte) 0xff, (byte) 0xCC, (byte) 0xDD}; int writeLen = port.write(pullRodOrderBytes, 100); System.out.println("pullRod "+writeLen); byte[] response = new byte[12]; port.read(response,200); mHandler.postDelayed(new Runnable() { @Override public void run() { byte[] readPullOrderBytes=new byte[]{(byte)0xAA, (byte) 0xBB,0x0A, 0x01, 0x55 ,0x01 , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; try { port.write(readPullOrderBytes, 100); byte[] responseRead = new byte[ 12]; port.read(responseRead,200); System.out.println("pullRead response:"+ByteUtil.bytesToString(responseRead)); } catch (IOException e) { e.printStackTrace(); } } },1000); System.out.println("response:"+ByteUtil.bytesToString(response)); }catch ( Exception e ){ e.printStackTrace(); SPUtils.getInstance().put("RunningState",1); MainViewModel.SendSmsResponse("其他设备未检测到"); Log.i(TAG,"开门失败"); } } }); thread1.start(); } /** * 运收门开锁 */ public Integer openLock(){//AA BB 0A 01 66 02 01 07 FF FF CC DD try { // byte[] openLockOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x02, 0x01,0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; byte[] openLockOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x02, 0x01,0x07, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(openLockOrderBytes,200); byte[] response=new byte[12]; port.read(response,500); getLockOpenNumber(); Log.i(TAG,ByteUtil.bytesToString(response)+"开收运门"); if(response[4]==102&&response[5]==2&&response[6]==1){ return 1; }else { return 0; } }catch ( Exception e ){ e.printStackTrace(); SPUtils.getInstance().put("RunningState",1); MainViewModel.SendSmsResponse("其他设备未检测到"); Log.i(TAG,"开收运门失败"); return 0; } } /** * 运收门关锁 */ public Integer closeLock(){ try { byte[] closeLockOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x02, 0x02, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(closeLockOrderBytes, 200); byte[] response=new byte[12]; port.read(response,500); Log.i(TAG,ByteUtil.bytesToString(response)+"关收运门"); if(response[4]==102&&response[5]==2&&response[6]==2){ return 0; }else { return 0; } }catch ( Exception e ){ e.printStackTrace(); Log.i(TAG,"关收运门失败"); return 0; } } /** * 超声波 * 距离 * @return */ // public Integer getDistance(){ // try { //// byte[] bytes = new byte[]{(byte)0xAA ,(byte)0xBB ,(byte)0x0C ,(byte)0x01 ,(byte)0x55 ,(byte)0x01 ,(byte)0xFF ,(byte)0xFF,(byte)0xFF ,(byte)0xFF ,(byte)0xCC ,(byte)0xDD}; // //新超声波 01 03 01 02 00 01 24 36 // byte[] bytes = new byte[]{(byte)0xAA ,(byte)0xBB ,(byte)0x01 ,(byte)0x03 ,(byte)0x01 ,(byte)0x02 ,(byte)0x00 ,(byte)0x01,(byte)0x24 ,(byte)0x36 ,(byte)0xCC ,(byte)0xDD}; //// byte[] bytes = new byte[]{(byte)0x01 ,(byte)0x03 ,(byte)0x01 ,(byte)0x02 ,(byte)0x00 ,(byte)0x01 ,(byte)0x24 ,(byte)0x36}; // port.write(bytes,100); // byte[] response = new byte[12]; // int len = port.read(response,100); // Log.i(TAG,ByteUtil.bytesToString(response)+len+"距离"); // for (int i:response) { // Log.i("超声波", ""+response[i]); // } // Log.i("超声波", ""+response.toString()); // byte[] disBytes = new byte[4]; // disBytes[2] = response[3]; // disBytes[3] = response[4]; // Log.i(TAG,ByteUtil.bytesToString(disBytes)+"juli"); // int disInt = ByteUtil.bytes2Int(disBytes); // if(response[4]==85&&response[5]==1){ // return disInt; // }else { // SPUtils.getInstance().put("RunningState",5); // MainViewModel.SendSmsResponse("温度超声波失灵"); // return 0; // } // }catch ( Exception e ){ // e.printStackTrace(); // SPUtils.getInstance().put("RunningState",1); // MainViewModel.SendSmsResponse("其他设备未检测到"); // return 0; // } // } // /** // * 温度 // * @return // */ // public Integer geTemperature(){//获取温度 // try{ // byte[] bytes = new byte[]{(byte)0xAA ,(byte)0xBB ,(byte)0x0C ,(byte)0x01 ,(byte)0x55 ,(byte)0x02 ,(byte)0xFF ,(byte)0xFF,(byte)0xFF ,(byte)0xFF ,(byte)0xCC ,(byte)0xDD}; // port.write(bytes,200); // byte[] response=new byte[12]; // int len=port.read(response,200); // Log.i(TAG,ByteUtil.bytesToString(response)+len+"温度"+response[4]+response[5]); // byte[] temBytes=new byte[4]; //// temBytes[2]=response[5]; // temBytes[3]=response[6]; // Integer temInt=ByteUtil.bytes2Int(temBytes); //// double temDouble=0; //// if(temInt<65536){ //// temDouble=temInt/10; //// return temDouble; //// } //// temDouble=-(temInt-65536)/10; //// return temDouble; // if(response[4]==85&&response[5]==2){ // return temInt; // }else { // SPUtils.getInstance().put("RunningState",5); // MainViewModel.SendSmsResponse("温度超声波失灵"); // return 0; // } // }catch ( Exception e ){ // e.printStackTrace(); // SPUtils.getInstance().put("RunningState",1); // MainViewModel.SendSmsResponse("其他设备未检测到"); // Log.i(TAG,"测温度失败"); // return 0; // } // } /* * 开灯关灯 * */ public void openLight1(){ try{ //AA BB 0D 01 66 01 01 FF FF FF CC DD byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x01, 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(bytes,200); byte[] response=new byte[12]; port.read(response,200); Log.i(TAG,"openLight1"+ByteUtil.bytesToString(response)); }catch (Exception e){ e.printStackTrace(); Log.i(TAG,"开灯1失败"); } } public void closeLight1(){ try{ byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x01, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(bytes,200); byte[] response=new byte[12]; port.read(response,200); Log.i(TAG,"closeLight1"+ByteUtil.bytesToString(response)); }catch (Exception e){ e.printStackTrace(); Log.i(TAG,"关灯1失败"); } } public void openLight2(){ try{ byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x02, 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(bytes,200); byte[] response=new byte[12]; port.read(response,200); Log.i(TAG,"openLight2"+ByteUtil.bytesToString(response)); }catch (Exception e){ e.printStackTrace(); Log.i(TAG,"开灯2失败"); } } public void closeLight2(){ try{ byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x02, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(bytes,200); byte[] response=new byte[12]; port.read(response,200); Log.i(TAG,"closeLight2"+ByteUtil.bytesToString(response)); }catch (Exception e){ e.printStackTrace(); Log.i(TAG,"关灯2失败"); } } public void openLight3(){ try{ byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x03, 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(bytes,200); byte[] response=new byte[12]; port.read(response,200); Log.i(TAG,"openLight3"+ByteUtil.bytesToString(response)); }catch (Exception e){ e.printStackTrace(); Log.i(TAG,"开灯3失败"); } } public void closeLight3(){ try{ byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x03, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(bytes,200); byte[] response=new byte[12]; port.read(response,200); Log.i(TAG,"closeLight3"+ByteUtil.bytesToString(response)); }catch (Exception e){ e.printStackTrace(); Log.i(TAG,"关灯3失败"); } } public void openLight4(){ try{ byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x04, 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(bytes,200); byte[] response=new byte[12]; port.read(response,200); Log.i(TAG,"openLight4"+ByteUtil.bytesToString(response)); }catch (Exception e){ e.printStackTrace(); Log.i(TAG,"开灯4失败"); } } public void closeLight4(){ try{ byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0D, 0x01, 0x66, 0x04, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(bytes,200); byte[] response=new byte[12]; port.read(response,200); Log.i(TAG,"closeLight4"+ByteUtil.bytesToString(response)); }catch (Exception e){ e.printStackTrace(); Log.i(TAG,"关灯4失败"); } } /* * 称重 * *//* public Integer readWeight(){ try{ // 主机发送 : AA BB 0B 01 55 FF FF FF FF FF CC DD // 设备回复: AA BB 0B 01 55 00 00 C9 FF FF CC DD // 00 00 C9 为数据 byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB,0x0B,0x01,0x55,(byte) 0xFF,(byte) 0xFF,(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(bytes,200); byte[] response=new byte[12]; int len=port.read(response,200); Log.i(TAG,"返回称重"+ByteUtil.bytesToString(response)); if(response[4]==85) { byte[] weightBytes = new byte[4]; weightBytes[1] = response[5]; weightBytes[2] = response[6]; weightBytes[3] = response[7]; int weightInt = ByteUtil.bytes2Int(weightBytes); Log.i(TAG,"称重数为:"+weightInt); if(weightInt>=16711680){ weightInt=0; } return weightInt; }else { SPUtils.getInstance().put("RunningState",4); MainViewModel.SendSmsResponse("readWeight称重失灵"); return 0; } }catch(Exception e){ SPUtils.getInstance().put("RunningState",1); MainViewModel.SendSmsResponse("其他设备未检测到"); e.printStackTrace(); Log.i(TAG,"称重失败"); return 0; } } *//*校准流程: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 try{ byte[] bytes=new byte[]{(byte) 0xAA,0x0B ,0x01 ,0x66 ,0x01 ,0x61 ,0x10 , (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(bytes,200); byte[] response=new byte[12]; port.read(response,200); Log.i(TAG,"返回设置刻度"+ByteUtil.bytesToString(response)); if(response[8]==96){ Log.i(TAG,"设置分度成功"); } }catch (Exception e){ e.printStackTrace(); Log.i(TAG,"设置刻度失败"); } } *//* * 称重清零去皮 * *//* 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 try{ byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB,0x0B,0x01,0x66,0x01,0x62,0x63, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(bytes,200); byte[] response=new byte[12]; port.read(response,200); Log.i(TAG,"返回清零"+ByteUtil.bytesToString(response)); if(response[8]==99){ Log.i(TAG,"称重清零成功"); } }catch (Exception e){ e.printStackTrace(); Log.i(TAG,"称重清零去皮失败"); } } *//* * 零度校准 * *//* 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 try{ byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB,0x0B,01,66,01,64,65, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}; port.write(bytes,200); byte[] response=new byte[12]; port.read(response,200); Log.i(TAG,"返回零度校准"+ByteUtil.bytesToString(response)); if (response[8]==101){ Log.i(TAG,"零度校准成功"); } }catch (Exception e){ e.printStackTrace(); Log.i(TAG,"零度校准失败"); } } *//* * 砝码校准 * *//* public void weightCalibration(){ try{ // 主机发送 : AA BB 0B 01 66 01 65 XX XX FF CC DD XX是校准的砝码 只能是单位g 防8kg 就是8000g 十六进制就是1F40 // 设备回复: AA BB 0B 01 66 01 65 00 64 FF CC DD byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB,0x0B ,0x01 ,0x66 ,0x01 ,0x65 ,0x00 ,0x64 , (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};//64 100g // byte[] bytes=new byte[]{(byte) 0xAA, (byte) 0xBB,0x0B ,0x01 ,0x66 ,0x01 ,0x65 ,0x27 ,0x10 , (byte) 0xFF, (byte) 0xCC, (byte) 0xDD};//2710 10000g port.write(bytes,200); byte[] response=new byte[12]; port.read(response,200); Log.i(TAG,"返回砝码校准"+ByteUtil.bytesToString(response)); if(response[8]==100){ Log.i(TAG,"砝码校准成功"); } }catch (Exception e){ e.printStackTrace(); 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(); } } }