| Crates.io | unistore-serial |
| lib.rs | unistore-serial |
| version | 0.1.0 |
| created_at | 2026-01-20 01:43:50.558068+00 |
| updated_at | 2026-01-20 01:43:50.558068+00 |
| description | Serial port communication capability for UniStore |
| homepage | https://github.com/yangbo1317/unistore |
| repository | https://github.com/yangbo1317/unistore |
| max_upload_size | |
| id | 2055637 |
| size | 50,167 |
UniStore 串口通信能力,提供跨平台的 RS232/UART 通信支持。
unistore-serial 提供:
[dependencies]
unistore-serial = "0.1"
use unistore_serial::{SerialPort, SerialConfig, list_ports};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 列出可用串口
let ports = list_ports()?;
for port in &ports {
println!("发现串口: {} - {:?}", port.name, port.port_type);
}
// 打开串口
let config = SerialConfig::default()
.with_baud_rate(115200)
.with_timeout_ms(1000);
let mut port = SerialPort::open("COM1", config)?;
// 写入数据
port.write(b"AT\r\n")?;
// 读取响应
let mut buf = [0u8; 256];
let n = port.read(&mut buf)?;
println!("收到: {:?}", &buf[..n]);
Ok(())
}
// 常见波特率配置
let config = SerialConfig::baud_115200_8n1(); // 115200, 8N1
let config = SerialConfig::baud_9600_8n1(); // 9600, 8N1
// 设置 DTR/RTS
port.set_dtr(true)?;
port.set_rts(true)?;
// 读取信号状态
let cts = port.read_cts()?;
let dsr = port.read_dsr()?;
let cd = port.read_cd()?;
需要直接使用 serialport 时:
let inner = port.inner();
// inner 是 Box<dyn serialport::SerialPort>
| 平台 | 支持状态 |
|---|---|
| Windows | ✅ COM1, COM2, ... |
| Linux | ✅ /dev/ttyUSB0, /dev/ttyACM0, ... |
| macOS | ✅ /dev/tty.usbserial-*, ... |
MIT OR Apache-2.0
本 crate 基于以下优秀项目构建:
感谢 serialport-rs 团队提供的出色工作!
许可证说明: serialport-rs 使用 MPL-2.0 许可证。UniStore 作为依赖使用, 不修改其源码,符合 MPL-2.0 的使用条款。