串口数据处理

This commit is contained in:
马增飞
2025-08-06 19:31:18 +08:00
parent f0f89982be
commit 005e7eae53
8 changed files with 43 additions and 6 deletions
@@ -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)
}