JSerialPort
JSerialPort 是一个 Kotlin Multiplatform 串口通信库,支持 Android、Windows 和 Linux 平台。
示例项目
JSerialPort Example
克隆仓库
Github
git clone https://github.com/jeadyx/kmp-serialport.git
Gitee(国内访问)
git clone https://gitee.com/jeadyx/kmp-serialport.git
特性
- 支持 Android、Windows 和 Linux 平台
- 使用 Kotlin Coroutines 和 Flow 进行异步操作
- 简单易用的 API
- 支持自定义波特率、数据位、停止位和校验位
安装
在项目的 build.gradle.kts 文件中添加以下依赖:
dependencies {
implementation("io.github.jeadyx:kmp-serialport:1.0.0")
}
使用方法
基本用法
val serialPort = SerialPortFactory.create()
serialPort.open(
portName = "/dev/ttyUSB0",
baudRate = 9600,
dataBits = 8,
stopBits = ,
parity = SerialPort.PARITY_NONE
)
serialPort.write(.toByteArray())
serialPort.read().collect { ->
println()
}
serialPort.close()
错误处理
try {
serialPort.open("/dev/ttyUSB0", 9600)
} catch (e: SerialPortException) {
println("Failed to open serial port: ${e.message}")
}
许可证
MIT License
Donate
