diff --git a/SerialPortLibrary/src/main/java/com/giftedcat/serialportlibrary/SerialPortManager.java b/SerialPortLibrary/src/main/java/com/giftedcat/serialportlibrary/SerialPortManager.java index 9e55a13..f963f74 100644 --- a/SerialPortLibrary/src/main/java/com/giftedcat/serialportlibrary/SerialPortManager.java +++ b/SerialPortLibrary/src/main/java/com/giftedcat/serialportlibrary/SerialPortManager.java @@ -34,6 +34,13 @@ public class SerialPortManager extends SerialPort { private Handler mSendingHandler; private SerialPortReadThread mSerialPortReadThread; + public static final int NORMAL = 0; + + public static final int SPLICING = 1; + + /** 数据读取方式*/ + private int readType; + /** * 打开串口 * @@ -42,7 +49,11 @@ public class SerialPortManager extends SerialPort { * @return 打开是否成功 */ public SerialPortManager(File device, int baudRate) { + this(device, baudRate, NORMAL); + } + public SerialPortManager(File device, int baudRate, int readType){ + this.readType = readType; Log.i(TAG, "openSerialPort: " + String.format("打开串口 %s 波特率 %s", device.getPath(), baudRate)); // 校验串口权限 @@ -65,11 +76,11 @@ public SerialPortManager(File device, int baudRate) { if (null != mOnOpenSerialPortListener) { mOnOpenSerialPortListener.onSuccess(device); } + Log.i(TAG, device.getName() + "串口打开成功"); // 开启发送消息的线程 startSendThread(); // 开启接收消息的线程 startReadThread(); - Log.i(TAG, device.getName() + "串口打开成功"); } catch (Exception e) { e.printStackTrace(); if (null != mOnOpenSerialPortListener) { @@ -178,7 +189,7 @@ private void stopSendThread() { * 开启接收消息的线程 */ private void startReadThread() { - mSerialPortReadThread = new SerialPortReadThread(mFileInputStream) { + mSerialPortReadThread = new SerialPortReadThread(mFileInputStream, readType) { @Override public void onDataReceived(byte[] bytes) { if (null != mOnSerialPortDataListener) { diff --git a/SerialPortLibrary/src/main/java/com/giftedcat/serialportlibrary/thread/SerialPortReadThread.java b/SerialPortLibrary/src/main/java/com/giftedcat/serialportlibrary/thread/SerialPortReadThread.java index 151d60e..e0c05c1 100644 --- a/SerialPortLibrary/src/main/java/com/giftedcat/serialportlibrary/thread/SerialPortReadThread.java +++ b/SerialPortLibrary/src/main/java/com/giftedcat/serialportlibrary/thread/SerialPortReadThread.java @@ -1,9 +1,14 @@ package com.giftedcat.serialportlibrary.thread; +import android.content.Context; +import android.os.SystemClock; import android.util.Log; +import com.giftedcat.serialportlibrary.SerialPortManager; + import java.io.IOException; import java.io.InputStream; +import java.util.Locale; /** * Created by giftedcat on 2020/6/13. @@ -18,15 +23,35 @@ public abstract class SerialPortReadThread extends Thread { private InputStream mInputStream; private byte[] mReadBuffer; - public SerialPortReadThread(InputStream inputStream) { + byte[] readBytes = null; + + private int readType; + + public SerialPortReadThread(InputStream inputStream, int readType) { mInputStream = inputStream; mReadBuffer = new byte[1024]; + + this.readType = readType; } @Override public void run() { super.run(); + switch (readType){ + case SerialPortManager.NORMAL: + normalRead(); + break; + case SerialPortManager.SPLICING: + splicingRead(); + break; + } + } + + /** + * 一般使用,等待inputStream卡死返回数据 + * */ + private void normalRead(){ while (!isInterrupted()) { try { if (null == mInputStream) { @@ -54,6 +79,103 @@ public void run() { } } + /** + * 轮询读取,判断inputStream中是否还有数据,还有就拼接 + * */ + private void splicingRead(){ + while (!isInterrupted()) { + if (null == mInputStream) { + return; + } + + Log.i(TAG, "run: "); + int size = 0; + + try { + int i = mInputStream.available(); + if (i == 0) { + size = 0; + } else { + size = mInputStream.read(mReadBuffer); + } + } catch (IOException e) { + e.printStackTrace(); + } + + if (size > 0) { + // 发现有信息后就追加到临时变量 + Log.i("SerialPortReadThread", size + ""); + readBytes = ArrayAppend(readBytes, mReadBuffer, size); + Log.i("SerialPortReadThread", bytesToHexString(readBytes, readBytes.length)); + } else { + // 这次没有数据了 + if (readBytes != null) { + onDataReceived(readBytes); + } + + // 清空,等待下个信息单元 + readBytes = null; + } + + SystemClock.sleep(50); //毫秒 + + } + } + + /** + * 转换字节为十六进制 + * + * @param src + * @param size + * @return + */ + public static String bytesToHexString(byte[] src, int size) { + String ret = ""; + if (src == null || size <= 0) { + return null; + } + for (int i = 0; i < size; i++) { + String hex = Integer.toHexString(src[i] & 0xFF); + if (hex.length() < 2) { + hex = "0" + hex; + } + ret += hex; + } + return ret.toUpperCase(Locale.US); + } + + /** + * 将源数组追加到目标数组 + * + * @param byte_1 Sou1原数组1 + * @param byte_2 Sou2原数组2 + * @param size 长度 + * @return bytestr 返回一个新的数组,包括了原数组1和原数组2 + */ + private byte[] ArrayAppend(byte[] byte_1, byte[] byte_2, int size) { + // java 合并两个byte数组 + + if (byte_1 == null && byte_2 == null) { + return null; + } else if (byte_1 == null) { + byte[] byte_3 = new byte[size]; + System.arraycopy(byte_2, 0, byte_3, 0, size); + return byte_3; + //return byte_2; + } else if (byte_2 == null) { + byte[] byte_3 = new byte[byte_1.length]; + System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length); + return byte_3; + //return byte_1; + } else { + byte[] byte_3 = new byte[byte_1.length + size]; + System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length); + System.arraycopy(byte_2, 0, byte_3, byte_1.length, size); + return byte_3; + } + + } + @Override public synchronized void start() { super.start();