package com.seller.utils; import android.app.Application; import com.kongqw.serialportlibrary.Driver; import com.kongqw.serialportlibrary.SerialUtils; import com.kongqw.serialportlibrary.enumerate.SerialPortEnum; import com.kongqw.serialportlibrary.listener.SerialPortDirectorListens; import java.util.ArrayList; import java.util.List; public class UartUtils { private static final byte Start1 = (byte) 0xAA, Start2 = (byte) 0x55; private static byte[] getCmd(int op, int pra1, int pra2, int pra3) { return new byte[]{Start1, Start2, (byte) op, (byte) pra1, (byte) pra2, (byte) pra3}; } public static boolean isDataCorrect(final byte[] data) { return data.length == 5 && data[0] == Start1 && data[1] == Start2; } public static void init(Application app, SerialPortDirectorListens listener) { SerialUtils.getInstance().setmSerialPortDirectorListens(listener); SerialUtils.getInstance().init(app, false, "串口", 40); List list2 = new ArrayList<>(); // 串口ttyS0 list2.add(new Driver("/dev/ttyS0", "9600")); SerialUtils.getInstance().manyOpenSerialPort(list2); } public static void close() { SerialUtils.getInstance().serialPortClose(); } public static void ReadWeight(int index) { SerialUtils.getInstance().sendData( SerialPortEnum.SERIAL_ONE, getCmd(0x01, index, 0x00, 0x00) ); } public static void OpenDoor(boolean front) { SerialUtils.getInstance().sendData( SerialPortEnum.SERIAL_ONE, getCmd(0x02, front ? 0x01 : 0x00, 0x00, 0x00) ); } public static void OpenValve(int index, int pulse) { SerialUtils.getInstance().sendData( SerialPortEnum.SERIAL_ONE, getCmd(0x03, index, (pulse & 0xFF00) >> 8, pulse & 0xFF) ); } public static void CloseValve(int index) { SerialUtils.getInstance().sendData( SerialPortEnum.SERIAL_ONE, getCmd(0x04, index, 0x00, 0x00) ); } public static void SwitchLight(int index, boolean on) { SerialUtils.getInstance().sendData( SerialPortEnum.SERIAL_ONE, getCmd(0x05, index, on ? 0x01 : 0x00, 0x00) ); } public static void SwitchGas(boolean on) { SerialUtils.getInstance().sendData( SerialPortEnum.SERIAL_ONE, getCmd(0x06, on ? 0x01 : 0x00, 0x00, 0x00) ); } public static void DoorStatus(int index) { SerialUtils.getInstance().sendData( SerialPortEnum.SERIAL_ONE, getCmd(0x07, index, 0x00, 0x00) ); } }