再次提交

This commit is contained in:
2024-08-21 14:19:40 +08:00
parent fb3257bb75
commit 6d47134dac
46 changed files with 1170 additions and 468 deletions

View File

@ -8,16 +8,16 @@ use std::{
thread::sleep,
};
struct SeriesSettings {
pub PortName: String,
pub BaudRate: u32,
pub port_name: String,
pub band_rate: u32,
pub port: Option<Box<dyn serialport::SerialPort>>,
pub isopen: bool,
}
lazy_static! {
static ref PORT_INFO: Arc<Mutex<SeriesSettings>> = Arc::new(Mutex::new(SeriesSettings {
PortName: String::from("NON"),
BaudRate: 9600,
port_name: String::from("NON"),
band_rate: 9600,
port: None,
isopen: false,
}));
@ -25,7 +25,7 @@ lazy_static! {
pub fn tryuseport() -> String {
let mut port_info = PORT_INFO.lock().unwrap();
if port_info.PortName == "NON" {
if port_info.port_name == "NON" {
return format!("Port is not set");
}
if port_info.isopen {
@ -36,7 +36,7 @@ pub fn tryuseport() -> String {
port_info.port = match serialport::new(port_info.PortName.clone(), port_info.BaudRate).open() {
port_info.port = match serialport::new(port_info.port_name.clone(), port_info.band_rate).open() {
Ok(p) => Some(p),
Err(e) => {
eprintln!("Failed to open Error: {}", e);
@ -50,8 +50,8 @@ pub fn tryuseport() -> String {
pub fn set_port_info(portname: &String, baudrate: u32) {
let mut port_info = PORT_INFO.lock().unwrap();
port_info.PortName = portname.to_string();
port_info.BaudRate = baudrate;
port_info.port_name = portname.to_string();
port_info.band_rate = baudrate;
}
@ -66,13 +66,13 @@ pub fn get_port_name() -> Vec<String> {
pub fn _reopenport() -> String {
let mut port_info = PORT_INFO.lock().unwrap();
if port_info.PortName == "NON" {
if port_info.port_name == "NON" {
return format!("Port is not set");
}
//关闭端口
port_info.port = None;
//重新打开端口
port_info.port = match serialport::new(port_info.PortName.clone(), port_info.BaudRate).open() {
port_info.port = match serialport::new(port_info.port_name.clone(), port_info.band_rate).open() {
Ok(p) => Some(p),
Err(e) => {
eprintln!("Failed to open Error: {}", e);
@ -85,7 +85,7 @@ pub fn _reopenport() -> String {
pub fn closeport() -> String {
let mut port_info = PORT_INFO.lock().unwrap();
if port_info.PortName == "NON" {
if port_info.port_name == "NON" {
return format!("Port is not set");
}
//关闭端口
@ -96,7 +96,7 @@ pub fn closeport() -> String {
pub fn sendtoprot(data: Vec<u8>) -> String {
let mut port_info = PORT_INFO.lock().unwrap();
if port_info.PortName == "NON" {
if port_info.port_name == "NON" {
return format!("Port is not set");
}
@ -119,7 +119,7 @@ pub fn sendtoprot(data: Vec<u8>) -> String {
pub fn clearserilport() -> String{
let mut port_info = PORT_INFO.lock().unwrap();
if port_info.PortName == "NON" {
if port_info.port_name == "NON" {
return "Port is not set".to_string();
}
let mut buf: Vec<u8> = vec![0; 1000];
@ -148,7 +148,7 @@ pub fn clearserilport() -> String{
pub fn readdatafromport(waittime: u64, commanid: u8) -> Vec<u8> {
let mut port_info = PORT_INFO.lock().unwrap();
if port_info.PortName == "NON" {
if port_info.port_name == "NON" {
return "Port is not set".as_bytes().to_vec();
}
let mut buf: Vec<u8> = vec![0; 1000];
@ -236,7 +236,27 @@ pub fn iris_calc_crc(p_buffer: &[u8]) -> u16 {
crc
}
pub fn readforport()->Vec<u8>{
let mut port_info = PORT_INFO.lock().unwrap();
if port_info.port_name == "NON" {
return "Port is not set".as_bytes().to_vec();
}
let mut buf: Vec<u8> = vec![0; 1000];
match &mut port_info.port {
Some(p) => {
p.set_timeout(Duration::from_millis(100)).unwrap();
let sizeread =match p.read(&mut buf){
Ok(size)=>{size},
Err(_e)=>{0}
};
buf.truncate(sizeread);
return buf;
}
None => {
return Vec::new();
}
}
}
#[test]
fn test() {
tryuseport();