AI write rust 2

Added features:

  1. use two files in this project

  2. use thread to read and send messages

Fixed issues:

  1. need set timeout for receive serial port, or it won't wait for the data and just return.

vserial.rs

rust 复制代码
use serialport::{SerialPort, SerialPortSettings};


// Define a trait for the serial port operations
pub trait SerialOperations {
    fn open(&mut self) -> Result<(), std::io::Error>;
    fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error>;
    fn receive(&mut self, buffer: &mut [u8]) -> Result<usize, std::io::Error>;
}

// Implement the SerialOperations trait for the Comm struct
pub struct Comm {
    pub port_name: String,
    pub port: Option<Box<dyn SerialPort>>,
}

impl SerialOperations for Comm {
    fn open(&mut self) -> Result<(), std::io::Error> {
        // Create the serial port settings
        let settings = SerialPortSettings {
            baud_rate: 9600,
            ..Default::default()
        };

        // Try to open the serial port with the specified settings
        match serialport::open_with_settings(&self.port_name, &settings) {
            Ok(p) => {
                self.port = Some(p);
                Ok(())
            },
            Err(e) => {
                println!("Error opening serial port: {}", e);
                Err(e.into())
            }
        }
    }

    fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> {
        if let Some(port) = &mut self.port {
            port.write(data)?;
            Ok(())
        } else {
            Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                "Serial port not open",
            ))
        }
    }

    fn receive(&mut self, buffer: &mut [u8]) -> Result<usize, std::io::Error> {
        if let Some(port) = &mut self.port {
            //Ok(port.read(buffer)?)
            match port.read(buffer) {
                Ok(bytes_read) => Ok(bytes_read),
                Err(e) => {
                    println!("Error reading from serial port: {}", e);
                    Err(e.into())
                }
            }
        } else {
            Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                "Serial port not open",
            ))
        }
    }
}

pub fn mod_test(){
    println!("this is module function call\n")
}

main.rs

rust 复制代码
mod vserial;

use crate::vserial::{Comm,SerialOperations};
use std::thread;
use std::time::Duration;

fn thread_send(name: &str)
{
    let mut count = 0;
    let mut comm_send = Comm {
        port_name: "/dev/ttyVSP0".to_string(),
        port: None,
    };
    println!("Thread {} started\n",name);
    // Open the serial port
    match comm_send.open() {
        Ok(p) => p,
        Err(e) => {
            println!("Error opening serial port: {}", e);
            return;
        }
    };
    loop {
        // Write data to the serial port
        if let Err(e) = comm_send.write(b"Hello World!\n") {
            println!("Error writing to serial port: {}", e);
            return;
        }
        if count == 10 { break;}
        count += 1;
    }

    println!("Thread {} end\n",name);
}

fn thread_recv(name: &str)
{
    let mut comm_recv = Comm {
        port_name: "/dev/ttyVSP4".to_string(),
        port: None,
    };
    println!("Thread {} started\n",name);

    // Open the serial port
    match comm_recv.open() {
        Ok(p) => p,
        Err(e) => {
            println!("Error opening serial port: {}", e);
            return;
        }
    };
    let _ = match comm_recv.port.as_mut() {
        Some(port) => { /*expected port of Comm*/
            let mut comm_timeout = port.timeout(); /*directly use port of Comm*/
            println!("Duration is seconds: {:.2?}\n",comm_timeout.as_secs_f32());
            comm_timeout = Duration::from_secs(1);
            let _ = port.set_timeout(comm_timeout);
        },
        None => {
            // Handle the absence of a port here
            ()
        }
    };
    /*let mut comm_port = comm_recv.port.as_mut();
    let mut comm_timeout = comm_port.as_mut().unwrap().timeout();
    println!("Duration is seconds: {:.2?}\n",comm_timeout.as_secs_f32());
    comm_timeout = Duration::from_secs(1);
    let _ = comm_port.as_mut().unwrap().set_timeout(comm_timeout);*/
    loop {
        // Receive data from the serial port
        let mut buffer = [0; 256];
        if let Err(e) = comm_recv.receive(&mut buffer) {
            println!("Error receiving from serial port: {}", e);
            break;
        }

        match std::str::from_utf8(&buffer) {
            Ok(s) => println!("{}", s),
            Err(_) => println!("Invalid UTF-8 sequence"),
        }
    }
    println!("Thread {} end\n",name);
}
fn main() {

    vserial::mod_test();

    thread::spawn(||{
        thread_recv("thread_recv");
    });
    thread::spawn(||{
        thread_send("thread_send");
    });
    thread::sleep(Duration::from_secs(10));

}

Cargo.toml

rust 复制代码
[package]
name = "vserial"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serialport = "3.1"

[lib]
path = "src/vserial.rs"
相关推荐
AC赳赳老秦11 小时前
DeepSeek教育科技应用:智能生成个性化学习规划与知识点拆解教程
前端·网络·数据库·人工智能·学习·matplotlib·deepseek
L Jiawen20 小时前
【Golang基础】基础知识(下)
服务器·开发语言·golang
haimin037121 小时前
linux设置CPU固定频率
linux·运维·服务器
一条咸鱼¥¥¥21 小时前
【运维经验】服务器磁盘做镜像的方法
运维·服务器·windows·经验分享
渣渣盟1 天前
NFS服务器配置全攻略:从入门到精通
linux·运维·服务器
一只旭宝1 天前
Linux专题九:I/O复用(水平以及边缘触发放到libevent库那一专题细讲)
linux·运维·服务器
缘如风1 天前
Linux上sunrpc 111端口关闭
linux·服务器
I · T · LUCKYBOOM1 天前
iptables防火墙
linux·运维·服务器·网络·安全
鸠摩智首席音效师1 天前
如何在 Linux 中使用 uptime 命令 ?
linux·运维·服务器
HalvmånEver1 天前
Linux:匿名管道(进程间通信二)
linux·运维·服务器