ComWeight.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package com.siwei.recyclebox.rs485;
  2. import android.content.Context;
  3. import android.util.Log;
  4. import com.siwei.recyclebox.deviceUtils.ByteUtil;
  5. import java.io.IOException;
  6. import java.util.Timer;
  7. import java.util.TimerTask;
  8. import tp.xmaihh.serialport.SerialHelper;
  9. import tp.xmaihh.serialport.bean.ComBean;
  10. /**
  11. * *************
  12. * 项目名称ai-garbage-box18
  13. * com.siwei.recyclebox.rs485
  14. * 2020/12/0818:00
  15. */
  16. public class ComWeight {
  17. private static ComWeight instance;
  18. private SerialHelper mHelper;
  19. boolean receivingData = false; //接收数据
  20. private byte[] readWeightArray; //接收称重数据的数组
  21. public int num = 0;
  22. /**
  23. * 称的配置
  24. * 厂家 深圳博途
  25. * baudRate: 波特率 19200
  26. * dataBits: 数据位 8 UsbSerialPort.DATABITS_8
  27. * stopBits: 停止位 1 UsbSerialPort.STOPBITS_1
  28. * parity: 校验位 偶校验 UsbSerialPort.PARITY_EVEN
  29. * vendorId: 供应商ID 6790
  30. * onDataReceived: 接口提供的数据监听,不知道怎么用
  31. */
  32. private final String TAG = "COM口";
  33. public static String port = "";
  34. private static final int btl = 19200;//波特率
  35. private static final int jyw = 2;//偶校验
  36. private static final int sjw = 8;//校验位
  37. private static final int tzw = 1;//停止位
  38. private static final int lk = 0;//流控
  39. /**
  40. * 单例模式
  41. *
  42. * @return
  43. * @author ltb
  44. * @date 2020/12/08 18:02
  45. */
  46. public static ComWeight getInstance() {
  47. if (instance == null) {
  48. instance = new ComWeight();
  49. }
  50. return instance;
  51. }
  52. /**
  53. * 连接称并配置
  54. *
  55. * @return
  56. * @author ltb
  57. * @date 2020/12/08 18:18
  58. */
  59. public void init() {
  60. //实例化SerialHelper对象,放入串口号和波特率
  61. mHelper = new SerialHelper(port, btl) {
  62. //监听传回数据
  63. @Override
  64. protected void onDataReceived(ComBean paramComBean) {
  65. if (receivingData) {
  66. readWeightArray = paramComBean.bRec;
  67. }
  68. }
  69. };
  70. //开始连接串口
  71. try {
  72. num = 0 ;
  73. mHelper.setPort(port);
  74. mHelper.setBaudRate(btl);
  75. mHelper.setStopBits(tzw); //设置停止位
  76. mHelper.setDataBits(sjw); //设置数据位
  77. mHelper.setParity(jyw); //设置校验位
  78. mHelper.setFlowCon(lk); //设置流控
  79. mHelper.open();
  80. } catch (Exception e) {
  81. Log.e(TAG, "init: 打开串口失败", e.fillInStackTrace());
  82. e.printStackTrace();
  83. }
  84. }
  85. public int readWeight() {
  86. Log.i(TAG, "readWeight: 给" + port + "发信息");
  87. mHelper.sendHex("1F03002A0001A67C");//发送指令
  88. //发送数据后开始可以执行监听
  89. receivingData = true;
  90. int bytes1 = 0;
  91. for (int times = 0; times < 5; times++) {
  92. // mHelper.sendHex("1F03002A0001A67C");//发送指令
  93. try {
  94. Thread.sleep(500);
  95. } catch (InterruptedException e) {
  96. e.printStackTrace();
  97. bytes1 = -4;
  98. Log.d(TAG, "readWeight: 不是吧阿sir,休眠也能报错");
  99. }
  100. Log.i(TAG, "readWeight: " + ByteUtil.bytesToString(readWeightArray));
  101. if (readWeightArray != null) {
  102. //用bytes接收数据
  103. Log.i(TAG, "readWeight: 校验码" + ByteUtil.getCRC(readWeightArray));
  104. if (readWeightArray[1] == 3 && ByteUtil.getCRC(readWeightArray) == 0 && readWeightArray[0] == 31) {
  105. byte[] weightBytes = new byte[4];
  106. weightBytes[2] = readWeightArray[3];
  107. weightBytes[3] = readWeightArray[4];
  108. int b = ByteUtil.bytes2Int(weightBytes) * 10;
  109. if (b > 327670) {
  110. Log.i(TAG, "readWeight: 读到了,重量为" + -(b - 655360) + "超重!!!");
  111. b = -(b - 655360);
  112. } else {
  113. Log.i(TAG, "readWeight: 读到了,重量为" + b);
  114. }
  115. readWeightArray = null;
  116. receivingData = false;
  117. return b;
  118. } else {
  119. mHelper.sendHex("1F03002A0001A67C");//发送指令
  120. Log.i(TAG, "readWeight: 返回数据不对,重新发送");
  121. bytes1 = -3;
  122. }
  123. } else {
  124. num ++;
  125. bytes1 = -3;
  126. mHelper.sendHex("1F03002A0001A67C");//发送指令
  127. Log.i(TAG, "readWeight: 收不到数据,重新发送");
  128. }
  129. }
  130. readWeightArray = null;
  131. receivingData = false;
  132. return bytes1;
  133. }
  134. /**
  135. * 关闭串口
  136. *
  137. * @return
  138. * @author ltb
  139. * @date 2020/12/09 13:20
  140. */
  141. public void close() {
  142. if (mHelper.isOpen()) {
  143. mHelper.close();
  144. Log.i(TAG, "close: 串口关闭");
  145. }
  146. }
  147. /**
  148. * @param * @param null
  149. * @return
  150. * @method
  151. * @description 设置分度值
  152. * @date: 2020/12/11 17:49
  153. * @author: ltb
  154. */
  155. public String setAccuracy() {
  156. mHelper.sendHex("1F1000000002042710000098F6");//发送指令
  157. receivingData = true; //发送数据后开始可以执行监听
  158. while (true) {//循环监听数据
  159. a:
  160. for (int times = 1; times < 3; times++) {
  161. try {
  162. Thread.sleep(1000);
  163. } catch (InterruptedException e) {
  164. e.printStackTrace();
  165. Log.d(TAG, "setAccuracy: 不是吧阿sir,休眠也能报错");
  166. }
  167. Log.d(TAG, "setAccuracy: 第" + times + "次查询");
  168. if (readWeightArray != null) {
  169. Log.i(TAG, "setAccuracy: " + ByteUtil.bytesToString(readWeightArray));
  170. readWeightArray = null;
  171. receivingData = false;
  172. return "设置成功";
  173. } else {
  174. readWeightArray = null;
  175. receivingData = false;
  176. return "设置失败";
  177. }
  178. }
  179. break;
  180. }
  181. readWeightArray = null;
  182. receivingData = false;
  183. return "没有读到数据";
  184. }
  185. /**
  186. * @param * @param null
  187. * @return
  188. * @method
  189. * @description 清零
  190. * @date: 2020/12/11 17:49
  191. * @author: ltb
  192. */
  193. public String setZero() {
  194. mHelper.sendHex("1F0600240000CA7F");//发送指令
  195. receivingData = true; //发送数据后开始可以执行监听
  196. while (true) {//循环监听数据
  197. a:
  198. for (int times = 1; times < 3; times++) {
  199. try {
  200. Thread.sleep(1000);
  201. } catch (InterruptedException e) {
  202. e.printStackTrace();
  203. Log.d(TAG, "setAccuracy: 不是吧阿sir,休眠也能报错");
  204. }
  205. Log.d(TAG, "setAccuracy: 第" + times + "次查询");
  206. if (readWeightArray != null) {
  207. Log.i(TAG, "setZero: " + ByteUtil.bytesToString(readWeightArray));
  208. readWeightArray = null;
  209. receivingData = false;
  210. return "清零成功";
  211. } else {
  212. readWeightArray = null;
  213. receivingData = false;
  214. return "清零失败";
  215. }
  216. }
  217. break;
  218. }
  219. readWeightArray = null;
  220. receivingData = false;
  221. return "没有读到数据";
  222. }
  223. /*** 两点标记法 ****/
  224. /**
  225. * @param * @param null
  226. * @return
  227. * @method
  228. * @description 零校准/设置起点
  229. * @date: 2020/12/11 17:49
  230. * @author: ltb
  231. */
  232. public String setZeroCalibration() {
  233. mHelper.sendHex("1F100008000204000000009221");//发送指令
  234. receivingData = true; //发送数据后开始可以执行监听
  235. while (true) {//循环监听数据
  236. a:
  237. for (int times = 1; times < 3; times++) {
  238. try {
  239. Thread.sleep(1000);
  240. } catch (InterruptedException e) {
  241. e.printStackTrace();
  242. Log.d(TAG, "setAccuracy: 不是吧阿sir,休眠也能报错");
  243. }
  244. Log.d(TAG, "setAccuracy: 第" + times + "次查询");
  245. if (readWeightArray != null) {
  246. Log.i(TAG, "setZerocalibration: " + ByteUtil.bytesToString(readWeightArray));
  247. readWeightArray = null;
  248. receivingData = false;
  249. return "校准成功";
  250. } else {
  251. readWeightArray = null;
  252. receivingData = false;
  253. return "校准失败";
  254. }
  255. }
  256. break;
  257. }
  258. readWeightArray = null;
  259. receivingData = false;
  260. return "没有读到数据";
  261. }
  262. /**
  263. * @param * @param null
  264. * @return
  265. * @method
  266. * @description 零校准/设置起点
  267. * @date: 2020/12/11 17:49
  268. * @author: ltb
  269. */
  270. public String setWeightsCalibration() {
  271. mHelper.sendHex("1F10001000020403E8000012FB");//发送指令
  272. receivingData = true; //发送数据后开始可以执行监听
  273. while (true) {//循环监听数据
  274. a:
  275. for (int times = 1; times < 3; times++) {
  276. try {
  277. Thread.sleep(1000);
  278. } catch (InterruptedException e) {
  279. e.printStackTrace();
  280. Log.d(TAG, "setAccuracy: 不是吧阿sir,休眠也能报错");
  281. }
  282. Log.d(TAG, "setAccuracy: 第" + times + "次查询");
  283. if (readWeightArray != null) {
  284. Log.i(TAG, "setWeightsCalibration: " + ByteUtil.bytesToString(readWeightArray));
  285. readWeightArray = null;
  286. receivingData = false;
  287. return "校准成功";
  288. } else {
  289. readWeightArray = null;
  290. receivingData = false;
  291. return "校准失败";
  292. }
  293. }
  294. break;
  295. }
  296. readWeightArray = null;
  297. receivingData = false;
  298. return "没有读到数据";
  299. }
  300. }