diff --git a/src/main.rs b/src/main.rs index 4652a9b..bafe3a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -134,6 +134,20 @@ async fn run_mqtt_and_serial() -> Result<()> { // 读取配置 let config = read_config()?; + // 检查串口是否被占用 + println!("正在检查串口 {} 是否可用...", config.serial_port); + match serialport::new(&config.serial_port, config.baud_rate) + .timeout(Duration::from_millis(10)) + .open() { + Err(e) => { + if e.kind() == std::io::ErrorKind::PermissionDenied { + eprintln!("串口 {} 已被占用", config.serial_port); + return Ok(()); + } + }, + Ok(_) => {} + } + // 创建 MQTT 客户端 let mut mqttopts = MqttOptions::new( &config.mqtt.client_id,