From 08e5d5699bfef13068559785ba9097d93d9b7589 Mon Sep 17 00:00:00 2001 From: OCEAN <1010331798@qq.com> Date: Sun, 30 Mar 2025 10:09:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=BA=86=E5=88=A4=E6=96=AD=E7=AB=AF?= =?UTF-8?q?=E5=8F=A3=E6=98=AF=E5=90=A6=E8=A2=AB=E5=8D=A0=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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,