desc:调整串口框架代码

main
xiaowusky 2 years ago
parent eda19438ac
commit 50f9742817

@ -0,0 +1,12 @@
package com.yinuo.safetywatcher.watcher.port
import com.common.serialport.inter.ISerialPortHelper
import com.common.serialport.utils.MultiPortUtils
import com.lztek.toolkit.SerialPort
object PlatformMultiPortUtils : MultiPortUtils<SerialPort>() {
override fun generateHelper(): ISerialPortHelper<SerialPort> {
// 每次生成新的helper
return PlatformSerialPortHelper()
}
}

@ -0,0 +1,45 @@
package com.yinuo.safetywatcher.watcher.port
import android.util.Log
import com.common.serialport.inter.ISerialPortHelper
import com.lztek.toolkit.SerialPort
import com.yinuo.safetywatcher.watcher.utils.LztekUtil
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
/**
* 平台帮助类
* sdkapi.jar
*/
class PlatformSerialPortHelper : ISerialPortHelper<SerialPort> {
private var mSerialPort: SerialPort? = null
override fun openSerialPort(portPath: String, baudrate: Int): SerialPort? {
try {
mSerialPort = LztekUtil.getLztek()?.openSerialPort(portPath, baudrate)
return mSerialPort
} catch (e: IOException) {
e.printStackTrace()
Log.e("SerialPortHelper", "open port error; " + e.message)
}
return null;
}
override fun inputStream(): InputStream? {
return mSerialPort?.inputStream
}
override fun outputStream(): OutputStream? {
return mSerialPort?.outputStream
}
override fun closePort() {
try {
mSerialPort?.close()
} catch (e: Exception) {
e.printStackTrace()
Log.e("SerialPortHelper", "closePort error; " + e.message)
}
}
}

@ -0,0 +1,15 @@
package com.yinuo.safetywatcher.watcher.port
import com.common.serialport.inter.ISerialPortHelper
import com.common.serialport.utils.SinglePortUtils
import com.lztek.toolkit.SerialPort
object PlatformSinglePortUtils : SinglePortUtils<SerialPort>() {
private val helper = PlatformSerialPortHelper()
override fun generateHelper(): ISerialPortHelper<SerialPort> {
// 公用一个helper
return helper
}
}

@ -0,0 +1,13 @@
package com.common.serialport
import com.aill.androidserialport.SerialPort
import com.common.serialport.inter.ISerialPortHelper
import com.common.serialport.inter.NormalSerialPortHelper
import com.common.serialport.utils.MultiPortUtils
object ComMultiPortUtils : MultiPortUtils<SerialPort>() {
override fun generateHelper(): ISerialPortHelper<SerialPort> {
return NormalSerialPortHelper()
}
}

@ -0,0 +1,15 @@
package com.common.serialport
import com.aill.androidserialport.SerialPort
import com.common.serialport.inter.ISerialPortHelper
import com.common.serialport.inter.NormalSerialPortHelper
import com.common.serialport.utils.SinglePortUtils
object ComSinglePortUtils : SinglePortUtils<SerialPort>() {
private val helper = NormalSerialPortHelper()
override fun generateHelper(): ISerialPortHelper<SerialPort> {
return helper
}
}

