| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- package com.siwei.recyclebox.rs485;
- import android.content.Context;
- import android.util.Log;
- import com.siwei.recyclebox.deviceUtils.ByteUtil;
- import java.io.IOException;
- import java.util.Timer;
- import java.util.TimerTask;
- import tp.xmaihh.serialport.SerialHelper;
- import tp.xmaihh.serialport.bean.ComBean;
- /**
- * *************
- * 项目名称ai-garbage-box18
- * com.siwei.recyclebox.rs485
- * 2020/12/0818:00
- */
- public class ComWeight {
- private static ComWeight instance;
- private SerialHelper mHelper;
- boolean receivingData = false; //接收数据
- private byte[] readWeightArray; //接收称重数据的数组
- public int num = 0;
- /**
- * 称的配置
- * 厂家 深圳博途
- * baudRate: 波特率 19200
- * dataBits: 数据位 8 UsbSerialPort.DATABITS_8
- * stopBits: 停止位 1 UsbSerialPort.STOPBITS_1
- * parity: 校验位 偶校验 UsbSerialPort.PARITY_EVEN
- * vendorId: 供应商ID 6790
- * onDataReceived: 接口提供的数据监听,不知道怎么用
- */
- private final String TAG = "COM口";
- public static String port = "";
- private static final int btl = 19200;//波特率
- private static final int jyw = 2;//偶校验
- private static final int sjw = 8;//校验位
- private static final int tzw = 1;//停止位
- private static final int lk = 0;//流控
- /**
- * 单例模式
- *
- * @return
- * @author ltb
- * @date 2020/12/08 18:02
- */
- public static ComWeight getInstance() {
- if (instance == null) {
- instance = new ComWeight();
- }
- return instance;
- }
- /**
- * 连接称并配置
- *
- * @return
- * @author ltb
- * @date 2020/12/08 18:18
- */
- public void init() {
- //实例化SerialHelper对象,放入串口号和波特率
- mHelper = new SerialHelper(port, btl) {
- //监听传回数据
- @Override
- protected void onDataReceived(ComBean paramComBean) {
- if (receivingData) {
- readWeightArray = paramComBean.bRec;
- }
- }
- };
- //开始连接串口
- try {
- num = 0 ;
- mHelper.setPort(port);
- mHelper.setBaudRate(btl);
- mHelper.setStopBits(tzw); //设置停止位
- mHelper.setDataBits(sjw); //设置数据位
- mHelper.setParity(jyw); //设置校验位
- mHelper.setFlowCon(lk); //设置流控
- mHelper.open();
- } catch (Exception e) {
- Log.e(TAG, "init: 打开串口失败", e.fillInStackTrace());
- e.printStackTrace();
- }
- }
- public int readWeight() {
- Log.i(TAG, "readWeight: 给" + port + "发信息");
- mHelper.sendHex("1F03002A0001A67C");//发送指令
- //发送数据后开始可以执行监听
- receivingData = true;
- int bytes1 = 0;
- for (int times = 0; times < 5; times++) {
- // mHelper.sendHex("1F03002A0001A67C");//发送指令
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- bytes1 = -4;
- Log.d(TAG, "readWeight: 不是吧阿sir,休眠也能报错");
- }
- Log.i(TAG, "readWeight: " + ByteUtil.bytesToString(readWeightArray));
- if (readWeightArray != null) {
- //用bytes接收数据
- Log.i(TAG, "readWeight: 校验码" + ByteUtil.getCRC(readWeightArray));
- if (readWeightArray[1] == 3 && ByteUtil.getCRC(readWeightArray) == 0 && readWeightArray[0] == 31) {
- byte[] weightBytes = new byte[4];
- weightBytes[2] = readWeightArray[3];
- weightBytes[3] = readWeightArray[4];
- int b = ByteUtil.bytes2Int(weightBytes) * 10;
- if (b > 327670) {
- Log.i(TAG, "readWeight: 读到了,重量为" + -(b - 655360) + "超重!!!");
- b = -(b - 655360);
- } else {
- Log.i(TAG, "readWeight: 读到了,重量为" + b);
- }
- readWeightArray = null;
- receivingData = false;
- return b;
- } else {
- mHelper.sendHex("1F03002A0001A67C");//发送指令
- Log.i(TAG, "readWeight: 返回数据不对,重新发送");
- bytes1 = -3;
- }
- } else {
- num ++;
- bytes1 = -3;
- mHelper.sendHex("1F03002A0001A67C");//发送指令
- Log.i(TAG, "readWeight: 收不到数据,重新发送");
- }
- }
- readWeightArray = null;
- receivingData = false;
- return bytes1;
- }
- /**
- * 关闭串口
- *
- * @return
- * @author ltb
- * @date 2020/12/09 13:20
- */
- public void close() {
- if (mHelper.isOpen()) {
- mHelper.close();
- Log.i(TAG, "close: 串口关闭");
- }
- }
- /**
- * @param * @param null
- * @return
- * @method
- * @description 设置分度值
- * @date: 2020/12/11 17:49
- * @author: ltb
- */
- public String setAccuracy() {
- mHelper.sendHex("1F1000000002042710000098F6");//发送指令
- receivingData = true; //发送数据后开始可以执行监听
- while (true) {//循环监听数据
- a:
- for (int times = 1; times < 3; times++) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- Log.d(TAG, "setAccuracy: 不是吧阿sir,休眠也能报错");
- }
- Log.d(TAG, "setAccuracy: 第" + times + "次查询");
- if (readWeightArray != null) {
- Log.i(TAG, "setAccuracy: " + ByteUtil.bytesToString(readWeightArray));
- readWeightArray = null;
- receivingData = false;
- return "设置成功";
- } else {
- readWeightArray = null;
- receivingData = false;
- return "设置失败";
- }
- }
- break;
- }
- readWeightArray = null;
- receivingData = false;
- return "没有读到数据";
- }
- /**
- * @param * @param null
- * @return
- * @method
- * @description 清零
- * @date: 2020/12/11 17:49
- * @author: ltb
- */
- public String setZero() {
- mHelper.sendHex("1F0600240000CA7F");//发送指令
- receivingData = true; //发送数据后开始可以执行监听
- while (true) {//循环监听数据
- a:
- for (int times = 1; times < 3; times++) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- Log.d(TAG, "setAccuracy: 不是吧阿sir,休眠也能报错");
- }
- Log.d(TAG, "setAccuracy: 第" + times + "次查询");
- if (readWeightArray != null) {
- Log.i(TAG, "setZero: " + ByteUtil.bytesToString(readWeightArray));
- readWeightArray = null;
- receivingData = false;
- return "清零成功";
- } else {
- readWeightArray = null;
- receivingData = false;
- return "清零失败";
- }
- }
- break;
- }
- readWeightArray = null;
- receivingData = false;
- return "没有读到数据";
- }
- /*** 两点标记法 ****/
- /**
- * @param * @param null
- * @return
- * @method
- * @description 零校准/设置起点
- * @date: 2020/12/11 17:49
- * @author: ltb
- */
- public String setZeroCalibration() {
- mHelper.sendHex("1F100008000204000000009221");//发送指令
- receivingData = true; //发送数据后开始可以执行监听
- while (true) {//循环监听数据
- a:
- for (int times = 1; times < 3; times++) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- Log.d(TAG, "setAccuracy: 不是吧阿sir,休眠也能报错");
- }
- Log.d(TAG, "setAccuracy: 第" + times + "次查询");
- if (readWeightArray != null) {
- Log.i(TAG, "setZerocalibration: " + ByteUtil.bytesToString(readWeightArray));
- readWeightArray = null;
- receivingData = false;
- return "校准成功";
- } else {
- readWeightArray = null;
- receivingData = false;
- return "校准失败";
- }
- }
- break;
- }
- readWeightArray = null;
- receivingData = false;
- return "没有读到数据";
- }
- /**
- * @param * @param null
- * @return
- * @method
- * @description 零校准/设置起点
- * @date: 2020/12/11 17:49
- * @author: ltb
- */
- public String setWeightsCalibration() {
- mHelper.sendHex("1F10001000020403E8000012FB");//发送指令
- receivingData = true; //发送数据后开始可以执行监听
- while (true) {//循环监听数据
- a:
- for (int times = 1; times < 3; times++) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- Log.d(TAG, "setAccuracy: 不是吧阿sir,休眠也能报错");
- }
- Log.d(TAG, "setAccuracy: 第" + times + "次查询");
- if (readWeightArray != null) {
- Log.i(TAG, "setWeightsCalibration: " + ByteUtil.bytesToString(readWeightArray));
- readWeightArray = null;
- receivingData = false;
- return "校准成功";
- } else {
- readWeightArray = null;
- receivingData = false;
- return "校准失败";
- }
- }
- break;
- }
- readWeightArray = null;
- receivingData = false;
- return "没有读到数据";
- }
- }
|