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

}
相关推荐
新之助小锅1 小时前
java版连接汇川PLC,发送数据,读取数据,保持重新链接,适用安卓
android·java·python
2501_916007472 小时前
iOS性能调试工具终极指南,从系统底层到多端协同的全方位优化实践(2025版)
android·ios·小程序·https·uni-app·iphone·webview
2501_915921432 小时前
iOS崩溃日志深度分析与工具组合实战,从符号化到自动化诊断的完整体系
android·ios·小程序·uni-app·自动化·cocoa·iphone
执念WRD5 小时前
熊海CMS v1.0代码审计实战
android·nginx·安全·web安全·网络安全·系统安全
jllllyuz5 小时前
基于ThinkPHP实现动态ZIP压缩包的生成
android
百***92028 小时前
【MySQL】MySQL库的操作
android·数据库·mysql
2501_916008899 小时前
没有源码如何加密 IPA 实战流程与多工具组合落地指南
android·ios·小程序·https·uni-app·iphone·webview
2501_9400940210 小时前
PS1模拟器 DuckStation更新最新版整合 下载即玩 附PS1Bios/游戏/金手指 安卓版+电脑版
android·游戏·电脑
橙武低代码12 小时前
业务流低代码平台:从理念到实战
android·低代码·ai编程
空白格9712 小时前
三方框架必学系列#Retrofit
android