
Cross-platform serial communication library simplifies asynchronous operations with coroutines and flow, featuring customizable baud rate, data bits, stop bits, and parity settings.
JSerialPort 是一个 Kotlin Multiplatform 串口通信库,支持 Android、Windows 和 Linux 平台。
git clone https://github.com/jeadyx/kmp-serialport.gitgit clone https://gitee.com/jeadyx/kmp-serialport.git在项目的 build.gradle.kts 文件中添加以下依赖:
dependencies {
implementation("io.github.jeadyx:kmp-serialport:1.0.0")
}// 创建串口实例
val serialPort = SerialPortFactory.create()
// 打开串口
serialPort.open(
portName = "/dev/ttyUSB0", // Linux
// portName = "COM1", // Windows
// portName = "/dev/ttyS0", // Android
baudRate = 9600,
dataBits = 8,
stopBits = 1,
parity = SerialPort.PARITY_NONE
)
// 写入数据
serialPort.write("Hello".toByteArray())
// 读取数据
serialPort.read().collect { data ->
println("Received: ${String(data)}")
}
// 关闭串口
serialPort.close()try {
serialPort.open("/dev/ttyUSB0", 9600)
} catch (e: SerialPortException) {
println("Failed to open serial port: ${e.message}")
}MIT License
JSerialPort 是一个 Kotlin Multiplatform 串口通信库,支持 Android、Windows 和 Linux 平台。
git clone https://github.com/jeadyx/kmp-serialport.gitgit clone https://gitee.com/jeadyx/kmp-serialport.git在项目的 build.gradle.kts 文件中添加以下依赖:
dependencies {
implementation("io.github.jeadyx:kmp-serialport:1.0.0")
}// 创建串口实例
val serialPort = SerialPortFactory.create()
// 打开串口
serialPort.open(
portName = "/dev/ttyUSB0", // Linux
// portName = "COM1", // Windows
// portName = "/dev/ttyS0", // Android
baudRate = 9600,
dataBits = 8,
stopBits = 1,
parity = SerialPort.PARITY_NONE
)
// 写入数据
serialPort.write("Hello".toByteArray())
// 读取数据
serialPort.read().collect { data ->
println("Received: ${String(data)}")
}
// 关闭串口
serialPort.close()try {
serialPort.open("/dev/ttyUSB0", 9600)
} catch (e: SerialPortException) {
println("Failed to open serial port: ${e.message}")
}MIT License