|
|
|
@ -10,7 +10,7 @@ class EasySerialPort<T>(
|
|
|
|
|
private val baudRate: Int,
|
|
|
|
|
private val helper: ISerialPortHelper<T>,
|
|
|
|
|
private val mReceiver: (ByteArray) -> Unit
|
|
|
|
|
) {
|
|
|
|
|
) : Runnable {
|
|
|
|
|
private var mStartReceiveMsg = true
|
|
|
|
|
private var mAutoRetryConnect = false
|
|
|
|
|
|
|
|
|
@ -24,6 +24,10 @@ class EasySerialPort<T>(
|
|
|
|
|
|
|
|
|
|
private fun openSerialPort() {
|
|
|
|
|
val mPort = helper.openSerialPort(portPath, baudRate)
|
|
|
|
|
mPort?.let {
|
|
|
|
|
mStartReceiveMsg = true
|
|
|
|
|
Thread(this).start()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -34,6 +38,35 @@ class EasySerialPort<T>(
|
|
|
|
|
helper.closePort()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 循环读消息
|
|
|
|
|
*/
|
|
|
|
|
override fun run() {
|
|
|
|
|
while (mStartReceiveMsg) {
|
|
|
|
|
try {
|
|
|
|
|
val ips = helper?.inputStream()
|
|
|
|
|
val size = ips?.read(mReadBytes)
|
|
|
|
|
if (size != null) {
|
|
|
|
|
if (size > 0) {
|
|
|
|
|
Log.i("EasySerialPort", "read msg ${HexUtils.byteArrToHex(mReadBytes)}")
|
|
|
|
|
mReceiver.invoke(mReadBytes)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Thread.sleep(100L)
|
|
|
|
|
} catch (e: IOException) {
|
|
|
|
|
Log.e(
|
|
|
|
|
"EasySerialPort",
|
|
|
|
|
"read msg error; path = " + portPath + ", error msg = " + e.message
|
|
|
|
|
)
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
if (mAutoRetryConnect) {
|
|
|
|
|
closePort()
|
|
|
|
|
openSerialPort()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 写消息
|
|
|
|
|
*/
|
|
|
|
@ -44,25 +77,25 @@ class EasySerialPort<T>(
|
|
|
|
|
outputStream?.write(data)
|
|
|
|
|
outputStream?.flush()
|
|
|
|
|
Log.i("EasySerialPort", "send msg ${HexUtils.byteArrToHex(data)}")
|
|
|
|
|
var offset = 1
|
|
|
|
|
if (data[0] ==0xFF.toByte()){
|
|
|
|
|
offset = 2
|
|
|
|
|
}
|
|
|
|
|
val cmd = data[offset]
|
|
|
|
|
// 写完之后读取数据
|
|
|
|
|
val ips = helper?.inputStream()
|
|
|
|
|
val size = ips?.read(mReadBytes)
|
|
|
|
|
if (size != null) {
|
|
|
|
|
if (size > 0) {
|
|
|
|
|
Log.i(
|
|
|
|
|
"EasySerialPort",
|
|
|
|
|
"cmd $cmd ;read msg ${HexUtils.byteArrToHex(mReadBytes)}"
|
|
|
|
|
)
|
|
|
|
|
if (mReadBytes[0].toInt() == 0x01 && mReadBytes[1].toInt() == 0x03 && mReadBytes[2].toInt() == 0x14) {
|
|
|
|
|
mReceiver.invoke(mReadBytes)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// var offset = 1
|
|
|
|
|
// if (data[0] == 0xFF.toByte()) {
|
|
|
|
|
// offset = 2
|
|
|
|
|
// }
|
|
|
|
|
// val cmd = data[offset]
|
|
|
|
|
// // 写完之后读取数据
|
|
|
|
|
// val ips = helper?.inputStream()
|
|
|
|
|
// val size = ips?.read(mReadBytes)
|
|
|
|
|
// if (size != null) {
|
|
|
|
|
// if (size > 0) {
|
|
|
|
|
// Log.i(
|
|
|
|
|
// "EasySerialPort",
|
|
|
|
|
// "cmd $cmd ;read msg ${HexUtils.byteArrToHex(mReadBytes)}"
|
|
|
|
|
// )
|
|
|
|
|
// if (mReadBytes[0].toInt() == 0x01 && mReadBytes[1].toInt() == 0x03 && mReadBytes[2].toInt() == 0x14) {
|
|
|
|
|
// mReceiver.invoke(mReadBytes)
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
} catch (e: IOException) {
|
|
|
|
|
Log.e(
|
|
|
|
|
"EasySerialPort",
|
|
|
|
|