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"
相关推荐
鼹鼠SDN2 小时前
没有公网 IP,如何在外面访问家里的打印机、摄像头和整个局域网?
网络·网络协议·tcp/ip
小诗懂技术4 小时前
【网络通信UDP】基础 进程间不同主机的通信
运维·服务器·网络
weixin_440730504 小时前
selenium实战记录-从登陆-首页-进入应用流程用例三种参数化+日志+报告
服务器·selenium·测试工具
Hstar_chen5 小时前
开源一款 HarmonyOS 服务器监控客户端:星辰云巡 1.0.3
服务器·华为·harmonyos
DM今天肝到几点?6 小时前
AI 安全进入「攻防同频」:Gemini 3.8 Flash Cyber 上线、Astra 触及关键级、HiddenLayer 融资 1 亿美元
网络·人工智能·深度学习·安全·语言模型·开源·知识图谱
幼儿园蛋小黑7 小时前
TCP 并发:从原理到高并发实践
网络·网络协议·tcp/ip
开心大爆炸8 小时前
xrdp 连接 登录对话框输入密码后闪退
linux·运维·服务器
我爱cope9 小时前
【计算机网络 | 网络层11:ICMP 协议:Ping 和 Traceroute 的底层原理】
网络·学习·计算机网络
Latticy9 小时前
vulnhub系列靶机-Photographer
网络·网络安全·内网渗透·内网
爱浦路 IPLOOK9 小时前
矿山无人化作业5G专网方案:企业专网核心网络选型分析
网络·分布式·科技·5g·信息与通信