android compose 串口通信

1.添加依赖

Kotlin 复制代码
implementation("io.github.xmaihh:serialport:2.1.1")

2.添加SerialHelper派生类

Kotlin 复制代码
class SerialPortHelper(portName:String,baudRate:Int): SerialHelper(portName,baudRate) {
    var receivedDataBuffer = mutableListOf<Byte>()
    override fun onDataReceived(paraComBean: ComBean?) {
        if(paraComBean!=null)
        {
            var temp:ByteArray = paraComBean.bRec;
            receivedDataBuffer.addAll(temp.toList())

            //receivedDataBuffer.addAll(paraComBean.toList())
        }
    }

    init {
        setStickPackageHelper (object : AbsStickPackageHelper {
            override fun execute(isStream: InputStream?): ByteArray? {
                try {
                    val available = isStream?.available()
                    if (available != null) {
                        if (available > 0) {
                            val buffer = ByteArray(available)
                            val size = isStream.read(buffer)
                            if (size > 0) {
                                return buffer
                            }
                        } else {
                            // 若没有数据,等待50毫秒
                            Thread.sleep(50)
                        }
                    }
                } catch (e: IOException) {
                    e.printStackTrace()
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }
                return null
            }

        })
    }

}

3.创建串口处理类

Kotlin 复制代码
class SericalCommunicate {

    var serialPort:SerialPortHelper? = null
    //var outputSeriaPort = mutableStateOf("/dev/ttyS0")
    var serialportName:MutableState<String> = mutableStateOf("/dev/ttyS0")
    var serialBaud:MutableState<Int> = mutableStateOf(115200)

    init {
        initSerialport()
    }

    @OptIn(ExperimentalStdlibApi::class)
    fun initSerialport()
    {
        try {
            serialPort = SerialPortHelper(serialportName.value,serialBaud.value)//SerialPort(File(serialportName.value),serialBaud.value,'N',8,)
        }catch (e:Exception)
        {
            e.printStackTrace()
        }


        GlobalScope.launch {
            val buffer = ByteArray(1024)
            while(serialPort!=null)
            {
                val reader = serialPort!!.receivedDataBuffer

                if(reader!=null) {
                    var length = reader!!.size
                    if (length>0)
                    {
                        val data = String(buffer, 0, length)
                        var dataStr = data.toByteArray().toHexString()
                        Log.d("scoreInfo", dataStr)
                    }
                    serialPort!!.receivedDataBuffer.clear()
                }
                delay(50)
            }
        }

    }

    fun SetSerialPara(){
        reConnectedSerial()
    }

    fun reConnectedSerial()
    {
        if(serialPort!=null) {
            serialPort?.close()
            serialPort = null
        }

        initSerialport()
    }

    fun sendDataFromSerialport(data:ByteArray)
    {
        if(serialPort!=null)
        {
            serialPort!!.send(data)
        }
    }

}
相关推荐
2501_915909063 小时前
Charles中文版使用教程 高效抓包与API调试常见问题处理
android·ios·小程序·https·uni-app·iphone·webview
智江鹏3 小时前
Android 之 RxJava2
android
御水流红叶4 小时前
安卓加固脱壳
android·开发语言·python
智江鹏5 小时前
Android 之 网络通信(HTTP/TCP/UDP/JSON)
android
xq95275 小时前
android webview和 js 各种用法交互
android
北有花开7 小时前
Android方法耗时监控插件:基于ASM字节码插桩的性能分析工具
android
whysqwhw7 小时前
React Native应用中实现原生组件与JavaScript组件的复杂交互
android
whysqwhw7 小时前
React Native 中调用 Android 自定义控件
android
往事如烟隔多年7 小时前
一加Ace5无法连接ColorOS助手解决(安卓设备ADB模式无法连接)
android·adb·手机·coloros
00后程序员张7 小时前
iOS软件性能监控实战指南 开发到上线的完整流程解析
android·ios·小程序·https·uni-app·iphone·webview