串口数据处理
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
package com.shuwei.intelligent.shelves.serial
|
||||
|
||||
import com.shuwei.intelligent.shelves.utils.hexToBytes
|
||||
import com.shuwei.intelligent.shelves.utils.toHexString2
|
||||
import io.github.jeadyx.jserialport.AndroidSerialPort
|
||||
import io.github.jeadyx.jserialport.SerialPort
|
||||
import io.github.jeadyx.jserialport.SerialPortFactory
|
||||
import kotlinx.coroutines.*
|
||||
import java.nio.charset.Charset
|
||||
import java.util.Locale
|
||||
|
||||
object SerialPortManager {
|
||||
private const val portName: String = "/dev/ttyS1"
|
||||
@@ -34,7 +38,8 @@ object SerialPortManager {
|
||||
// 发送数据
|
||||
suspend fun send(data: String): Boolean {
|
||||
return try {
|
||||
serialPort?.write(data.toByteArray())
|
||||
// serialPort?.write(data.toByteArray())
|
||||
serialPort?.write(data.hexToBytes())
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
@@ -47,7 +52,9 @@ object SerialPortManager {
|
||||
scope.launch {
|
||||
while (isActive) {
|
||||
serialPort?.read()?.collect { buffer ->
|
||||
callback(String(buffer))
|
||||
// val data = String(buffer)
|
||||
val data = buffer.toHexString2().toUpperCase()
|
||||
callback(data)
|
||||
}
|
||||
delay(50)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user