lmw
2024-09-04 a3bc6aa4fa3bad787c9fc366d6904f60265eec3b
app/src/main/java/android_serialport_api/SerialPortUtils.java
@@ -2,10 +2,11 @@
import android.util.Log;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.lotaai.canguiayw.CThreadPoolExecutor;
import com.lotaai.canguiayw.application.App;
import com.lotaai.canguiayw.common.SettingConfig;
import com.pranavpandey.android.dynamic.toasts.DynamicToast;
import java.io.File;
import java.io.IOException;
@@ -34,9 +35,10 @@
    /**
     * 打开串口
     *
     * @return serialPort串口对象
     */
    public SerialPort openSerialPort(String path, int baudrate){
    public SerialPort openSerialPort(final String path, final int baudrate) {
        try {
            serialPort = new SerialPort(new File(path), baudrate, 0);
            this.serialPortStatus = true;
@@ -58,10 +60,18 @@
                                if (onDataReceiveListener!=null) {
                                    onDataReceiveListener.onDataReceive(buffer, size);
                                }
//                                LogUtils.i(TAG, "锁串口返回:" + ConvertUtils.bytes2HexString(buffer));
                            }
                        } catch (IOException e) {
                        } catch (final IOException e) {
                            LogUtils.e(TAG, "锁串口返回run: 数据读取异常:" +e.toString());
                            CThreadPoolExecutor.runOnMainThread(new Runnable() {
                                @Override
                                public void run() {
                                    DynamicToast.makeSuccess(App.app, "锁串口返回run: 数据读取异常:" + e.toString(), 3).show();
                                }
                            });
                        }
                    }
                }
@@ -69,9 +79,21 @@
            SettingConfig.getInstance().getExecutor().execute(syncRunnable);
        } catch (IOException e) {
            LogUtils.e(TAG, "openSerialPort: 打开串口异常:" + e.toString());
            CThreadPoolExecutor.runOnMainThread(new Runnable() {
                @Override
                public void run() {
                }
            });
            return serialPort;
        }
        LogUtils.i(TAG, "openSerialPort: 打开串口:" + path + ", 波特率:" + baudrate);
        CThreadPoolExecutor.runOnMainThread(new Runnable() {
            @Override
            public void run() {
                DynamicToast.makeSuccess(App.app, "打开串口:" + path + ", 波特率:" + baudrate, 3).show();
            }
        });
        return serialPort;
    }
@@ -117,16 +139,24 @@
                outputStream.write(sendData);
                outputStream.flush();
            }
        } catch (IOException e) {
        } catch (final IOException e) {
            LogUtils.e(TAG, "sendSerialPort: 串口数据发送失败:"+e.toString());
            CThreadPoolExecutor.runOnMainThread(new Runnable() {
                @Override
                public void run() {
                    DynamicToast.makeSuccess(App.app,  "串口数据发送失败:" + e.toString(), 3).show();
                }
            });
        }
    }
    //这是写了一监听器来监听接收数据
    public OnDataReceiveListener onDataReceiveListener = null;
    public interface OnDataReceiveListener {
        void onDataReceive(byte[] buffer, int size);
    }
    public void setOnDataReceiveListener(OnDataReceiveListener dataReceiveListener) {
        onDataReceiveListener = dataReceiveListener;
    }