串口数据处理

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 -1
View File
@@ -5,7 +5,7 @@ plugins {
android {
namespace = "com.shuwei.intelligent.shelves"
compileSdk = 36
compileSdk = 34
defaultConfig {
applicationId = "com.shuwei.intelligent.shelves"
@@ -68,10 +68,16 @@ class HomeActivity : BaseActivity() {
SerialPortManager.startReceive { data ->
runOnUiThread {
// 更新UI显示接收数据
receiveSerialPortData(data)
runCatching {
receiveSerialPortData(data)
}.onFailure {
it.printStackTrace()
}
}
}
SerialPortManager.send(ACTIVE_CMD)
SerialPortManager.send(DEVICE_INFO_CMD)
}
}
TaskManager.startTask()
@@ -117,6 +123,7 @@ class HomeActivity : BaseActivity() {
val intent = Intent(ShelfActivity.RECEIVER_DEVICE_INFO)
intent.putExtra(ShelfActivity.SHELF_WEIGHT, weightArray[clickIndex])
intent.putExtra(ShelfActivity.RECEIVER_DEVICE_INFO, data)
sendBroadcast(intent)
}
@@ -9,6 +9,7 @@ import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
@@ -37,6 +38,7 @@ import kotlin.random.Random
class ShelfActivity : BaseActivity() {
companion object {
private const val TAG = "ShelfActivity"
const val SHELF_MODEL = "shelfModel"
const val RECEIVER_DEVICE_INFO = "receiverDeviceInfo"
const val SHELF_WEIGHT = "shelfWeight"
@@ -119,6 +121,8 @@ class ShelfActivity : BaseActivity() {
private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
intent?.let {
val data = it.getStringExtra(RECEIVER_DEVICE_INFO)
Log.d(TAG, "onReceive: $data")
realWeight = it.getIntExtra(SHELF_WEIGHT, 0)
binding.tvFoodWeight.text = "${realWeight}"
}
@@ -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)
}
@@ -28,3 +28,20 @@ fun hexToDec(hex: String): Int {
return result
}
fun hexStringToByteArray(hex: String): ByteArray {
require(hex.length % 2 == 0) { "Hex string must have even length" }
return hex.chunked(2)
.map { it.toInt(16).toByte() }
.toByteArray()
}
fun byteArrayToHexString(bytes: ByteArray): String {
return bytes.joinToString("") { "%02x".format(it) }
}
// 扩展函数版本
fun String.hexToBytes(): ByteArray = hexStringToByteArray(this)
fun ByteArray.toHexString2(): String = byteArrayToHexString(this)
@@ -2,7 +2,7 @@ package com.shuwei.intelligent.shelves.utils
import android.widget.Toast
import com.shuwei.intelligent.shelves.App
//D6DADEDB0302000001000C06
object UIUtils {
fun toast(strRes: Int) {
Toast.makeText(App.get(), strRes, Toast.LENGTH_SHORT).show()