串口数据处理
This commit is contained in:
@@ -5,7 +5,7 @@ plugins {
|
|||||||
|
|
||||||
android {
|
android {
|
||||||
namespace = "com.shuwei.intelligent.shelves"
|
namespace = "com.shuwei.intelligent.shelves"
|
||||||
compileSdk = 36
|
compileSdk = 34
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId = "com.shuwei.intelligent.shelves"
|
applicationId = "com.shuwei.intelligent.shelves"
|
||||||
|
|||||||
@@ -68,10 +68,16 @@ class HomeActivity : BaseActivity() {
|
|||||||
SerialPortManager.startReceive { data ->
|
SerialPortManager.startReceive { data ->
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
// 更新UI显示接收数据
|
// 更新UI显示接收数据
|
||||||
|
runCatching {
|
||||||
receiveSerialPortData(data)
|
receiveSerialPortData(data)
|
||||||
|
}.onFailure {
|
||||||
|
it.printStackTrace()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SerialPortManager.send(ACTIVE_CMD)
|
SerialPortManager.send(ACTIVE_CMD)
|
||||||
|
|
||||||
|
SerialPortManager.send(DEVICE_INFO_CMD)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TaskManager.startTask()
|
TaskManager.startTask()
|
||||||
@@ -117,6 +123,7 @@ class HomeActivity : BaseActivity() {
|
|||||||
|
|
||||||
val intent = Intent(ShelfActivity.RECEIVER_DEVICE_INFO)
|
val intent = Intent(ShelfActivity.RECEIVER_DEVICE_INFO)
|
||||||
intent.putExtra(ShelfActivity.SHELF_WEIGHT, weightArray[clickIndex])
|
intent.putExtra(ShelfActivity.SHELF_WEIGHT, weightArray[clickIndex])
|
||||||
|
intent.putExtra(ShelfActivity.RECEIVER_DEVICE_INFO, data)
|
||||||
sendBroadcast(intent)
|
sendBroadcast(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import android.os.Build
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
|
import android.util.Log
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
@@ -37,6 +38,7 @@ import kotlin.random.Random
|
|||||||
class ShelfActivity : BaseActivity() {
|
class ShelfActivity : BaseActivity() {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
private const val TAG = "ShelfActivity"
|
||||||
const val SHELF_MODEL = "shelfModel"
|
const val SHELF_MODEL = "shelfModel"
|
||||||
const val RECEIVER_DEVICE_INFO = "receiverDeviceInfo"
|
const val RECEIVER_DEVICE_INFO = "receiverDeviceInfo"
|
||||||
const val SHELF_WEIGHT = "shelfWeight"
|
const val SHELF_WEIGHT = "shelfWeight"
|
||||||
@@ -119,6 +121,8 @@ class ShelfActivity : BaseActivity() {
|
|||||||
private val receiver = object : BroadcastReceiver() {
|
private val receiver = object : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context?, intent: Intent?) {
|
override fun onReceive(context: Context?, intent: Intent?) {
|
||||||
intent?.let {
|
intent?.let {
|
||||||
|
val data = it.getStringExtra(RECEIVER_DEVICE_INFO)
|
||||||
|
Log.d(TAG, "onReceive: $data")
|
||||||
realWeight = it.getIntExtra(SHELF_WEIGHT, 0)
|
realWeight = it.getIntExtra(SHELF_WEIGHT, 0)
|
||||||
binding.tvFoodWeight.text = "${realWeight}克"
|
binding.tvFoodWeight.text = "${realWeight}克"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package com.shuwei.intelligent.shelves.serial
|
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.AndroidSerialPort
|
||||||
import io.github.jeadyx.jserialport.SerialPort
|
import io.github.jeadyx.jserialport.SerialPort
|
||||||
import io.github.jeadyx.jserialport.SerialPortFactory
|
import io.github.jeadyx.jserialport.SerialPortFactory
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
import java.nio.charset.Charset
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
object SerialPortManager {
|
object SerialPortManager {
|
||||||
private const val portName: String = "/dev/ttyS1"
|
private const val portName: String = "/dev/ttyS1"
|
||||||
@@ -34,7 +38,8 @@ object SerialPortManager {
|
|||||||
// 发送数据
|
// 发送数据
|
||||||
suspend fun send(data: String): Boolean {
|
suspend fun send(data: String): Boolean {
|
||||||
return try {
|
return try {
|
||||||
serialPort?.write(data.toByteArray())
|
// serialPort?.write(data.toByteArray())
|
||||||
|
serialPort?.write(data.hexToBytes())
|
||||||
true
|
true
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
@@ -47,7 +52,9 @@ object SerialPortManager {
|
|||||||
scope.launch {
|
scope.launch {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
serialPort?.read()?.collect { buffer ->
|
serialPort?.read()?.collect { buffer ->
|
||||||
callback(String(buffer))
|
// val data = String(buffer)
|
||||||
|
val data = buffer.toHexString2().toUpperCase()
|
||||||
|
callback(data)
|
||||||
}
|
}
|
||||||
delay(50)
|
delay(50)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,3 +28,20 @@ fun hexToDec(hex: String): Int {
|
|||||||
|
|
||||||
return result
|
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 android.widget.Toast
|
||||||
import com.shuwei.intelligent.shelves.App
|
import com.shuwei.intelligent.shelves.App
|
||||||
|
//D6DADEDB0302000001000C06
|
||||||
object UIUtils {
|
object UIUtils {
|
||||||
fun toast(strRes: Int) {
|
fun toast(strRes: Int) {
|
||||||
Toast.makeText(App.get(), strRes, Toast.LENGTH_SHORT).show()
|
Toast.makeText(App.get(), strRes, Toast.LENGTH_SHORT).show()
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
[versions]
|
[versions]
|
||||||
agp = "8.11.1"
|
#agp = "8.11.1"
|
||||||
|
agp = "8.4.0"
|
||||||
kotlin = "2.0.21"
|
kotlin = "2.0.21"
|
||||||
coreKtx = "1.10.1"
|
coreKtx = "1.10.1"
|
||||||
junit = "4.13.2"
|
junit = "4.13.2"
|
||||||
|
|||||||
+1
@@ -2,5 +2,6 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
|
||||||
|
#distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-8.13-bin.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
|||||||
Reference in New Issue
Block a user