desc:优化串口Lib

main
xiaowusky 2 years ago
parent f3a207b7a9
commit 9aec189a06

@ -13,25 +13,38 @@ public class BaseTask implements ITask {
private TaskPriority mTaskPriority = TaskPriority.DEFAULT; //默认优先级 private TaskPriority mTaskPriority = TaskPriority.DEFAULT; //默认优先级
private int mSequence;// 入队次序 private int mSequence;// 入队次序
private Boolean mTaskStatus = false; // 标志任务状态,是否仍在展示 private Boolean mTaskStatus = false; // 标志任务状态,是否仍在展示
protected WeakReference<BlockTaskQueue> taskQueue;//阻塞队列 protected WeakReference<TaskScheduler> taskScheduler;
protected WeakReference<BlockTaskQueue> taskQueue;//消息队列阻塞队列
protected int duration = 0; //任务执行时间 protected int duration = 0; //任务执行时间
//此队列用来实现任务时间不确定的队列阻塞功能 //此队列用来实现任务时间不确定的队列阻塞功能
private PriorityBlockingQueue<Integer> blockQueue; private PriorityBlockingQueue<Integer> blockQueue;
//关联调度器
public void attachScheduler(TaskScheduler taskScheduler) {
this.taskScheduler = new WeakReference<>(taskScheduler);
this.taskQueue = new WeakReference<>(taskScheduler.getQueue());
}
//构造函数 //构造函数
public BaseTask() { public BaseTask() {
taskQueue = new WeakReference<>(BlockTaskQueue.getInstance());
blockQueue = new PriorityBlockingQueue<>(); blockQueue = new PriorityBlockingQueue<>();
} }
//入队实现 //入队实现
@Override @Override
public void enqueue() { public void enqueue() throws Exception {
TaskScheduler.getInstance().enqueue(this); if (taskScheduler == null) {
throw new Exception("必须关联调度器使用");
}
taskScheduler.get().enqueue(this);
} }
//执行任务方法此时标记为设为true并且将当前任务记录下来 //执行任务方法此时标记为设为true并且将当前任务记录下来
@Override @Override
public void doTask() { public void doTask() {
mTaskStatus = true; mTaskStatus = true;
} }
//任务执行完成,改变标记位,将任务在队列中移除,并且把记录清除 //任务执行完成,改变标记位,将任务在队列中移除,并且把记录清除
@Override @Override
public void finishTask() { public void finishTask() {
@ -39,47 +52,56 @@ public class BaseTask implements ITask {
this.taskQueue.get().remove(this); this.taskQueue.get().remove(this);
Log.d(TAG, taskQueue.get().size() + ""); Log.d(TAG, taskQueue.get().size() + "");
} }
//设置任务优先级实现 //设置任务优先级实现
@Override @Override
public ITask setPriority(TaskPriority mTaskPriority) { public ITask setPriority(TaskPriority mTaskPriority) {
this.mTaskPriority = mTaskPriority; this.mTaskPriority = mTaskPriority;
return this; return this;
} }
//设置任务执行时间 //设置任务执行时间
public ITask setDuration(int duration) { public ITask setDuration(int duration) {
this.duration = duration; this.duration = duration;
return this; return this;
} }
//获取任务优先级 //获取任务优先级
@Override @Override
public TaskPriority getPriority() { public TaskPriority getPriority() {
return mTaskPriority; return mTaskPriority;
} }
//获取任务执行时间 //获取任务执行时间
@Override @Override
public int getDuration() { public int getDuration() {
return duration; return duration;
} }
//设置任务次序 //设置任务次序
@Override @Override
public void setSequence(int mSequence) { public void setSequence(int mSequence) {
this.mSequence = mSequence; this.mSequence = mSequence;
} }
//获取任务次序 //获取任务次序
@Override @Override
public int getSequence() { public int getSequence() {
return mSequence; return mSequence;
} }
// 获取任务状态 // 获取任务状态
@Override @Override
public boolean getStatus() { public boolean getStatus() {
return mTaskStatus; return mTaskStatus;
} }
//阻塞任务执行 //阻塞任务执行
@Override @Override
public void blockTask() throws Exception { public void blockTask() throws Exception {
blockQueue.take(); //如果队列里面没数据,就会一直阻塞 blockQueue.take(); //如果队列里面没数据,就会一直阻塞
} }
//解除阻塞 //解除阻塞
@Override @Override
public void unLockBlock() { public void unLockBlock() {
@ -99,6 +121,7 @@ public class BaseTask implements ITask {
return me == it ? this.getSequence() - another.getSequence() : return me == it ? this.getSequence() - another.getSequence() :
it.ordinal() - me.ordinal(); it.ordinal() - me.ordinal();
} }
//输出一些信息 //输出一些信息
@Override @Override
public String toString() { public String toString() {

@ -3,7 +3,7 @@ package com.common.queue.Task;
public interface ITask extends Comparable<ITask> { public interface ITask extends Comparable<ITask> {
// 将该任务插入队列 // 将该任务插入队列
void enqueue(); void enqueue() throws Exception;
// 执行具体任务的方法 // 执行具体任务的方法
void doTask(); void doTask();

@ -7,10 +7,6 @@ public class TaskScheduler {
private BlockTaskQueue mTaskQueue = new BlockTaskQueue(); private BlockTaskQueue mTaskQueue = new BlockTaskQueue();
private TaskExecutor mExecutor; private TaskExecutor mExecutor;
// private static class ShowDurationHolder {
// private final static TaskScheduler INSTANCE = new TaskScheduler();
// }
public TaskScheduler() { public TaskScheduler() {
initExecutor(); initExecutor();
} }
@ -20,10 +16,6 @@ public class TaskScheduler {
mExecutor.start(); mExecutor.start();
} }
// public static TaskScheduler getInstance() {
// return ShowDurationHolder.INSTANCE;
// }
public void enqueue(ITask task) { public void enqueue(ITask task) {
//因为TaskScheduler这里写成单例如果isRunning改成false的话不判断一下就会一直都是false //因为TaskScheduler这里写成单例如果isRunning改成false的话不判断一下就会一直都是false
if (!mExecutor.isRunning()) { if (!mExecutor.isRunning()) {
@ -40,5 +32,9 @@ public class TaskScheduler {
public void clearExecutor() { public void clearExecutor() {
mExecutor.clearExecutor(); mExecutor.clearExecutor();
} }
public BlockTaskQueue getQueue() {
return mTaskQueue;
}
} }

@ -12,8 +12,9 @@ class EasySerialPort(
private val mReceiver: (ByteArray) -> Unit private val mReceiver: (ByteArray) -> Unit
) : Runnable { ) : Runnable {
private var mPort: SerialPort? = null private var mPort: SerialPort? = null
private var startReceiveMsg = true private var mStartReceiveMsg = true
private var autoRetryConnect = false private var mAutoRetryConnect = false
private val mTaskScheduler = TaskScheduler()
init { init {
openSerialPort(); openSerialPort();
@ -22,7 +23,7 @@ class EasySerialPort(
private fun openSerialPort() { private fun openSerialPort() {
mPort = SerialPortHelper.openSerialPort(portPath, baudRate) mPort = SerialPortHelper.openSerialPort(portPath, baudRate)
mPort?.let { mPort?.let {
startReceiveMsg = true mStartReceiveMsg = true
Thread(this).start() Thread(this).start()
} }
} }
@ -31,7 +32,7 @@ class EasySerialPort(
* 关闭串口 * 关闭串口
*/ */
open fun closePort() { open fun closePort() {
startReceiveMsg = false mStartReceiveMsg = false
SerialPortHelper.closePort(mPort) SerialPortHelper.closePort(mPort)
} }
@ -39,7 +40,7 @@ class EasySerialPort(
* 循环读消息 * 循环读消息
*/ */
override fun run() { override fun run() {
while (startReceiveMsg) { while (mStartReceiveMsg) {
try { try {
val ips = mPort?.inputStream val ips = mPort?.inputStream
val readByte = ips?.available()?.let { ByteArray(it) } val readByte = ips?.available()?.let { ByteArray(it) }
@ -55,7 +56,7 @@ class EasySerialPort(
"read msg error; path = " + portPath + ", error msg = " + e.message "read msg error; path = " + portPath + ", error msg = " + e.message
) )
e.printStackTrace() e.printStackTrace()
if (autoRetryConnect) { if (mAutoRetryConnect) {
closePort() closePort()
openSerialPort() openSerialPort()
} }
@ -78,26 +79,27 @@ class EasySerialPort(
"write msg error; path = " + portPath + ", error msg = " + e.message "write msg error; path = " + portPath + ", error msg = " + e.message
) )
e.printStackTrace() e.printStackTrace()
if (autoRetryConnect) { if (mAutoRetryConnect) {
closePort() closePort()
openSerialPort() openSerialPort()
} }
} }
} }
TaskScheduler.getInstance().enqueue(task) task.attachScheduler(mTaskScheduler)
task.enqueue()
} }
/** /**
* 开始自动重连 * 开始自动重连
*/ */
open fun enableAutoConnect() { open fun enableAutoConnect() {
this.autoRetryConnect = true this.mAutoRetryConnect = true
} }
/** /**
* 关闭自动重连 * 关闭自动重连
*/ */
open fun disableAutoConnect() { open fun disableAutoConnect() {
this.autoRetryConnect = true this.mAutoRetryConnect = true
} }
} }

@ -9,7 +9,7 @@ import com.common.serialport.EasySerialPort
*/ */
object CommonPortUtils { object CommonPortUtils {
private val portMaps = HashMap<String, EasySerialPort?>() private val mPortMaps = HashMap<String, EasySerialPort?>()
/** /**
* 打开串口 * 打开串口
@ -22,11 +22,11 @@ object CommonPortUtils {
baudRate: Int, baudRate: Int,
mReceiver: (ByteArray) -> Unit mReceiver: (ByteArray) -> Unit
) { ) {
if (portMaps.containsKey(portPath)) { if (mPortMaps.containsKey(portPath)) {
return return
} }
val easyPort = EasySerialPort(portPath, baudRate, mReceiver) val easyPort = EasySerialPort(portPath, baudRate, mReceiver)
portMaps.put(portPath, easyPort) mPortMaps.put(portPath, easyPort)
} }
/** /**
@ -35,7 +35,7 @@ object CommonPortUtils {
* @param data 消息byte数组 * @param data 消息byte数组
*/ */
fun sendMsg(portPath: String, data: ByteArray) { fun sendMsg(portPath: String, data: ByteArray) {
SinglePortUtils.easyPort?.write(data) SinglePortUtils.mEasyPort?.write(data)
} }
/** /**
@ -44,7 +44,7 @@ object CommonPortUtils {
* @param data String类型消息内部会toByteArray * @param data String类型消息内部会toByteArray
*/ */
fun sendMsg(portPath: String, data: String) { fun sendMsg(portPath: String, data: String) {
SinglePortUtils.easyPort?.write(data.toByteArray()) SinglePortUtils.mEasyPort?.write(data.toByteArray())
} }
/** /**
@ -52,19 +52,19 @@ object CommonPortUtils {
* @param portPath 串口地址 * @param portPath 串口地址
*/ */
fun release(portPath: String) { fun release(portPath: String) {
if (portMaps.containsKey(portPath)) { if (mPortMaps.containsKey(portPath)) {
portMaps.get(portPath)?.closePort() mPortMaps.get(portPath)?.closePort()
} }
portMaps.remove(portPath) mPortMaps.remove(portPath)
} }
/** /**
* 释放所有串口 * 释放所有串口
*/ */
fun releaseAll() { fun releaseAll() {
portMaps.forEach { mPortMaps.forEach {
it.value?.closePort() it.value?.closePort()
} }
portMaps.clear() mPortMaps.clear()
} }
} }

@ -8,7 +8,7 @@ import com.common.serialport.EasySerialPort
* 操作单个串口工具类 * 操作单个串口工具类
*/ */
object SinglePortUtils { object SinglePortUtils {
var easyPort: EasySerialPort? = null var mEasyPort: EasySerialPort? = null
/** /**
* 初始化参数 * 初始化参数
@ -21,7 +21,7 @@ object SinglePortUtils {
baudRate: Int, baudRate: Int,
mReceiver: (ByteArray) -> Unit mReceiver: (ByteArray) -> Unit
) { ) {
easyPort = EasySerialPort(portPath, baudRate, mReceiver) mEasyPort = EasySerialPort(portPath, baudRate, mReceiver)
} }
/** /**
@ -30,7 +30,7 @@ object SinglePortUtils {
* @param data 消息byte数组 * @param data 消息byte数组
*/ */
fun sendMsg(data: ByteArray) { fun sendMsg(data: ByteArray) {
easyPort?.write(data) mEasyPort?.write(data)
} }
/** /**
@ -39,13 +39,13 @@ object SinglePortUtils {
* @param data String类型消息内部会toByteArray * @param data String类型消息内部会toByteArray
*/ */
fun sendMsg(data: String) { fun sendMsg(data: String) {
easyPort?.write(data.toByteArray()) mEasyPort?.write(data.toByteArray())
} }
/** /**
* 释放串口 * 释放串口
*/ */
fun release(){ fun release(){
easyPort?.closePort() mEasyPort?.closePort()
} }
} }
Loading…
Cancel
Save