UartUtils.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package com.seller.utils;
  2. import android.app.Application;
  3. import com.kongqw.serialportlibrary.Driver;
  4. import com.kongqw.serialportlibrary.SerialUtils;
  5. import com.kongqw.serialportlibrary.enumerate.SerialPortEnum;
  6. import com.kongqw.serialportlibrary.listener.SerialPortDirectorListens;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. public class UartUtils {
  10. private static final byte Start1 = (byte) 0xAA, Start2 = (byte) 0x55;
  11. private static byte[] getCmd(int op, int pra1, int pra2, int pra3) {
  12. return new byte[]{Start1, Start2, (byte) op, (byte) pra1, (byte) pra2, (byte) pra3};
  13. }
  14. public static boolean isDataCorrect(final byte[] data) {
  15. return data.length == 5 && data[0] == Start1 && data[1] == Start2;
  16. }
  17. public static void init(Application app, SerialPortDirectorListens listener) {
  18. SerialUtils.getInstance().setmSerialPortDirectorListens(listener);
  19. SerialUtils.getInstance().init(app, false, "串口", 40);
  20. List<Driver> list2 = new ArrayList<>();
  21. // 串口ttyS0
  22. list2.add(new Driver("/dev/ttyS0", "9600"));
  23. SerialUtils.getInstance().manyOpenSerialPort(list2);
  24. }
  25. public static void close() {
  26. SerialUtils.getInstance().serialPortClose();
  27. }
  28. public static void ReadWeight(int index) {
  29. SerialUtils.getInstance().sendData(
  30. SerialPortEnum.SERIAL_ONE, getCmd(0x01, index, 0x00, 0x00)
  31. );
  32. }
  33. public static void OpenDoor(boolean front) {
  34. SerialUtils.getInstance().sendData(
  35. SerialPortEnum.SERIAL_ONE, getCmd(0x02, front ? 0x01 : 0x00, 0x00, 0x00)
  36. );
  37. }
  38. public static void OpenValve(int index, int pulse) {
  39. SerialUtils.getInstance().sendData(
  40. SerialPortEnum.SERIAL_ONE, getCmd(0x03, index, (pulse & 0xFF00) >> 8, pulse & 0xFF)
  41. );
  42. }
  43. public static void CloseValve(int index) {
  44. SerialUtils.getInstance().sendData(
  45. SerialPortEnum.SERIAL_ONE, getCmd(0x04, index, 0x00, 0x00)
  46. );
  47. }
  48. public static void SwitchLight(int index, boolean on) {
  49. SerialUtils.getInstance().sendData(
  50. SerialPortEnum.SERIAL_ONE, getCmd(0x05, index, on ? 0x01 : 0x00, 0x00)
  51. );
  52. }
  53. public static void SwitchGas(boolean on) {
  54. SerialUtils.getInstance().sendData(
  55. SerialPortEnum.SERIAL_ONE, getCmd(0x06, on ? 0x01 : 0x00, 0x00, 0x00)
  56. );
  57. }
  58. public static void DoorStatus(int index) {
  59. SerialUtils.getInstance().sendData(
  60. SerialPortEnum.SERIAL_ONE, getCmd(0x07, index, 0x00, 0x00)
  61. );
  62. }
  63. }