优化并增加日志

This commit is contained in:
马增飞
2025-11-18 18:49:46 +08:00
parent 79d14ca300
commit 096499cd65
17 changed files with 482 additions and 155 deletions
@@ -21,36 +21,32 @@ object SerialPortManager {
private val scope = CoroutineScope(Dispatchers.IO)
// 打开串口
fun open(): Boolean {
lifecycleScope.launch {
return try {
serialPort = SerialPortFactory.create().apply {
open(
portName = portName,
baudRate = baudRate,
dataBits = 8,
stopBits = 1,
parity = SerialPort.PARITY_NONE
)
} as AndroidSerialPort?
true
} catch (e: Exception) {
e.printStackTrace()
false
}
suspend fun open(): Boolean {
return try {
serialPort = SerialPortFactory.create().apply {
open(
portName = portName,
baudRate = baudRate,
dataBits = 8,
stopBits = 1,
parity = SerialPort.PARITY_NONE
)
} as AndroidSerialPort?
true
} catch (e: Exception) {
e.printStackTrace()
false
}
}
// 发送数据
fun send(data: String): Boolean {
lifecycleScope.launch {
return try {
serialPort?.write(data.hexToBytes())
true
} catch (e: Exception) {
e.printStackTrace()
false
}
suspend fun send(data: String): Boolean {
return try {
serialPort?.write(data.hexToBytes())
true
} catch (e: Exception) {
e.printStackTrace()
false
}
}