@ -1,17 +1,16 @@
package com.common.serialport
import android.util.Log
import com.aill.androidserialport.SerialPort
import com.common.serialport.inter.SerialPortHelper
import com.common.queue.TaskScheduler
import com.common.serialport.inter.ISerialPortHelper
import java.io.IOException
class EasySerialPort(
class EasySerialPort<T>(
private val portPath: String,
private val baudRate: Int,
private val helper: ISerialPortHelper<T>,
private val mReceiver: (ByteArray) -> Unit
) : Runnable {
private var mPort: SerialPort? = null
private var mStartReceiveMsg = true
private var mAutoRetryConnect = false
private val mTaskScheduler = TaskScheduler()
@ -21,7 +20,7 @@ class EasySerialPort(
}
private fun openSerialPort() {
mPort = SerialPortHelper.openSerialPort(portPath, baudRate)
val mPort = helper.openSerialPort(portPath, baudRate)
mPort?.let {
mStartReceiveMsg = true
Thread(this).start()
@ -33,7 +32,7 @@ class EasySerialPort(
*/
open fun closePort() {
mStartReceiveMsg = false
SerialPortHelper.closePort(mPort)
helper.closePort()
}
/**
@ -42,7 +41,7 @@ class EasySerialPort(
override fun run() {
while (mStartReceiveMsg) {
try {
val ips = mPort?.inputStream
val ips = helper?.inputStream()
val readByte = ips?.available()?.let { ByteArray(it) }
readByte?.let {
val size = ips.read(it)
@ -70,7 +69,7 @@ class EasySerialPort(
open fun write(data: ByteArray) {
val task = SendMsgTask(data) {
try {
val outputStream = mPort?.outputStream
val outputStream = helper?.outputStream()
outputStream?.write(it)
outputStream?.flush()
} catch (e: IOException) {

@ -1,10 +0,0 @@
package com.common.serialport.inter
import com.aill.androidserialport.SerialPort
interface IHelper {
fun openSerialPort(portPath: String, baudrate: Int): SerialPort?;
fun closePort(port: SerialPort?);
}

@ -0,0 +1,15 @@
package com.common.serialport.inter
import java.io.InputStream
import java.io.OutputStream
interface ISerialPortHelper<T> {
fun openSerialPort(portPath: String, baudrate: Int): T?
fun closePort()
fun inputStream(): InputStream?
fun outputStream(): OutputStream?
}

@ -0,0 +1,47 @@
package com.common.serialport.inter
import android.util.Log
import com.aill.androidserialport.SerialPort
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
/**
* 通用帮助类
* implementation 'com.aill:AndroidSerialPort:1.0.8'
*/
class NormalSerialPortHelper : ISerialPortHelper<SerialPort> {
private var mSerialPort: SerialPort? = null
override fun openSerialPort(portPath: String, baudrate: Int): SerialPort? {
try {
mSerialPort = SerialPort(File(portPath), baudrate, 0)
return mSerialPort
} catch (e: IOException) {
e.printStackTrace()
Log.e("SerialPortHelper", "open port error; " + e.message)
}
return null;
}
override fun inputStream(): InputStream? {
return mSerialPort?.inputStream
}
override fun outputStream(): OutputStream? {
return mSerialPort?.outputStream
}
override fun closePort() {
try {
mSerialPort?.close()
mSerialPort?.inputStream?.close()
mSerialPort?.outputStream?.close()
} catch (e: Exception) {
e.printStackTrace()
Log.e("SerialPortHelper", "closePort error; " + e.message)
}
}
}

@ -1,29 +0,0 @@
package com.common.serialport.inter
import android.util.Log
import com.aill.androidserialport.SerialPort
import java.io.File
import java.io.IOException
object SerialPortHelper : IHelper {
override fun openSerialPort(portPath: String, baudrate: Int): SerialPort? {
try {
return SerialPort(File(portPath), baudrate, 0);
} catch (e: IOException) {
e.printStackTrace()
Log.e("SerialPortHelper", "open port error; " + e.message)
}
return null;
}
override fun closePort(port: SerialPort?) {
try {
port?.close()
port?.inputStream?.close()
port?.outputStream?.close()
} catch (e: Exception) {
e.printStackTrace()
Log.e("SerialPortHelper", "closePort error; " + e.message)
}
}
}

@ -1,15 +1,22 @@
package com.common.serialport.utils
import com.common.serialport.EasySerialPort
import com.common.serialport.inter.ISerialPortHelper
/**
* @author xiaowusky
* 操作串口工具类
* 可以同时处理多个串口操作
*/
object CommonPortUtils {
abstract class MultiPortUtils<T> {
private val mPortMaps = HashMap<String, EasySerialPort?>()
private val mPortMaps = HashMap<String, EasySerialPort<T>?>()
/**
* 生成ISerialPortHelper对象
* 又子类重写决定具体的实现对象
*/
abstract fun generateHelper(): ISerialPortHelper<T>
/**
* 打开串口
@ -25,8 +32,8 @@ object CommonPortUtils {
if (mPortMaps.containsKey(portPath)) {
return
}
val easyPort = EasySerialPort(portPath, baudRate, mReceiver)
mPortMaps.put(portPath, easyPort)
val easyPort = EasySerialPort(portPath, baudRate, generateHelper(), mReceiver)
mPortMaps[portPath] = easyPort
}
/**
@ -35,7 +42,9 @@ object CommonPortUtils {
* @param data 消息byte数组
*/
fun sendMsg(portPath: String, data: ByteArray) {
SinglePortUtils.mEasyPort?.write(data)
if (mPortMaps.containsKey(portPath)) {
mPortMaps[portPath]?.write(data)
}
}
/**
@ -44,7 +53,9 @@ object CommonPortUtils {
* @param data String类型消息内部会toByteArray
*/
fun sendMsg(portPath: String, data: String) {
SinglePortUtils.mEasyPort?.write(data.toByteArray())
if (mPortMaps.containsKey(portPath)) {
mPortMaps[portPath]?.write(data.toByteArray())
}
}
/**
@ -53,7 +64,7 @@ object CommonPortUtils {
*/
fun release(portPath: String) {
if (mPortMaps.containsKey(portPath)) {
mPortMaps.get(portPath)?.closePort()
mPortMaps[portPath]?.closePort()
}
mPortMaps.remove(portPath)
}

@ -2,13 +2,16 @@
package com.common.serialport.utils
import com.common.serialport.EasySerialPort
import com.common.serialport.inter.ISerialPortHelper
/**
* @author xiaowusky
* 操作单个串口工具类
*/
object SinglePortUtils {
var mEasyPort: EasySerialPort? = null
abstract class SinglePortUtils<T> {
private var mEasyPort: EasySerialPort<T>? = null
abstract fun generateHelper(): ISerialPortHelper<T>
/**
* 初始化参数
@ -21,7 +24,7 @@ object SinglePortUtils {
baudRate: Int,
mReceiver: (ByteArray) -> Unit
) {
mEasyPort = EasySerialPort(portPath, baudRate, mReceiver)
mEasyPort = EasySerialPort(portPath, baudRate, generateHelper(), mReceiver)
}
/**

Loading…
Cancel
Save