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"
相关推荐
数据与人工智能律师12 分钟前
当机床开始“思考”,传统“制造”到“智造”升级路上的法律暗礁
大数据·网络·算法·云计算·区块链
开源的6lowpan25 分钟前
无线USB转换器TOS-WLink露面1个月以来总结
网络·无线调试器·无线usb转换器·无线串口·无线jlink·无线stlink
~央千澈~1 小时前
WebSocket与XMPP:即时通讯技术的本质区别与选择逻辑优雅草卓伊凡|片翼|许贝贝
网络·websocket·网络协议
超级土豆粉1 小时前
OSI 七层网络模型
网络
陶然同学2 小时前
从零开始:VMware上的Linux与Java开发环境配置
linux·运维·服务器·vmware·虚拟机
盛满暮色 风止何安2 小时前
BGP基础
运维·服务器·网络·网络协议·tcp/ip·网络安全·智能路由器
阿拉丁的梦2 小时前
ue5的blender4.1groom毛发插件v012安装和使用方法(排除了冲突错误)
linux·运维·服务器
成工小白10 小时前
【Linux】C语言模拟实现shell命令行(程序替换原理)
linux·运维·服务器
西装没钱买11 小时前
C语言多进程TCP服务器与客户端
服务器·c语言·tcp/ip·进程
福理原乡大王11 小时前
Linux信号详解
linux·运维·服务器·c++·ubuntu·信号处理