Skip to content

Commit

Permalink
boolean status
Browse files Browse the repository at this point in the history
  • Loading branch information
chazzhou committed Apr 9, 2023
1 parent d1c00da commit d09cdae
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 60 deletions.
3 changes: 2 additions & 1 deletion examples/svelte-app/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let serialport: Serialport | undefined = undefined;
function openSerialport() {
serialport = new Serialport({ path: 'COM10', baudRate: 9600 });
serialport = new Serialport({ path: 'COM4', baudRate: 115200 });
serialport
.open()
.then((res) => {
Expand Down Expand Up @@ -70,6 +70,7 @@
}
function listen() {
console.log(serialport)
serialport
.listen((data: any[]) => {
console.log('listen serialport data: ', data);
Expand Down
21 changes: 3 additions & 18 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,6 @@ fn get_serialport<T, F: FnOnce(&mut SerialportInfo) -> Result<T, Error>>(
}
}

/// `get_worksheet` 根据 `path` 和 `sheet_name` 获取文件 sheet 实例。
// fn try_get_serialport<T, F: FnOnce(&mut SerialportInfo) -> Result<T, Error>>(
// state: Arc<std::sync::Mutex<HashMap<std::string::String, SerialportInfo>>>,
// path: String,
// f: F,
// ) -> Result<T, Error> {
// match state.try_lock() {
// Ok(mut map) => match map.get_mut(&path) {
// Some(serialport_info) => return f(serialport_info),
// None => {
// return Err(Error::String(format!("未找到 {} 串口", &path)));
// }
// },
// Err(error) => return Err(Error::String(format!("获取文件锁失败! {} ", error))),
// }
// }

fn get_data_bits(value: Option<usize>) -> DataBits {
match value {
Some(value) => match value {
Expand Down Expand Up @@ -193,10 +176,12 @@ pub fn close<R: Runtime>(
if serialports.remove(&path).is_some() {
Ok(())
} else {
print!("Port {} is not opened", path);
Err(Error::String(format!("Port {} is not opened", path)))
}
}
Err(error) => {
println!("Cannot get lock: {}", error);
Err(Error::String(format!("Cannot get lock: {}", error)))
}
}
Expand Down Expand Up @@ -364,7 +349,7 @@ pub fn read<R: Runtime>(
}
}
Err(_err) => {
// println!("读取数据失败! {:?}", err);
println!("Port {} read failed", path);
}
}
thread::sleep(Duration::from_millis(timeout.unwrap_or(200)));
Expand Down
31 changes: 16 additions & 15 deletions webview-dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ declare class Serialport {
*/
static closeAll(): Promise<void>;
/**
* @description: Stop listening to serial port
* @return {Promise<void>}
* @description: Stop listening to the serial port
* @return {Promise<boolean>}
*/
cancelListen(): Promise<void>;
cancelListen(): Promise<boolean>;
/**
* @description: Cancel read serial port data
* @return {Promise<void>}
* @return {Promise<boolean>}
*/
cancelRead(): Promise<void>;
cancelRead(): Promise<boolean>;
/**
* @description:
* @param {object} options
Expand All @@ -84,26 +84,27 @@ declare class Serialport {
}): Promise<void>;
/**
* @description: Close serial port
* @return {Promise<InvokeResult>}
* @return {Promise<boolean>}
*/
close(): Promise<void>;
close(): Promise<boolean>;
/**
* @description: Listen to serial port data
* @description: Register a listener to receive data read from the serial port
* @param {function} fn
* @return {Promise<void>}
* @return {Promise<boolean>}
*/
listen(fn: (...args: any[]) => void, isDecode?: boolean): Promise<void>;
listen(fn: (...args: any[]) => void, isDecode?: boolean): Promise<boolean>;
/**
* @description: Open serial port
* @return {*}
* @return {Promise<boolean>}
*/
open(): Promise<void>;
open(): Promise<boolean>;
/**
* @description: Read data from the serial port
* @description: Tell the backend to start reading the serial port data.
* The backend will read the data and send it to the front end through the listen method.
* @param {ReadOptions} options Read options { timeout, size }
* @return {Promise<void>}
* @return {Promise<boolean>}
*/
read(options?: ReadOptions): Promise<void>;
read(options?: ReadOptions): Promise<boolean>;
/**
* @description: Set the serial port baud rate
* @param {number} value
Expand Down
Loading

0 comments on commit d09cdae

Please # to comment.