sftool_lib/common/
speed.rs

1use crate::SifliToolTrait;
2use crate::common::ram_command::{Command, RamCommand};
3use std::io::Write;
4use std::time::Duration;
5
6/// 通用的速度设置操作实现
7pub struct SpeedOps;
8
9impl SpeedOps {
10    /// 设置串口速度的通用实现
11    pub fn set_speed<T>(tool: &mut T, speed: u32) -> Result<(), std::io::Error>
12    where
13        T: SifliToolTrait + RamCommand,
14    {
15        // 发送设置波特率命令
16        tool.command(Command::SetBaud {
17            baud: speed,
18            delay: 500,
19        })?;
20
21        // 设置串口波特率
22        tool.port().set_baud_rate(speed)?;
23
24        // 等待一段时间让设置生效
25        std::thread::sleep(Duration::from_millis(300));
26
27        // 发送回车换行测试连接
28        tool.port().write_all("\r\n".as_bytes())?;
29        tool.port().flush()?;
30
31        // 再等待一段时间
32        std::thread::sleep(Duration::from_millis(300));
33
34        // 清空缓冲区
35        tool.port().clear(serialport::ClearBuffer::All)?;
36
37        Ok(())
38    }
39}