SerialPortUtil.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package serialporttest.utils;
  2. import android.text.TextUtils;
  3. import android.util.Log;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import android_serialport_api.SerialPort;
  9. /**
  10. * Created by Administrator on 2017/6/6.
  11. */
  12. public class SerialPortUtil {
  13. private String TAG = "SerialPortUtil";
  14. public SerialPort serialPort = null;
  15. public InputStream inputStream = null;
  16. public OutputStream outputStream = null;
  17. ISerialPortBedOnclickEvent onDataReceiveListener = null;
  18. ISerialPortBedOnclickString onDataReceiveStringListener = null;
  19. public boolean isOpenSerialPortUtil = false;
  20. private static byte[] KeyValue = new byte[8];
  21. int DataIndex = 0;
  22. int DataValue = -1;
  23. public Thread receiveThread = null;
  24. public SerialPortUtil() {
  25. }
  26. /**
  27. * 打开串口的方法
  28. */
  29. public void openSerialPort() {
  30. Log.i(TAG, "打开串口");
  31. try {
  32. serialPort = new SerialPort(new File("/dev/" + getSystemPort()), getSystemBaudrate(), 0);
  33. //获取打开的串口中的输入输出流,以便于串口数据的收发
  34. inputStream = serialPort.getInputStream();
  35. outputStream = serialPort.getOutputStream();
  36. isOpenSerialPortUtil = true;
  37. receiveSerialPort();
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. /**
  43. * 接收串口数据的方法
  44. */
  45. private byte[] buffer;
  46. private String data;
  47. public byte[] re_buffer = new byte[2048];
  48. String data1 = "";
  49. public int writeIndex = 0, readIndex = 0;
  50. public boolean finshFlag = false;
  51. /**
  52. * 请求设备维一序列号(设备 ID)
  53. */
  54. public static String KEY_ID = "";
  55. public void receiveSerialPort() {
  56. initKeyValue();
  57. Log.i(TAG, "接收串口数据");
  58. if (receiveThread != null)
  59. return;
  60. buffer = new byte[1024];
  61. receiveThread = new Thread() {
  62. @Override
  63. public void run() {
  64. while (isOpenSerialPortUtil) {
  65. try {
  66. if (inputStream == null) {
  67. return;
  68. }
  69. int size = inputStream.read(buffer);
  70. if (size > 0 && isOpenSerialPortUtil) {
  71. data = new String(buffer, 0, size);
  72. Log.d("aaaa", "data==" + data);
  73. if (systemVersionIsO()) {//安卓7.0.1 or 8.1.0 版用
  74. //addByData(data);//my do
  75. changeData(size);//吴总改了后的
  76. } else {//安卓4.2.0 版用
  77. sendData(data);
  78. }
  79. }
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. }
  85. };
  86. //启动接收线程
  87. receiveThread.start();
  88. }
  89. String addData = "";
  90. public synchronized void addByData(String data) {
  91. addData = addData + data;
  92. if (StringUtils.vagueInquiry(data, "$") && StringUtils.vagueInquiry(data, "#")) {
  93. sendData(addData);
  94. addData = "";
  95. }
  96. }
  97. public void changeData(int size) {
  98. if (size > 0) {
  99. for (int i = 0; i < size; i++) {
  100. re_buffer[writeIndex] = buffer[i];
  101. writeIndex++;
  102. if (writeIndex > 2047) {
  103. writeIndex = 0;
  104. }
  105. }
  106. }
  107. if (readIndex != writeIndex) {
  108. while (readIndex != writeIndex) {
  109. if (re_buffer[readIndex] == '$') {
  110. data1 = "$";
  111. readIndex++;
  112. if (readIndex > 2047) {
  113. readIndex = 0;
  114. }
  115. } else if (re_buffer[readIndex] == '#') {
  116. data1 = data1 + "#";
  117. readIndex++;
  118. if (readIndex > 2047) {
  119. readIndex = 0;
  120. }
  121. finshFlag = true;
  122. break;
  123. } else if (data1.length() > 0) {
  124. data1 = data1 + new String(re_buffer, readIndex, 1);
  125. readIndex++;
  126. if (readIndex > 2047) {
  127. readIndex = 0;
  128. }
  129. } else {
  130. readIndex++;
  131. if (readIndex > 2047) {
  132. readIndex = 0;
  133. }
  134. }
  135. }
  136. }
  137. if (finshFlag) {
  138. sendData(data1);
  139. data1 = "";
  140. finshFlag = false;
  141. }
  142. }
  143. public void sendData(String data) {
  144. Log.d("bbbb", "data==" + data);
  145. if (!StringUtils.notEmpty(data)) return;
  146. if (isOpenSerialPortUtil) {
  147. if (data.contains("ID")) {
  148. String str = data.substring(data.indexOf(",") + 1, data.indexOf("1#"));
  149. int i = 0;
  150. while (i < 10) {
  151. if (str.length() > 4) {
  152. break;
  153. } else {
  154. try {
  155. Thread.sleep(100);
  156. } catch (InterruptedException e) {
  157. e.printStackTrace();
  158. }
  159. getKeyId();
  160. i++;
  161. }
  162. }
  163. Log.e(TAG, str);
  164. KEY_ID = str;
  165. } else {
  166. if (data.contains("$") && data.contains("#")) {
  167. if (null != onDataReceiveStringListener) {//add by waderson 20190708
  168. onDataReceiveStringListener.serialPortBedOnclickString(data);
  169. }
  170. String str = data.substring(data.indexOf("$") + 1, data.indexOf("#"));
  171. if (TextUtils.isDigitsOnly(str.substring(3, 4)) && TextUtils.isDigitsOnly(str.substring(5, 6))) {
  172. DataIndex = Integer.parseInt(str.substring(3, 4));
  173. DataValue = Integer.parseInt(str.substring(5, 6));
  174. if (DataIndex < 8) {
  175. KeyValue[DataIndex] = (byte) DataValue;
  176. }
  177. if (null != onDataReceiveListener) {
  178. onDataReceiveListener.serialPortBedOnclick(KeyValue);
  179. }
  180. }
  181. //======================================
  182. for (int i = 0; i < KeyValue.length; i++) {
  183. if (KeyValue[i] > 0) KeyValue[i] = -1;
  184. }
  185. //======================================
  186. }
  187. }
  188. }
  189. }
  190. /**
  191. * 判断是否是7.1以上的系统版本
  192. *
  193. * @return
  194. */
  195. public static boolean systemVersionIsO() {
  196. if (android.os.Build.VERSION.SDK_INT >= 24) {//7.1以上的系统
  197. return true;
  198. } else {
  199. return false;
  200. }
  201. }
  202. /**
  203. * 关闭串口的方法
  204. * 关闭串口中的输入输出流
  205. * 然后将flag的值设为flag,终止接收数据线程
  206. */
  207. public void closeSerialPort() {
  208. Log.i(TAG, "关闭串口");
  209. try {
  210. if (inputStream != null) {
  211. inputStream.close();
  212. }
  213. if (outputStream != null) {
  214. outputStream.close();
  215. }
  216. if (receiveThread != null && receiveThread.isAlive()) {
  217. receiveThread.interrupt();
  218. receiveThread = null;
  219. }
  220. isOpenSerialPortUtil = false;
  221. } catch (IOException e) {
  222. e.printStackTrace();
  223. }
  224. }
  225. //初始化 key值
  226. public static void initKeyValue() {
  227. KeyValue[0] = -1;
  228. KeyValue[1] = -1;
  229. KeyValue[2] = -1;
  230. KeyValue[3] = -1;
  231. KeyValue[4] = -1;
  232. KeyValue[5] = -1;
  233. KeyValue[6] = -1;
  234. KeyValue[7] = -1;
  235. }
  236. /**
  237. * 心跳信号
  238. */
  239. public void startHeart() {
  240. send("$HEART,1F#");
  241. }
  242. /**
  243. * 关闭心跳<br>
  244. * 若MCU在10秒内没有收到信号,将自动重启Android. 随机数为“W”时将关闭心跳<br>
  245. */
  246. public void closeHeart() {
  247. send("$HEART,WE#");
  248. }
  249. /**
  250. * 系统重启
  251. */
  252. public void systemRestart() {
  253. send("$SYSRESET,2D#");
  254. Log.i(TAG, "系统重启发送");
  255. }
  256. /*
  257. * 进入系统ROM升级模式
  258. * */
  259. public void systemUpDate() {
  260. Log.i(TAG, "系统ROM升级模式串口数据");
  261. send("$SYSUPDATE,3C#");
  262. Log.i(TAG, "系统ROM升级模式发送");
  263. }
  264. /**
  265. * 发送串口数据的方法
  266. *
  267. * @param command 要发送的数据
  268. */
  269. private void send(String command) {
  270. try {
  271. if (isOpenSerialPortUtil) {
  272. byte[] sendData = command.getBytes();
  273. outputStream.write(sendData);
  274. Log.d("NURSELIGHT","==command=="+"护理灯串口数据发送成功");
  275. Log.i(TAG, "串口数据发送成功");
  276. }
  277. } catch (IOException e) {
  278. e.printStackTrace();
  279. Log.d("NURSELIGHT","==command=="+"护理灯串口数据发送失败");
  280. Log.i(TAG, "串口数据发送失败");
  281. }
  282. }
  283. public void setOnDataReceiveListener(ISerialPortBedOnclickEvent dataReceiveListener) {
  284. onDataReceiveListener = dataReceiveListener;
  285. }
  286. public void setOnDataReceiveStringListener(ISerialPortBedOnclickString dataReceiveStringListener) {
  287. onDataReceiveStringListener = dataReceiveStringListener;
  288. }
  289. // private void saveKeyValue() {
  290. // if (timer != null) {
  291. // timer.purge();
  292. // }
  293. // timer = new Timer();
  294. //
  295. // if (timerTask != null) {
  296. // timerTask.cancel();
  297. // }
  298. // timerTask = new TimerTask() {
  299. // @Override
  300. // public void run() {
  301. // if (KeyValue[DataIndex] > 0) {
  302. // initKeyValue();
  303. // }
  304. // }
  305. // };
  306. // timer.schedule(timerTask, 10, mTimeInterval);
  307. // }
  308. public static int getSystemVersion() {
  309. return android.os.Build.VERSION.SDK_INT;
  310. }
  311. public static String getSystemPort() {
  312. String port;
  313. if (getSystemVersion() >= 26) { //android8.0 = 26
  314. port = "ttyS4";// rk3368 android8.1
  315. } else if (getSystemVersion() >= 24) { //android7.0 = 24
  316. port = "ttyS1";//rk3128 android7.1
  317. } else {
  318. port = "ttyS2";// a33 android4.4
  319. }
  320. return port;
  321. }
  322. /**
  323. * 得到波特率
  324. *
  325. * @return String
  326. */
  327. public static int getSystemBaudrate() {
  328. int baudrate;
  329. if (getSystemVersion() >= 26) { //android8.0 = 26
  330. baudrate = 115200;// rk3368 android8.1
  331. } else if (getSystemVersion() >= 24) { //android7.0 = 24
  332. baudrate = 115200;//rk3128 android7.1
  333. } else {
  334. baudrate = 115200;// a20 android4.2
  335. }
  336. return baudrate;
  337. }
  338. public interface ISerialPortBedOnclickEvent {
  339. void serialPortBedOnclick(byte[] buffer);
  340. }
  341. public interface ISerialPortBedOnclickString {
  342. void serialPortBedOnclickString(String str);
  343. }
  344. //------------------------below things was add by Waderson 20171106 -----------------------------------
  345. public static final String C_HEARD = "$";//开头符
  346. public static final String C_END = "#";//结束符
  347. public static final String C_SEPARATE = ",";//分隔符
  348. /**
  349. * 手柄MIC切换<br>
  350. */
  351. public static final String MIC = "MIC";
  352. /**
  353. * 床头灯的切换<br> 0 关闭 1打开 2闪烁
  354. */
  355. public static final String BEDLIGHT = "RELAY";
  356. /**
  357. * 门灯控制开关
  358. */
  359. public static final String DOORLIGHT = "DOORLED";
  360. /**
  361. * 卫生间呼叫灯控制<br>
  362. */
  363. public static final String ULED = "ULED";
  364. /**
  365. * 护理灯光控制<br>
  366. */
  367. public static final String NURSELIGHT = "NLED";
  368. /**
  369. * 当前 SIP 协议状态<br>
  370. */
  371. public static final String SIP_STATUS = "SIP";
  372. /**
  373. * 心跳控制<br>
  374. * 若MCU在10秒内没有收到信号,将自动重启Android. 随机数为“W”时将关闭心跳<br>
  375. * $ HEART ,1 E # <br>
  376. */
  377. /**
  378. * 写入串口<br>
  379. * Waderson 20171103
  380. * command 命令 <br>
  381. * random 随机数<br>
  382. * check 校验符<br>
  383. */
  384. public void sendCommand(String command, String random, String check) {//$NLED3,beComeDoublF#
  385. String random_v = "1", check_v = "F";
  386. if (null == command || "".equals(command)) {// beComeDoubleStr(rr) + beComeDoubleStr(gg) + beComeDoubleStr(bb), "F"
  387. return;
  388. }
  389. if (null != random && !"".equals(random)) {
  390. random_v = random;
  391. }
  392. if (null != check && !"".equals(check)) {
  393. check_v = check;
  394. }
  395. Log.d("NURSELIGHT","=="+C_HEARD + command + C_SEPARATE + random_v + check_v + C_END);
  396. send(C_HEARD + command + C_SEPARATE + random_v + check_v + C_END);
  397. }
  398. /**
  399. * 请求设备维一序列号(设备 ID)
  400. */
  401. public void getKeyId() {
  402. send("$ID,11#");
  403. }
  404. }