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)
        }
    }

}
相关推荐
百锦再8 小时前
React编程高级主题:测试代码
android·前端·javascript·react.js·前端框架·reactjs
2501_9160088910 小时前
全面介绍Fiddler、Wireshark、HttpWatch、SmartSniff和firebug抓包工具功能与使用
android·ios·小程序·https·uni-app·iphone·webview
玉梅小洋10 小时前
Windows 10 Android 构建配置指南
android·windows
Libraeking12 小时前
视觉篇:Canvas 自定义绘图与高级动画的华丽圆舞曲
android·经验分享·android jetpack
Fushize12 小时前
多模块架构下的依赖治理:如何避免 Gradle 依赖地狱
android·架构·kotlin
Jomurphys13 小时前
Kotlin - 类型别名 typealias
android·kotlin
Haha_bj13 小时前
Flutter ——flutter_screenutil 屏幕适配
android·ios
Haha_bj14 小时前
Flutter ——device_info_plus详解
android·flutter·ios
前端小伙计14 小时前
Android/Flutter 项目统一构建配置最佳实践
android·flutter
Mr_sun.15 小时前
Day09——入退管理-入住-2
android·java·开发语言