|
@@ -0,0 +1,205 @@
|
|
|
+package com.example.offpay.ictrs232;
|
|
|
+
|
|
|
+import android.util.Log;
|
|
|
+
|
|
|
+import com.example.offpay.event.WmdbIctMessageEvent;
|
|
|
+import com.hboxs.base_library.constant.Global;
|
|
|
+import com.hboxs.base_library.constant.Name;
|
|
|
+import com.hboxs.base_library.util.LogUtils;
|
|
|
+import com.hboxs.base_library.util.PreventSpeedClickUtil;
|
|
|
+import com.orhanobut.hawk.Hawk;
|
|
|
+
|
|
|
+import org.greenrobot.eventbus.EventBus;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+
|
|
|
+import android_serialport_api.SerialPort;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author by AllenJ on 2018/4/20.
|
|
|
+ * <p>
|
|
|
+ * 通过串口用于接收或发送数据
|
|
|
+ */
|
|
|
+
|
|
|
+public class IctSerialPortUtil {
|
|
|
+ private static final String TAG = "SerialPortUtil";
|
|
|
+ private SerialPort serialPort = null;
|
|
|
+ private InputStream inputStream = null;
|
|
|
+ private OutputStream outputStream = null;
|
|
|
+ private ReceiveThread mReceiveThread = null;
|
|
|
+ private boolean isStart = false;
|
|
|
+ private SerialListen mSerialListen;
|
|
|
+
|
|
|
+ //-----------------单例模式 start---------------
|
|
|
+ private IctSerialPortUtil() {
|
|
|
+ }
|
|
|
+
|
|
|
+ private static IctSerialPortUtil sCreditCardMain;
|
|
|
+
|
|
|
+ public static IctSerialPortUtil getSerialPortUtil() {
|
|
|
+ if (sCreditCardMain == null) {
|
|
|
+ synchronized (IctSerialPortUtil.class) {//todo 修改了这里看看情况
|
|
|
+ sCreditCardMain = new IctSerialPortUtil();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sCreditCardMain;
|
|
|
+ }
|
|
|
+ //-----------------单例模式 end---------------
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 打开串口,接收数据
|
|
|
+ * 通过串口,接收单片机发送来的数据
|
|
|
+ */
|
|
|
+ public void openSerialPort(String path, int baudrate, int flags, int parity) {
|
|
|
+ try {
|
|
|
+// serialPort = new SerialPort(new File(path), baudrate, flags, 1, 8, 1);// parity 1表示使用,0表示不使用奇偶校验。ict,如果这里做不好,就会导致接收的数据存在乱码
|
|
|
+ serialPort = new SerialPort(new File(path), baudrate, flags, parity, 8, 1);//威佛
|
|
|
+ //调用对象SerialPort方法,获取串口中"读和写"的数据流
|
|
|
+ inputStream = serialPort.getInputStream();
|
|
|
+ outputStream = serialPort.getOutputStream();
|
|
|
+ isStart = true;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ getSerialPort();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setSerialListen(SerialListen vSerialListen) {
|
|
|
+ mSerialListen = vSerialListen;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关闭串口
|
|
|
+ * 关闭串口中的输入输出流
|
|
|
+ */
|
|
|
+ public void closeSerialPort() {
|
|
|
+ Log.i("test", "关闭串口");
|
|
|
+ try {
|
|
|
+ if (inputStream != null) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+ if (outputStream != null) {
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+ isStart = false;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String lastData = "";
|
|
|
+ /**
|
|
|
+ * 发送数据
|
|
|
+ * 通过串口,发送数据到单片机
|
|
|
+ *
|
|
|
+ * @param data 要发送的数据
|
|
|
+ */
|
|
|
+ public void sendSerialPort(String data) {
|
|
|
+ //如果500发送 并且是12 则跳过
|
|
|
+ //如果500发送 但不是12 则不跳过
|
|
|
+ //如果不是快,那么就直接跳过。
|
|
|
+ if ((!PreventSpeedClickUtil.isFastClick500())&& "12".equals(data)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if ("12".equals(data) && ("1304".equals(lastData)||lastData.startsWith("1302")||lastData.startsWith("1305")||lastData.startsWith("1300"))) {//如果上一次是10 06 ,這一次是12,則
|
|
|
+ //直接技術
|
|
|
+ lastData=data;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //如果传递的数据为12
|
|
|
+ lastData=data;
|
|
|
+ Log.d(TAG, "sendSerialPort: " + data);
|
|
|
+ Log.d(TAG, "sendSerialPort: " + data);
|
|
|
+ Log.d(TAG, "creditCardVendCancel: 取消支付2start"+data);
|
|
|
+ LogUtils.logWrite("SWD:" + data);
|
|
|
+ try {
|
|
|
+ if (outputStream != null) {
|
|
|
+ byte[] sendData = DataUtils.HexToByteArr(data);
|
|
|
+ outputStream.write(sendData);
|
|
|
+ outputStream.flush();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getSerialPort() {
|
|
|
+ if (mReceiveThread == null) {
|
|
|
+ mReceiveThread = new ReceiveThread();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ mReceiveThread.start();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 接收串口数据的线程
|
|
|
+ */
|
|
|
+
|
|
|
+ private class ReceiveThread extends Thread {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ super.run();
|
|
|
+ //条件判断,只要条件为true,则一直执行这个线程
|
|
|
+ StringBuilder sber = new StringBuilder();
|
|
|
+ String communicAtion = Hawk.get(Name.NOTE_COMMUNICATION, Name.WMDB);
|
|
|
+
|
|
|
+ while (isStart) {
|
|
|
+ if (inputStream == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ synchronized (Global.class) {
|
|
|
+ byte[] readData = new byte[1024];
|
|
|
+ try {
|
|
|
+ int size = 0;
|
|
|
+ /** 获取流中数据的量*/
|
|
|
+ int i = inputStream.available();
|
|
|
+ if (i == 0) {
|
|
|
+ size = 0;
|
|
|
+ } else {
|
|
|
+ /** 流中有数据,则添加到临时数组中*/
|
|
|
+ Log.d(TAG, "data run read: ");
|
|
|
+ size = inputStream.read(readData);
|
|
|
+ }
|
|
|
+ if (size > 0) {
|
|
|
+// if (Name.WMDB.equals(communicAtion)) {
|
|
|
+//
|
|
|
+// String ascii = new String(readData, "ascii");
|
|
|
+// if (ascii.contains("\n")) {
|
|
|
+// String substring = ascii.substring(0, size);
|
|
|
+// sber.append(substring);
|
|
|
+//// if (mSerialListen != null) {
|
|
|
+// String[] split = sber.toString().split("\n");
|
|
|
+// for (String s1 : split) {
|
|
|
+// if ("30 03".equals(s1)) {
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+//// EventBus.getDefault().post(new WmdbIctMessageEvent(Name.SOEPAY_WMDB, s1.trim()));
|
|
|
+// }
|
|
|
+// sber.delete(0, sber.length());
|
|
|
+// } else {
|
|
|
+// String substring = ascii.substring(0, size);
|
|
|
+// sber.append(substring);
|
|
|
+// }
|
|
|
+// } else if (Name.ICT.equals(communicAtion)) {
|
|
|
+ String readString = DataUtils.ByteArrToHex(readData, 0, size);
|
|
|
+ EventBus.getDefault().post(new WmdbIctMessageEvent(Name.ICT, readString));
|
|
|
+// }
|
|
|
+ }
|
|
|
+ Thread.sleep(30);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ sber.delete(0, sber.length());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|