From d09cdae95569d846fdf249ff564439818f89dcda Mon Sep 17 00:00:00 2001 From: Charles Zhou Date: Sun, 9 Apr 2023 01:33:46 -0500 Subject: [PATCH] boolean status --- examples/svelte-app/src/App.svelte | 3 +- src/command.rs | 21 ++---------- webview-dist/index.d.ts | 31 ++++++++--------- webview-dist/index.js | 2 +- webview-src/index.ts | 53 ++++++++++++++++-------------- 5 files changed, 50 insertions(+), 60 deletions(-) diff --git a/examples/svelte-app/src/App.svelte b/examples/svelte-app/src/App.svelte index 295cced..ef3a55e 100644 --- a/examples/svelte-app/src/App.svelte +++ b/examples/svelte-app/src/App.svelte @@ -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) => { @@ -70,6 +70,7 @@ } function listen() { + console.log(serialport) serialport .listen((data: any[]) => { console.log('listen serialport data: ', data); diff --git a/src/command.rs b/src/command.rs index c567aa3..37475f5 100644 --- a/src/command.rs +++ b/src/command.rs @@ -25,23 +25,6 @@ fn get_serialport Result>( } } -/// `get_worksheet` 根据 `path` 和 `sheet_name` 获取文件 sheet 实例。 -// fn try_get_serialport Result>( -// state: Arc>>, -// path: String, -// f: F, -// ) -> Result { -// 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) -> DataBits { match value { Some(value) => match value { @@ -193,10 +176,12 @@ pub fn close( 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))) } } @@ -364,7 +349,7 @@ pub fn read( } } Err(_err) => { - // println!("读取数据失败! {:?}", err); + println!("Port {} read failed", path); } } thread::sleep(Duration::from_millis(timeout.unwrap_or(200))); diff --git a/webview-dist/index.d.ts b/webview-dist/index.d.ts index e1990d4..5a3f8e5 100644 --- a/webview-dist/index.d.ts +++ b/webview-dist/index.d.ts @@ -64,15 +64,15 @@ declare class Serialport { */ static closeAll(): Promise; /** - * @description: Stop listening to serial port - * @return {Promise} + * @description: Stop listening to the serial port + * @return {Promise} */ - cancelListen(): Promise; + cancelListen(): Promise; /** * @description: Cancel read serial port data - * @return {Promise} + * @return {Promise} */ - cancelRead(): Promise; + cancelRead(): Promise; /** * @description: * @param {object} options @@ -84,26 +84,27 @@ declare class Serialport { }): Promise; /** * @description: Close serial port - * @return {Promise} + * @return {Promise} */ - close(): Promise; + close(): Promise; /** - * @description: Listen to serial port data + * @description: Register a listener to receive data read from the serial port * @param {function} fn - * @return {Promise} + * @return {Promise} */ - listen(fn: (...args: any[]) => void, isDecode?: boolean): Promise; + listen(fn: (...args: any[]) => void, isDecode?: boolean): Promise; /** * @description: Open serial port - * @return {*} + * @return {Promise} */ - open(): Promise; + open(): Promise; /** - * @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} + * @return {Promise} */ - read(options?: ReadOptions): Promise; + read(options?: ReadOptions): Promise; /** * @description: Set the serial port baud rate * @param {number} value diff --git a/webview-dist/index.js b/webview-dist/index.js index d31ae65..a6b68ee 100644 --- a/webview-dist/index.js +++ b/webview-dist/index.js @@ -1 +1 @@ -function e(e,t,a,n){return new(a||(a=Promise))((function(i,s){function r(e){try{l(n.next(e))}catch(e){s(e)}}function o(e){try{l(n.throw(e))}catch(e){s(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof a?t:new a((function(e){e(t)}))).then(r,o)}l((n=n.apply(e,t||[])).next())}))}function t(e,t){var a,n,i,s,r={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return s={next:o(0),throw:o(1),return:o(2)},"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function o(o){return function(l){return function(o){if(a)throw new TypeError("Generator is already executing.");for(;s&&(s=0,o[0]&&(r=0)),r;)try{if(a=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return r.label++,{value:o[1],done:!1};case 5:r.label++,n=o[1],o=[0];continue;case 7:o=r.ops.pop(),r.trys.pop();continue;default:if(!(i=r.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){r=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]{for(var n in t)a(e,n,{get:t[n],enumerable:!0})};function i(e,t=!1){let a=window.crypto.getRandomValues(new Uint32Array(1))[0],n=`_${a}`;return Object.defineProperty(window,n,{value:a=>(t&&Reflect.deleteProperty(window,n),null==e?void 0:e(a)),writable:!1,configurable:!0}),a}async function s(e,t={}){return new Promise(((a,n)=>{let s=i((e=>{a(e),Reflect.deleteProperty(window,`_${r}`)}),!0),r=i((e=>{n(e),Reflect.deleteProperty(window,`_${s}`)}),!0);window.__TAURI_IPC__({cmd:e,callback:s,error:r,...t})}))}function r(e,t="asset"){let a=encodeURIComponent(e);return navigator.userAgent.includes("Windows")?`https://${t}.localhost/${a}`:`${t}://localhost/${a}`}async function o(e){return s("tauri",e)}n({},{convertFileSrc:()=>r,invoke:()=>s,transformCallback:()=>i});async function l(e,t){return o({__tauriModule:"Event",message:{cmd:"unlisten",event:e,eventId:t}})}async function u(e,t,a){await o({__tauriModule:"Event",message:{cmd:"emit",event:e,windowLabel:t,payload:a}})}async function c(e,t,a){return o({__tauriModule:"Event",message:{cmd:"listen",event:e,windowLabel:t,handler:i(a)}}).then((t=>async()=>l(e,t)))}async function d(e,t,a){return c(e,t,(t=>{a(t),l(e,t.id).catch((()=>{}))}))}n({},{TauriEvent:()=>h,emit:()=>y,listen:()=>p,once:()=>m});var h=(e=>(e.WINDOW_RESIZED="tauri://resize",e.WINDOW_MOVED="tauri://move",e.WINDOW_CLOSE_REQUESTED="tauri://close-requested",e.WINDOW_CREATED="tauri://window-created",e.WINDOW_DESTROYED="tauri://destroyed",e.WINDOW_FOCUS="tauri://focus",e.WINDOW_BLUR="tauri://blur",e.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",e.WINDOW_THEME_CHANGED="tauri://theme-changed",e.WINDOW_FILE_DROP="tauri://file-drop",e.WINDOW_FILE_DROP_HOVER="tauri://file-drop-hover",e.WINDOW_FILE_DROP_CANCELLED="tauri://file-drop-cancelled",e.MENU="tauri://menu",e.CHECK_UPDATE="tauri://update",e.UPDATE_AVAILABLE="tauri://update-available",e.INSTALL_UPDATE="tauri://update-install",e.STATUS_UPDATE="tauri://update-status",e.DOWNLOAD_PROGRESS="tauri://update-download-progress",e))(h||{});async function p(e,t){return c(e,null,t)}async function m(e,t){return d(e,null,t)}async function y(e,t){return u(e,void 0,t)}n({},{CloseRequestedEvent:()=>D,LogicalPosition:()=>g,LogicalSize:()=>w,PhysicalPosition:()=>_,PhysicalSize:()=>b,UserAttentionType:()=>f,WebviewWindow:()=>z,WebviewWindowHandle:()=>A,WindowManager:()=>E,appWindow:()=>P,availableMonitors:()=>O,currentMonitor:()=>R,getAll:()=>W,getCurrent:()=>v,primaryMonitor:()=>L});var w=class{constructor(e,t){this.type="Logical",this.width=e,this.height=t}},b=class{constructor(e,t){this.type="Physical",this.width=e,this.height=t}toLogical(e){return new w(this.width/e,this.height/e)}},g=class{constructor(e,t){this.type="Logical",this.x=e,this.y=t}},_=class{constructor(e,t){this.type="Physical",this.x=e,this.y=t}toLogical(e){return new g(this.x/e,this.y/e)}},f=(e=>(e[e.Critical=1]="Critical",e[e.Informational=2]="Informational",e))(f||{});function v(){return new z(window.__TAURI_METADATA__.__currentWindow.label,{skip:!0})}function W(){return window.__TAURI_METADATA__.__windows.map((e=>new z(e.label,{skip:!0})))}var P,M=["tauri://created","tauri://error"],A=class{constructor(e){this.label=e,this.listeners=Object.create(null)}async listen(e,t){return this._handleTauriEvent(e,t)?Promise.resolve((()=>{let a=this.listeners[e];a.splice(a.indexOf(t),1)})):c(e,this.label,t)}async once(e,t){return this._handleTauriEvent(e,t)?Promise.resolve((()=>{let a=this.listeners[e];a.splice(a.indexOf(t),1)})):d(e,this.label,t)}async emit(e,t){if(M.includes(e)){for(let a of this.listeners[e]||[])a({event:e,id:-1,windowLabel:this.label,payload:t});return Promise.resolve()}return u(e,this.label,t)}_handleTauriEvent(e,t){return!!M.includes(e)&&(e in this.listeners?this.listeners[e].push(t):this.listeners[e]=[t],!0)}},E=class extends A{async scaleFactor(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"scaleFactor"}}}})}async innerPosition(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"innerPosition"}}}}).then((({x:e,y:t})=>new _(e,t)))}async outerPosition(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"outerPosition"}}}}).then((({x:e,y:t})=>new _(e,t)))}async innerSize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"innerSize"}}}}).then((({width:e,height:t})=>new b(e,t)))}async outerSize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"outerSize"}}}}).then((({width:e,height:t})=>new b(e,t)))}async isFullscreen(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"isFullscreen"}}}})}async isMaximized(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"isMaximized"}}}})}async isDecorated(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"isDecorated"}}}})}async isResizable(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"isResizable"}}}})}async isVisible(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"isVisible"}}}})}async theme(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"theme"}}}})}async center(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"center"}}}})}async requestUserAttention(e){let t=null;return e&&(t=1===e?{type:"Critical"}:{type:"Informational"}),o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"requestUserAttention",payload:t}}}})}async setResizable(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setResizable",payload:e}}}})}async setTitle(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setTitle",payload:e}}}})}async maximize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"maximize"}}}})}async unmaximize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"unmaximize"}}}})}async toggleMaximize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"toggleMaximize"}}}})}async minimize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"minimize"}}}})}async unminimize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"unminimize"}}}})}async show(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"show"}}}})}async hide(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"hide"}}}})}async close(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"close"}}}})}async setDecorations(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setDecorations",payload:e}}}})}async setAlwaysOnTop(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setAlwaysOnTop",payload:e}}}})}async setSize(e){if(!e||"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `size` argument must be either a LogicalSize or a PhysicalSize instance");return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setSize",payload:{type:e.type,data:{width:e.width,height:e.height}}}}}})}async setMinSize(e){if(e&&"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `size` argument must be either a LogicalSize or a PhysicalSize instance");return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setMinSize",payload:e?{type:e.type,data:{width:e.width,height:e.height}}:null}}}})}async setMaxSize(e){if(e&&"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `size` argument must be either a LogicalSize or a PhysicalSize instance");return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setMaxSize",payload:e?{type:e.type,data:{width:e.width,height:e.height}}:null}}}})}async setPosition(e){if(!e||"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `position` argument must be either a LogicalPosition or a PhysicalPosition instance");return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setPosition",payload:{type:e.type,data:{x:e.x,y:e.y}}}}}})}async setFullscreen(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setFullscreen",payload:e}}}})}async setFocus(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setFocus"}}}})}async setIcon(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setIcon",payload:{icon:"string"==typeof e?e:Array.from(e)}}}}})}async setSkipTaskbar(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setSkipTaskbar",payload:e}}}})}async setCursorGrab(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setCursorGrab",payload:e}}}})}async setCursorVisible(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setCursorVisible",payload:e}}}})}async setCursorIcon(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setCursorIcon",payload:e}}}})}async setCursorPosition(e){if(!e||"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `position` argument must be either a LogicalPosition or a PhysicalPosition instance");return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setCursorPosition",payload:{type:e.type,data:{x:e.x,y:e.y}}}}}})}async setIgnoreCursorEvents(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setIgnoreCursorEvents",payload:e}}}})}async startDragging(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"startDragging"}}}})}async onResized(e){return this.listen("tauri://resize",e)}async onMoved(e){return this.listen("tauri://move",e)}async onCloseRequested(e){return this.listen("tauri://close-requested",(t=>{let a=new D(t);Promise.resolve(e(a)).then((()=>{if(!a.isPreventDefault())return this.close()}))}))}async onFocusChanged(e){let t=await this.listen("tauri://focus",(t=>{e({...t,payload:!0})})),a=await this.listen("tauri://blur",(t=>{e({...t,payload:!1})}));return()=>{t(),a()}}async onScaleChanged(e){return this.listen("tauri://scale-change",e)}async onMenuClicked(e){return this.listen("tauri://menu",e)}async onFileDropEvent(e){let t=await this.listen("tauri://file-drop",(t=>{e({...t,payload:{type:"drop",paths:t.payload}})})),a=await this.listen("tauri://file-drop-hover",(t=>{e({...t,payload:{type:"hover",paths:t.payload}})})),n=await this.listen("tauri://file-drop-cancelled",(t=>{e({...t,payload:{type:"cancel"}})}));return()=>{t(),a(),n()}}async onThemeChanged(e){return this.listen("tauri://theme-changed",e)}},D=class{constructor(e){this._preventDefault=!1,this.event=e.event,this.windowLabel=e.windowLabel,this.id=e.id}preventDefault(){this._preventDefault=!0}isPreventDefault(){return this._preventDefault}},z=class extends E{constructor(e,t={}){super(e),null!=t&&t.skip||o({__tauriModule:"Window",message:{cmd:"createWebview",data:{options:{label:e,...t}}}}).then((async()=>this.emit("tauri://created"))).catch((async e=>this.emit("tauri://error",e)))}static getByLabel(e){return W().some((t=>t.label===e))?new z(e,{skip:!0}):null}};function T(e){return null===e?null:{name:e.name,scaleFactor:e.scaleFactor,position:new _(e.position.x,e.position.y),size:new b(e.size.width,e.size.height)}}async function R(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{cmd:{type:"currentMonitor"}}}}).then(T)}async function L(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{cmd:{type:"primaryMonitor"}}}}).then(T)}async function O(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{cmd:{type:"availableMonitors"}}}}).then((e=>e.map(T)))}"__TAURI_METADATA__"in window?P=new z(window.__TAURI_METADATA__.__currentWindow.label,{skip:!0}):(console.warn('Could not find "window.__TAURI_METADATA__". The "appWindow" value will reference the "main" window label.\nNote that this is not an issue if running this frontend on a browser instead of a Tauri window.'),P=new z("main",{skip:!0}));var C=function(){function a(e){this.isOpen=!1,this.encoding=e.encoding||"utf-8",this.options={path:e.path,baudRate:e.baudRate,dataBits:e.dataBits||8,flowControl:e.flowControl||null,parity:e.parity||null,stopBits:e.stopBits||2,timeout:e.timeout||200},this.size=e.size||1024}return a.available_ports=function(){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,s("plugin:serialport|available_ports")];case 1:return[2,t.sent()];case 2:return e=t.sent(),[2,Promise.reject(e)];case 3:return[2]}}))}))},a.forceClose=function(a){return e(this,void 0,void 0,(function(){return t(this,(function(e){switch(e.label){case 0:return[4,s("plugin:serialport|force_close",{path:a})];case 1:return[2,e.sent()]}}))}))},a.closeAll=function(){return e(this,void 0,void 0,(function(){return t(this,(function(e){switch(e.label){case 0:return[4,s("plugin:serialport|close_all")];case 1:return[2,e.sent()]}}))}))},a.prototype.cancelListen=function(){return e(this,void 0,void 0,(function(){return t(this,(function(e){try{return this.unListen&&(this.unListen(),this.unListen=void 0),[2]}catch(e){return[2,Promise.reject("Failed to stop listening to the serial port: "+e)]}return[2]}))}))},a.prototype.cancelRead=function(){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,s("plugin:serialport|cancel_read",{path:this.options.path})];case 1:return[2,t.sent()];case 2:return e=t.sent(),[2,Promise.reject(e)];case 3:return[2]}}))}))},a.prototype.change=function(a){return e(this,void 0,void 0,(function(){var e,n;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,5,,6]),e=!1,this.isOpen?(e=!0,[4,this.close()]):[3,2];case 1:t.sent(),t.label=2;case 2:return a.path&&(this.options.path=a.path),a.baudRate&&(this.options.baudRate=a.baudRate),e?[4,this.open()]:[3,4];case 3:t.sent(),t.label=4;case 4:return[2,Promise.resolve()];case 5:return n=t.sent(),[2,Promise.reject(n)];case 6:return[2]}}))}))},a.prototype.close=function(){return e(this,void 0,void 0,(function(){var e,a;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,4,,5]),this.isOpen?[4,this.cancelRead()]:[2];case 1:return t.sent(),[4,s("plugin:serialport|close",{path:this.options.path})];case 2:return e=t.sent(),[4,this.cancelListen()];case 3:return t.sent(),this.isOpen=!1,[2,e];case 4:return a=t.sent(),[2,Promise.reject(a)];case 5:return[2]}}))}))},a.prototype.listen=function(a,n){return void 0===n&&(n=!0),e(this,void 0,void 0,(function(){var e,i,s,r=this;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,3,,4]),[4,this.cancelListen()];case 1:return t.sent(),e="plugin-serialport-read-"+this.options.path,i=this,[4,P.listen(e,(function(e){var t=e.payload;try{if(n){var i=new TextDecoder(r.encoding).decode(new Uint8Array(t.data));a(i)}else a(new Uint8Array(t.data))}catch(e){console.error(e)}}))];case 2:return i.unListen=t.sent(),[2];case 3:return s=t.sent(),[2,Promise.reject("Failed to listen to the serial port: "+s)];case 4:return[2]}}))}))},a.prototype.open=function(){return e(this,void 0,void 0,(function(){var e,a;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),this.options.path?this.options.baudRate?this.isOpen?[2]:[4,s("plugin:serialport|open",{path:this.options.path,baudRate:this.options.baudRate,dataBits:this.options.dataBits,flowControl:this.options.flowControl,parity:this.options.parity,stopBits:this.options.stopBits,timeout:this.options.timeout})]:[2,Promise.reject("baudRate cannot be empty!")]:[2,Promise.reject("path cannot be empty!")];case 1:return e=t.sent(),this.isOpen=!0,[2,Promise.resolve(e)];case 2:return a=t.sent(),[2,Promise.reject(a)];case 3:return[2]}}))}))},a.prototype.read=function(a){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,s("plugin:serialport|read",{path:this.options.path,timeout:(null==a?void 0:a.timeout)||this.options.timeout,size:(null==a?void 0:a.size)||this.size})];case 1:return[2,t.sent()];case 2:return e=t.sent(),[2,Promise.reject(e)];case 3:return[2]}}))}))},a.prototype.setBaudRate=function(a){return e(this,void 0,void 0,(function(){var e,n;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,5,,6]),e=!1,this.isOpen?(e=!0,[4,this.close()]):[3,2];case 1:t.sent(),t.label=2;case 2:return this.options.baudRate=a,e?[4,this.open()]:[3,4];case 3:t.sent(),t.label=4;case 4:return[2,Promise.resolve()];case 5:return n=t.sent(),[2,Promise.reject(n)];case 6:return[2]}}))}))},a.prototype.setPath=function(a){return e(this,void 0,void 0,(function(){var e,n;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,5,,6]),e=!1,this.isOpen?(e=!0,[4,this.close()]):[3,2];case 1:t.sent(),t.label=2;case 2:return this.options.path=a,e?[4,this.open()]:[3,4];case 3:t.sent(),t.label=4;case 4:return[2,Promise.resolve()];case 5:return n=t.sent(),[2,Promise.reject(n)];case 6:return[2]}}))}))},a.prototype.write=function(a){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),this.isOpen?[4,s("plugin:serialport|write",{value:a,path:this.options.path})]:[2,Promise.reject("Port ".concat(this.options.path," is not open!"))];case 1:return[2,t.sent()];case 2:return e=t.sent(),[2,Promise.reject(e)];case 3:return[2]}}))}))},a.prototype.writeBinary=function(a){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,4,,5]),this.isOpen?a instanceof Uint8Array||a instanceof Array?[4,s("plugin:serialport|write_binary",{value:Array.from(a),path:this.options.path})]:[3,2]:[2,Promise.reject("Port ".concat(this.options.path," is not open!"))];case 1:return[2,t.sent()];case 2:return[2,Promise.reject("value Parameter must be of type string, Uint8Array, or number[]!")];case 3:return[3,5];case 4:return e=t.sent(),[2,Promise.reject(e)];case 5:return[2]}}))}))},a}();export{C as Serialport}; +function e(e,t,a,n){return new(a||(a=Promise))((function(i,s){function r(e){try{l(n.next(e))}catch(e){s(e)}}function o(e){try{l(n.throw(e))}catch(e){s(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof a?t:new a((function(e){e(t)}))).then(r,o)}l((n=n.apply(e,t||[])).next())}))}function t(e,t){var a,n,i,s,r={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return s={next:o(0),throw:o(1),return:o(2)},"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function o(o){return function(l){return function(o){if(a)throw new TypeError("Generator is already executing.");for(;s&&(s=0,o[0]&&(r=0)),r;)try{if(a=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return r.label++,{value:o[1],done:!1};case 5:r.label++,n=o[1],o=[0];continue;case 7:o=r.ops.pop(),r.trys.pop();continue;default:if(!(i=r.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){r=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]{for(var n in t)a(e,n,{get:t[n],enumerable:!0})};function i(e,t=!1){let a=window.crypto.getRandomValues(new Uint32Array(1))[0],n=`_${a}`;return Object.defineProperty(window,n,{value:a=>(t&&Reflect.deleteProperty(window,n),null==e?void 0:e(a)),writable:!1,configurable:!0}),a}async function s(e,t={}){return new Promise(((a,n)=>{let s=i((e=>{a(e),Reflect.deleteProperty(window,`_${r}`)}),!0),r=i((e=>{n(e),Reflect.deleteProperty(window,`_${s}`)}),!0);window.__TAURI_IPC__({cmd:e,callback:s,error:r,...t})}))}function r(e,t="asset"){let a=encodeURIComponent(e);return navigator.userAgent.includes("Windows")?`https://${t}.localhost/${a}`:`${t}://localhost/${a}`}async function o(e){return s("tauri",e)}n({},{convertFileSrc:()=>r,invoke:()=>s,transformCallback:()=>i});async function l(e,t){return o({__tauriModule:"Event",message:{cmd:"unlisten",event:e,eventId:t}})}async function u(e,t,a){await o({__tauriModule:"Event",message:{cmd:"emit",event:e,windowLabel:t,payload:a}})}async function c(e,t,a){return o({__tauriModule:"Event",message:{cmd:"listen",event:e,windowLabel:t,handler:i(a)}}).then((t=>async()=>l(e,t)))}async function d(e,t,a){return c(e,t,(t=>{a(t),l(e,t.id).catch((()=>{}))}))}n({},{TauriEvent:()=>h,emit:()=>y,listen:()=>p,once:()=>m});var h=(e=>(e.WINDOW_RESIZED="tauri://resize",e.WINDOW_MOVED="tauri://move",e.WINDOW_CLOSE_REQUESTED="tauri://close-requested",e.WINDOW_CREATED="tauri://window-created",e.WINDOW_DESTROYED="tauri://destroyed",e.WINDOW_FOCUS="tauri://focus",e.WINDOW_BLUR="tauri://blur",e.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",e.WINDOW_THEME_CHANGED="tauri://theme-changed",e.WINDOW_FILE_DROP="tauri://file-drop",e.WINDOW_FILE_DROP_HOVER="tauri://file-drop-hover",e.WINDOW_FILE_DROP_CANCELLED="tauri://file-drop-cancelled",e.MENU="tauri://menu",e.CHECK_UPDATE="tauri://update",e.UPDATE_AVAILABLE="tauri://update-available",e.INSTALL_UPDATE="tauri://update-install",e.STATUS_UPDATE="tauri://update-status",e.DOWNLOAD_PROGRESS="tauri://update-download-progress",e))(h||{});async function p(e,t){return c(e,null,t)}async function m(e,t){return d(e,null,t)}async function y(e,t){return u(e,void 0,t)}n({},{CloseRequestedEvent:()=>D,LogicalPosition:()=>g,LogicalSize:()=>w,PhysicalPosition:()=>_,PhysicalSize:()=>b,UserAttentionType:()=>f,WebviewWindow:()=>z,WebviewWindowHandle:()=>A,WindowManager:()=>E,appWindow:()=>W,availableMonitors:()=>O,currentMonitor:()=>R,getAll:()=>P,getCurrent:()=>v,primaryMonitor:()=>L});var w=class{constructor(e,t){this.type="Logical",this.width=e,this.height=t}},b=class{constructor(e,t){this.type="Physical",this.width=e,this.height=t}toLogical(e){return new w(this.width/e,this.height/e)}},g=class{constructor(e,t){this.type="Logical",this.x=e,this.y=t}},_=class{constructor(e,t){this.type="Physical",this.x=e,this.y=t}toLogical(e){return new g(this.x/e,this.y/e)}},f=(e=>(e[e.Critical=1]="Critical",e[e.Informational=2]="Informational",e))(f||{});function v(){return new z(window.__TAURI_METADATA__.__currentWindow.label,{skip:!0})}function P(){return window.__TAURI_METADATA__.__windows.map((e=>new z(e.label,{skip:!0})))}var W,M=["tauri://created","tauri://error"],A=class{constructor(e){this.label=e,this.listeners=Object.create(null)}async listen(e,t){return this._handleTauriEvent(e,t)?Promise.resolve((()=>{let a=this.listeners[e];a.splice(a.indexOf(t),1)})):c(e,this.label,t)}async once(e,t){return this._handleTauriEvent(e,t)?Promise.resolve((()=>{let a=this.listeners[e];a.splice(a.indexOf(t),1)})):d(e,this.label,t)}async emit(e,t){if(M.includes(e)){for(let a of this.listeners[e]||[])a({event:e,id:-1,windowLabel:this.label,payload:t});return Promise.resolve()}return u(e,this.label,t)}_handleTauriEvent(e,t){return!!M.includes(e)&&(e in this.listeners?this.listeners[e].push(t):this.listeners[e]=[t],!0)}},E=class extends A{async scaleFactor(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"scaleFactor"}}}})}async innerPosition(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"innerPosition"}}}}).then((({x:e,y:t})=>new _(e,t)))}async outerPosition(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"outerPosition"}}}}).then((({x:e,y:t})=>new _(e,t)))}async innerSize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"innerSize"}}}}).then((({width:e,height:t})=>new b(e,t)))}async outerSize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"outerSize"}}}}).then((({width:e,height:t})=>new b(e,t)))}async isFullscreen(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"isFullscreen"}}}})}async isMaximized(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"isMaximized"}}}})}async isDecorated(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"isDecorated"}}}})}async isResizable(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"isResizable"}}}})}async isVisible(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"isVisible"}}}})}async theme(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"theme"}}}})}async center(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"center"}}}})}async requestUserAttention(e){let t=null;return e&&(t=1===e?{type:"Critical"}:{type:"Informational"}),o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"requestUserAttention",payload:t}}}})}async setResizable(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setResizable",payload:e}}}})}async setTitle(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setTitle",payload:e}}}})}async maximize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"maximize"}}}})}async unmaximize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"unmaximize"}}}})}async toggleMaximize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"toggleMaximize"}}}})}async minimize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"minimize"}}}})}async unminimize(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"unminimize"}}}})}async show(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"show"}}}})}async hide(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"hide"}}}})}async close(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"close"}}}})}async setDecorations(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setDecorations",payload:e}}}})}async setAlwaysOnTop(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setAlwaysOnTop",payload:e}}}})}async setSize(e){if(!e||"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `size` argument must be either a LogicalSize or a PhysicalSize instance");return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setSize",payload:{type:e.type,data:{width:e.width,height:e.height}}}}}})}async setMinSize(e){if(e&&"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `size` argument must be either a LogicalSize or a PhysicalSize instance");return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setMinSize",payload:e?{type:e.type,data:{width:e.width,height:e.height}}:null}}}})}async setMaxSize(e){if(e&&"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `size` argument must be either a LogicalSize or a PhysicalSize instance");return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setMaxSize",payload:e?{type:e.type,data:{width:e.width,height:e.height}}:null}}}})}async setPosition(e){if(!e||"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `position` argument must be either a LogicalPosition or a PhysicalPosition instance");return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setPosition",payload:{type:e.type,data:{x:e.x,y:e.y}}}}}})}async setFullscreen(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setFullscreen",payload:e}}}})}async setFocus(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setFocus"}}}})}async setIcon(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setIcon",payload:{icon:"string"==typeof e?e:Array.from(e)}}}}})}async setSkipTaskbar(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setSkipTaskbar",payload:e}}}})}async setCursorGrab(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setCursorGrab",payload:e}}}})}async setCursorVisible(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setCursorVisible",payload:e}}}})}async setCursorIcon(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setCursorIcon",payload:e}}}})}async setCursorPosition(e){if(!e||"Logical"!==e.type&&"Physical"!==e.type)throw new Error("the `position` argument must be either a LogicalPosition or a PhysicalPosition instance");return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setCursorPosition",payload:{type:e.type,data:{x:e.x,y:e.y}}}}}})}async setIgnoreCursorEvents(e){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"setIgnoreCursorEvents",payload:e}}}})}async startDragging(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{label:this.label,cmd:{type:"startDragging"}}}})}async onResized(e){return this.listen("tauri://resize",e)}async onMoved(e){return this.listen("tauri://move",e)}async onCloseRequested(e){return this.listen("tauri://close-requested",(t=>{let a=new D(t);Promise.resolve(e(a)).then((()=>{if(!a.isPreventDefault())return this.close()}))}))}async onFocusChanged(e){let t=await this.listen("tauri://focus",(t=>{e({...t,payload:!0})})),a=await this.listen("tauri://blur",(t=>{e({...t,payload:!1})}));return()=>{t(),a()}}async onScaleChanged(e){return this.listen("tauri://scale-change",e)}async onMenuClicked(e){return this.listen("tauri://menu",e)}async onFileDropEvent(e){let t=await this.listen("tauri://file-drop",(t=>{e({...t,payload:{type:"drop",paths:t.payload}})})),a=await this.listen("tauri://file-drop-hover",(t=>{e({...t,payload:{type:"hover",paths:t.payload}})})),n=await this.listen("tauri://file-drop-cancelled",(t=>{e({...t,payload:{type:"cancel"}})}));return()=>{t(),a(),n()}}async onThemeChanged(e){return this.listen("tauri://theme-changed",e)}},D=class{constructor(e){this._preventDefault=!1,this.event=e.event,this.windowLabel=e.windowLabel,this.id=e.id}preventDefault(){this._preventDefault=!0}isPreventDefault(){return this._preventDefault}},z=class extends E{constructor(e,t={}){super(e),null!=t&&t.skip||o({__tauriModule:"Window",message:{cmd:"createWebview",data:{options:{label:e,...t}}}}).then((async()=>this.emit("tauri://created"))).catch((async e=>this.emit("tauri://error",e)))}static getByLabel(e){return P().some((t=>t.label===e))?new z(e,{skip:!0}):null}};function T(e){return null===e?null:{name:e.name,scaleFactor:e.scaleFactor,position:new _(e.position.x,e.position.y),size:new b(e.size.width,e.size.height)}}async function R(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{cmd:{type:"currentMonitor"}}}}).then(T)}async function L(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{cmd:{type:"primaryMonitor"}}}}).then(T)}async function O(){return o({__tauriModule:"Window",message:{cmd:"manage",data:{cmd:{type:"availableMonitors"}}}}).then((e=>e.map(T)))}"__TAURI_METADATA__"in window?W=new z(window.__TAURI_METADATA__.__currentWindow.label,{skip:!0}):(console.warn('Could not find "window.__TAURI_METADATA__". The "appWindow" value will reference the "main" window label.\nNote that this is not an issue if running this frontend on a browser instead of a Tauri window.'),W=new z("main",{skip:!0}));var C=function(){function a(e){this.isOpen=!1,this.encoding=e.encoding||"utf-8",this.options={path:e.path,baudRate:e.baudRate,dataBits:e.dataBits||8,flowControl:e.flowControl||null,parity:e.parity||null,stopBits:e.stopBits||2,timeout:e.timeout||200},this.size=e.size||1024}return a.available_ports=function(){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,s("plugin:serialport|available_ports")];case 1:return[2,t.sent()];case 2:return e=t.sent(),[2,Promise.reject(e)];case 3:return[2]}}))}))},a.forceClose=function(a){return e(this,void 0,void 0,(function(){return t(this,(function(e){switch(e.label){case 0:return[4,s("plugin:serialport|force_close",{path:a})];case 1:return[2,e.sent()]}}))}))},a.closeAll=function(){return e(this,void 0,void 0,(function(){return t(this,(function(e){switch(e.label){case 0:return[4,s("plugin:serialport|close_all")];case 1:return[2,e.sent()]}}))}))},a.prototype.cancelListen=function(){return e(this,void 0,void 0,(function(){return t(this,(function(e){try{return this.unListen&&(this.unListen(),this.unListen=void 0),[2,Promise.resolve(!0)]}catch(e){return[2,Promise.reject("Failed to stop listening to the serial port: "+e)]}return[2]}))}))},a.prototype.cancelRead=function(){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,s("plugin:serialport|cancel_read",{path:this.options.path})];case 1:return t.sent(),[2,Promise.resolve(!0)];case 2:return e=t.sent(),[2,Promise.reject(e)];case 3:return[2]}}))}))},a.prototype.change=function(a){return e(this,void 0,void 0,(function(){var e,n;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,5,,6]),e=!1,this.isOpen?(e=!0,[4,this.close()]):[3,2];case 1:t.sent(),t.label=2;case 2:return a.path&&(this.options.path=a.path),a.baudRate&&(this.options.baudRate=a.baudRate),e?[4,this.open()]:[3,4];case 3:t.sent(),t.label=4;case 4:return[2,Promise.resolve()];case 5:return n=t.sent(),[2,Promise.reject(n)];case 6:return[2]}}))}))},a.prototype.close=function(){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,4,,5]),this.isOpen?[4,this.cancelRead()]:[2,Promise.resolve(!0)];case 1:return t.sent(),[4,s("plugin:serialport|close",{path:this.options.path})];case 2:return t.sent(),[4,this.cancelListen()];case 3:return t.sent(),this.isOpen=!1,[2,Promise.resolve(!0)];case 4:return e=t.sent(),[2,Promise.reject(e)];case 5:return[2]}}))}))},a.prototype.listen=function(a,n){return void 0===n&&(n=!0),e(this,void 0,void 0,(function(){var e,i,s,r=this;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,3,,4]),[4,this.cancelListen()];case 1:return t.sent(),e="plugin-serialport-read-"+this.options.path,i=this,[4,W.listen(e,(function(e){var t=e.payload;try{if(n){var i=new TextDecoder(r.encoding).decode(new Uint8Array(t.data));a(i)}else a(new Uint8Array(t.data))}catch(e){console.error(e)}}))];case 2:return i.unListen=t.sent(),[2,Promise.resolve(!0)];case 3:return s=t.sent(),[2,Promise.reject("Failed to listen to the serial port: "+s)];case 4:return[2]}}))}))},a.prototype.open=function(){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),this.options.path?this.options.baudRate?this.isOpen?[2,Promise.resolve(!0)]:[4,s("plugin:serialport|open",{path:this.options.path,baudRate:this.options.baudRate,dataBits:this.options.dataBits,flowControl:this.options.flowControl,parity:this.options.parity,stopBits:this.options.stopBits,timeout:this.options.timeout})]:[2,Promise.reject("baudRate cannot be empty!")]:[2,Promise.reject("path cannot be empty!")];case 1:return t.sent(),this.isOpen=!0,[2,Promise.resolve(!0)];case 2:return e=t.sent(),[2,Promise.reject(e)];case 3:return[2]}}))}))},a.prototype.read=function(a){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,s("plugin:serialport|read",{path:this.options.path,timeout:(null==a?void 0:a.timeout)||this.options.timeout,size:(null==a?void 0:a.size)||this.size})];case 1:return t.sent(),[2,Promise.resolve(!0)];case 2:return e=t.sent(),[2,Promise.reject(e)];case 3:return[2]}}))}))},a.prototype.setBaudRate=function(a){return e(this,void 0,void 0,(function(){var e,n;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,5,,6]),e=!1,this.isOpen?(e=!0,[4,this.close()]):[3,2];case 1:t.sent(),t.label=2;case 2:return this.options.baudRate=a,e?[4,this.open()]:[3,4];case 3:t.sent(),t.label=4;case 4:return[2,Promise.resolve()];case 5:return n=t.sent(),[2,Promise.reject(n)];case 6:return[2]}}))}))},a.prototype.setPath=function(a){return e(this,void 0,void 0,(function(){var e,n;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,5,,6]),e=!1,this.isOpen?(e=!0,[4,this.close()]):[3,2];case 1:t.sent(),t.label=2;case 2:return this.options.path=a,e?[4,this.open()]:[3,4];case 3:t.sent(),t.label=4;case 4:return[2,Promise.resolve()];case 5:return n=t.sent(),[2,Promise.reject(n)];case 6:return[2]}}))}))},a.prototype.write=function(a){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),this.isOpen?[4,s("plugin:serialport|write",{value:a,path:this.options.path})]:[2,Promise.reject("Port ".concat(this.options.path," is not open!"))];case 1:return[2,t.sent()];case 2:return e=t.sent(),[2,Promise.reject(e)];case 3:return[2]}}))}))},a.prototype.writeBinary=function(a){return e(this,void 0,void 0,(function(){var e;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,4,,5]),this.isOpen?a instanceof Uint8Array||a instanceof Array?[4,s("plugin:serialport|write_binary",{value:Array.from(a),path:this.options.path})]:[3,2]:[2,Promise.reject("Port ".concat(this.options.path," is not open!"))];case 1:return[2,t.sent()];case 2:return[2,Promise.reject("value Parameter must be of type string, Uint8Array, or number[]!")];case 3:return[3,5];case 4:return e=t.sent(),[2,Promise.reject(e)];case 5:return[2]}}))}))},a}();export{C as Serialport}; diff --git a/webview-src/index.ts b/webview-src/index.ts index fdd5a8a..fe414a8 100644 --- a/webview-src/index.ts +++ b/webview-src/index.ts @@ -103,16 +103,16 @@ class Serialport { } /** - * @description: Stop listening to serial port - * @return {Promise} + * @description: Stop listening to the serial port + * @return {Promise} */ - async cancelListen(): Promise { + async cancelListen(): Promise { try { if (this.unListen) { this.unListen(); this.unListen = undefined; } - return; + return Promise.resolve(true); } catch (error) { return Promise.reject('Failed to stop listening to the serial port: ' + error); } @@ -120,13 +120,14 @@ class Serialport { /** * @description: Cancel read serial port data - * @return {Promise} + * @return {Promise} */ - async cancelRead(): Promise { + async cancelRead(): Promise { try { - return await invoke('plugin:serialport|cancel_read', { + await invoke('plugin:serialport|cancel_read', { path: this.options.path, }); + return Promise.resolve(true); } catch (error) { return Promise.reject(error); } @@ -161,32 +162,31 @@ class Serialport { /** * @description: Close serial port - * @return {Promise} + * @return {Promise} */ - async close(): Promise { + async close(): Promise { try { if (!this.isOpen) { - return; + return Promise.resolve(true); } await this.cancelRead(); - const res = await invoke('plugin:serialport|close', { + await invoke('plugin:serialport|close', { path: this.options.path, }); - await this.cancelListen(); this.isOpen = false; - return res; + return Promise.resolve(true); } catch (error) { return Promise.reject(error); } } /** - * @description: Listen to serial port data + * @description: Register a listener to receive data read from the serial port * @param {function} fn - * @return {Promise} + * @return {Promise} */ - async listen(fn: (...args: any[]) => void, isDecode = true): Promise { + async listen(fn: (...args: any[]) => void, isDecode = true): Promise { try { await this.cancelListen(); let readEvent = 'plugin-serialport-read-' + this.options.path; @@ -206,7 +206,7 @@ class Serialport { } }, ); - return; + return Promise.resolve(true); } catch (error) { return Promise.reject('Failed to listen to the serial port: ' + error); } @@ -214,9 +214,9 @@ class Serialport { /** * @description: Open serial port - * @return {*} + * @return {Promise} */ - async open(): Promise { + async open(): Promise { try { if (!this.options.path) { return Promise.reject(`path cannot be empty!`); @@ -225,7 +225,7 @@ class Serialport { return Promise.reject(`baudRate cannot be empty!`); } if (this.isOpen) { - return; + return Promise.resolve(true); } const res = await invoke('plugin:serialport|open', { path: this.options.path, @@ -237,24 +237,27 @@ class Serialport { timeout: this.options.timeout, }); this.isOpen = true; - return Promise.resolve(res); + return Promise.resolve(true); } catch (error) { return Promise.reject(error); } } + /** - * @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} + * @return {Promise} */ - async read(options?: ReadOptions): Promise { + async read(options?: ReadOptions): Promise { try { - return await invoke('plugin:serialport|read', { + await invoke('plugin:serialport|read', { path: this.options.path, timeout: options?.timeout || this.options.timeout, size: options?.size || this.size, }); + return Promise.resolve(true); } catch (error) { return Promise.reject(error); }