desc:调整串口框架代码
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,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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue