Skip to content

Commit

Permalink
3.1.7代码提交 增加读写速度
Browse files Browse the repository at this point in the history
  • Loading branch information
陈力 committed Oct 26, 2023
1 parent cd4bb33 commit 210e809
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 52 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// implementation project(path: ':serial_lib')
implementation 'com.github.cl-6666:serialPort:V3.1.3'
implementation project(path: ':serial_lib')
// implementation 'com.github.cl-6666:serialPort:V3.1.3'
// eventbus
implementation 'org.greenrobot:eventbus:3.2.0'
implementation 'com.github.getActivity:ToastUtils:9.6'
implementation 'com.github.JessYanCoding:AndroidAutoSize:v1.2.1'
implementation 'com.google.code.gson:gson:2.8.6'


}
31 changes: 30 additions & 1 deletion app/src/main/java/com/cl/myapplication/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import android.app.Application;

import com.cl.log.XLogConfig;
import com.google.gson.Gson;
import com.hjq.toast.ToastUtils;
import com.kongqw.serialportlibrary.SerialConfig;
import com.kongqw.serialportlibrary.SerialUtils;

/**
Expand All @@ -19,6 +22,32 @@ public void onCreate() {
super.onCreate();
// 初始化 Toast 框架
ToastUtils.init(this);
SerialUtils.getInstance().init(this,true,"TAG");
SerialUtils.getInstance().init(this,true,"TAG",100);

//初始化日志框架
// XLogConfig logConfig = new XLogConfig.Builder()
// //全局TAG
// .setGlobalTag("TAG")
// //是否包含线程信息
// .setWhetherThread(true)
// //Xlog是否可用
// .setWhetherToPrint(true)
// //是否存储日志到本地 log文件的有效时长,单位毫秒,<=0表示一直有效
// .setStoreLog(true, 0)
// //堆栈的深度
// .setStackDeep(5)
// .setInjectSequence(new XLogConfig.JsonParser() {
// @Override
// public String toJson(Object src) {
// String json = new Gson().toJson(src);
// return json;
// }
// }).build();
//
// SerialConfig serialConfig = new SerialConfig.Builder()
// .setXLogConfig(logConfig)
// .setIntervalSleep(200)
// .build();
// SerialUtils.getInstance().init(this, serialConfig);
}
}
6 changes: 3 additions & 3 deletions serial_lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode 316
versionName "3.1.6"
versionCode 317
versionName "3.1.7"
externalNativeBuild {
cmake {
arguments '-DANDROID_TOOLCHAIN=clang'
Expand Down Expand Up @@ -49,7 +49,7 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'androidx.appcompat:appcompat:1.1.0'
api 'com.github.cl-6666:xlog:V2.0.0'
api 'com.github.cl-6666:xlog:v3.0.0'
testImplementation 'junit:junit:4.12'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.kongqw.serialportlibrary;

import androidx.annotation.NonNull;

import com.cl.log.XLogConfig;

/**
* name:cl
* date:2023/10/26
* desc:串口配置类
*/
public class SerialConfig {

//配置日志相关参数
private XLogConfig xLogConfig;
//串口接收间隔时间
private int intervalSleep;
//串口重连机制
private boolean serialPortReconnection;


public SerialConfig(Builder builder) {
this.xLogConfig=builder.xLogConfig;
this.intervalSleep=builder.intervalSleep;
this.serialPortReconnection=builder.serialPortReconnection;
}


public XLogConfig getxLogConfig() {
return xLogConfig;
}

public int getIntervalSleep() {
return intervalSleep;
}

public boolean isSerialPortReconnection() {
return serialPortReconnection;
}

public static class Builder {

//配置日志相关参数
private XLogConfig xLogConfig;
//串口接收间隔时间
private int intervalSleep = 100;
//串口重连机制
private boolean serialPortReconnection = false;

public Builder setXLogConfig(@NonNull XLogConfig config) {
xLogConfig = config;
return this;
}

public Builder setIntervalSleep(int sleep) {
intervalSleep = sleep;
return this;
}

public Builder setSerialPortReconnection(boolean serialReconnection) {
serialPortReconnection = serialReconnection;
return this;
}

public SerialConfig build() {
return new SerialConfig(this);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.util.Log;

import com.cl.log.XLog;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
Expand All @@ -19,7 +21,7 @@ public class SerialPortFinder {
public SerialPortFinder() {
File file = new File(DRIVERS_PATH);
boolean b = file.canRead();
Log.i(TAG, "SerialPortFinder: file.canRead() = " + b);
XLog.i(TAG, "SerialPortFinder: file.canRead() = " + b);
}

/**
Expand All @@ -36,7 +38,7 @@ private ArrayList<Driver> getDrivers() throws IOException {
String driverName = readLine.substring(0, 0x15).trim();
String[] fields = readLine.split(" +");
if ((fields.length >= 5) && (fields[fields.length - 1].equals(SERIAL_FIELD))) {
Log.d(TAG, "Found new driver " + driverName + " on " + fields[fields.length - 4]);
XLog.d(TAG, "Found new driver " + driverName + " on " + fields[fields.length - 4]);
drivers.add(new Driver(driverName, fields[fields.length - 4]));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.kongqw.serialportlibrary;

import android.app.Application;
import android.os.Handler;
import android.os.Looper;

import com.cl.log.XConsolePrinter;
import com.cl.log.XFilePrinter;
import com.cl.log.XLog;
import com.cl.log.XLogConfig;
import com.cl.log.XLogManager;
Expand Down Expand Up @@ -47,9 +44,7 @@ public class SerialUtils implements OnOpenSerialPortListener, OnSerialPortDataLi

//串口监听
private SerialPortDirectorListens mSerialPortDirectorListens;


private static final Handler HANDLER = new Handler(Looper.getMainLooper());
private SerialConfig mSerialConfig;


private SerialUtils() {
Expand All @@ -64,46 +59,49 @@ public static SerialUtils getInstance() {
return SerialUtilsInstance.INSTANCE;
}

public SerialConfig getmSerialConfig() {
return mSerialConfig;
}

/**
* 新初始化方法,需要在 Application.create 中初始化
*
* @param application 应用的上下文
* @param serialConfig 需要配置的参数
*/
public void init(Application application, SerialConfig serialConfig) {
sApplication = application;
mSerialConfig = serialConfig;
if (serialConfig.getxLogConfig() == null) {
initLog(true, "SerialUtils");
} else {
XLogManager.getInstance().init(serialConfig.getxLogConfig(), new XConsolePrinter());
}
}


/**
* 初始化 串口框架,需要在 Application.create 中初始化
*
* @param application 应用的上下文
*/
public void init(Application application, boolean logSwitch, String logLabel) {
public void init(Application application, boolean logSwitch, String logLabel,int sleep) {
sApplication = application;
//默认设置读写速度100
mSerialConfig = new SerialConfig(new SerialConfig.Builder().setIntervalSleep(sleep));
initLog(logSwitch, logLabel);
}


public void initLog(final boolean logSwitch, final String logLabel) {
XLogManager.init(new XLogConfig() {
@Override
public String getGlobalTag() {
return logLabel;
}

@Override
public boolean enable() {
return logSwitch;
}

@Override
public JsonParser injectJsonParser() {
//TODO 根据需求自行添加
return super.injectJsonParser();
}

@Override
public boolean includeThread() {
return false;
}

@Override
public int stackTraceDepth() {
return 5;
}
}, new XConsolePrinter(), XFilePrinter.getInstance(sApplication.getCacheDir().getAbsolutePath(), 0));
//初始化日志框架
XLogConfig logConfig = new XLogConfig.Builder()
//全局TAG
.setGlobalTag(logLabel)
//Xlog是否可用
.setWhetherToPrint(logSwitch)
.build();
XLogManager.getInstance().init(logConfig, new XConsolePrinter());
}


Expand Down Expand Up @@ -208,42 +206,42 @@ public boolean sendData(SerialPortEnum serialPortEnum, byte[] sendBytes) {
if (serialPortManager1 != null) {
return serialPortManager1.sendBytes(sendBytes);
} else {
XLog.i("请检测当前类型是否初始化--"+serialPortEnum.name());
XLog.i("请检测当前类型是否初始化--" + serialPortEnum.name());
return false;
}
case SERIAL_TWO:
if (serialPortManager2 != null) {
return serialPortManager2.sendBytes(sendBytes);
} else {
XLog.i("请检测当前类型是否初始化--"+serialPortEnum.name());
XLog.i("请检测当前类型是否初始化--" + serialPortEnum.name());
return false;
}
case SERIAL_THREE:
if (serialPortManager3 != null) {
return serialPortManager3.sendBytes(sendBytes);
} else {
XLog.i("请检测当前类型是否初始化--"+serialPortEnum.name());
XLog.i("请检测当前类型是否初始化--" + serialPortEnum.name());
return false;
}
case SERIAL_FOUR:
if (serialPortManager4 != null) {
return serialPortManager4.sendBytes(sendBytes);
} else {
XLog.i("请检测当前类型是否初始化--"+serialPortEnum.name());
XLog.i("请检测当前类型是否初始化--" + serialPortEnum.name());
return false;
}
case SERIAL_FIVE:
if (serialPortManager5 != null) {
return serialPortManager5.sendBytes(sendBytes);
} else {
XLog.i("请检测当前类型是否初始化--"+serialPortEnum.name());
XLog.i("请检测当前类型是否初始化--" + serialPortEnum.name());
return false;
}
case SERIAL_SIX:
if (serialPortManager6 != null) {
return serialPortManager6.sendBytes(sendBytes);
} else {
XLog.i("请检测当前类型是否初始化--"+serialPortEnum.name());
XLog.i("请检测当前类型是否初始化--" + serialPortEnum.name());
return false;
}
default:
Expand All @@ -255,21 +253,21 @@ public boolean sendData(SerialPortEnum serialPortEnum, byte[] sendBytes) {

@Override
public void openState(SerialPortEnum serialPortEnum, File device, SerialStatus status) {
if (mSerialPortDirectorListens!=null){
if (mSerialPortDirectorListens != null) {
mSerialPortDirectorListens.openState(serialPortEnum, device, status);
}
}

@Override
public void onDataReceived(byte[] bytes, SerialPortEnum serialPortEnum) {
if (mSerialPortDirectorListens!=null){
if (mSerialPortDirectorListens != null) {
mSerialPortDirectorListens.onDataReceived(bytes, serialPortEnum);
}
}

@Override
public void onDataSent(byte[] bytes, SerialPortEnum serialPortEnum) {
if (mSerialPortDirectorListens!=null){
if (mSerialPortDirectorListens != null) {
mSerialPortDirectorListens.onDataSent(bytes, serialPortEnum);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.kongqw.serialportlibrary.thread;

import com.cl.log.XLog;
import com.kongqw.serialportlibrary.SerialUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -28,7 +31,7 @@ public void run() {
super.run();
while (!isInterrupted()) {
try {
Thread.sleep(500);
Thread.sleep(SerialUtils.getInstance().getmSerialConfig().getIntervalSleep());
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit 210e809

Please sign in to comment.