diff --git a/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasPortUtils.kt b/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasPortUtils.kt index 2d68092..2275cb3 100644 --- a/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasPortUtils.kt +++ b/app/src/main/java/com/yinuo/safetywatcher/watcher/port/GasPortUtils.kt @@ -38,7 +38,9 @@ object GasPortUtils { listeners.forEach { it.invoke(array) } - ParseHelper.parse(array) + if (array[0].toInt() == 0x01 && array[1].toInt() == 0x03 && array[2].toInt() == 0x14) { + ParseHelper.parse(array) + } } } } diff --git a/library-serialPort/src/main/java/com/common/serialport/EasySerialPort.kt b/library-serialPort/src/main/java/com/common/serialport/EasySerialPort.kt index e7929ce..3dc71b7 100644 --- a/library-serialPort/src/main/java/com/common/serialport/EasySerialPort.kt +++ b/library-serialPort/src/main/java/com/common/serialport/EasySerialPort.kt @@ -10,7 +10,7 @@ class EasySerialPort( private val baudRate: Int, private val helper: ISerialPortHelper, private val mReceiver: (ByteArray) -> Unit -) { +) : Runnable { private var mStartReceiveMsg = true private var mAutoRetryConnect = false @@ -24,6 +24,10 @@ class EasySerialPort( private fun openSerialPort() { val mPort = helper.openSerialPort(portPath, baudRate) + mPort?.let { + mStartReceiveMsg = true + Thread(this).start() + } } /** @@ -34,6 +38,35 @@ class EasySerialPort( 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( 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",