import { listen } from "./chunk-R4EXQSPT.js"; import { invoke } from "./chunk-DSY64NDS.js"; import "./chunk-DC5AMYBS.js"; // node_modules/.pnpm/tauri-plugin-serialplugin@2.12.1/node_modules/tauri-plugin-serialplugin/dist-js/index.js var DataBits; (function(DataBits2) { DataBits2["Five"] = "Five"; DataBits2["Six"] = "Six"; DataBits2["Seven"] = "Seven"; DataBits2["Eight"] = "Eight"; })(DataBits || (DataBits = {})); var FlowControl; (function(FlowControl2) { FlowControl2["None"] = "None"; FlowControl2["Software"] = "Software"; FlowControl2["Hardware"] = "Hardware"; })(FlowControl || (FlowControl = {})); var Parity; (function(Parity2) { Parity2["None"] = "None"; Parity2["Odd"] = "Odd"; Parity2["Even"] = "Even"; })(Parity || (Parity = {})); var StopBits; (function(StopBits2) { StopBits2["One"] = "One"; StopBits2["Two"] = "Two"; })(StopBits || (StopBits = {})); var ClearBuffer; (function(ClearBuffer2) { ClearBuffer2["Input"] = "Input"; ClearBuffer2["Output"] = "Output"; ClearBuffer2["All"] = "All"; })(ClearBuffer || (ClearBuffer = {})); var SerialPort = class { constructor(options) { this.isOpen = false; this.encoding = options.encoding || "utf-8"; this.options = { path: options.path, baudRate: options.baudRate, dataBits: options.dataBits || DataBits.Eight, flowControl: options.flowControl || FlowControl.None, parity: options.parity || Parity.None, stopBits: options.stopBits || StopBits.One, size: options.size || 1024, timeout: options.timeout || 200 }; this.size = options.size || 1024; } /** * @description Lists all available serial ports * @returns {Promise<{ [key: string]: PortInfo }>} A promise that resolves to a map of port names to port information */ static async available_ports() { try { const result = await invoke("plugin:serialplugin|available_ports"); return Promise.resolve(result); } catch (error) { return Promise.reject(error); } } /** * @description Lists all available serial ports using platform-specific commands * @returns {Promise<{ [key: string]: PortInfo }>} A promise that resolves to a map of port names to port information */ static async available_ports_direct() { try { const result = await invoke("plugin:serialplugin|available_ports_direct"); return Promise.resolve(result); } catch (error) { return Promise.reject(error); } } /** * @description Lists all managed serial ports (ports that are currently open and managed by the application). * @returns {Promise} A promise that resolves to an array of port paths (names). */ static async managed_ports() { try { const result = await invoke("plugin:serialplugin|managed_ports"); return Promise.resolve(result); } catch (error) { return Promise.reject(error); } } /** * @description Forcefully closes a specific serial port * @param {string} path The path of the serial port to close * @returns {Promise} A promise that resolves when the port is closed */ static async forceClose(path) { return await invoke("plugin:serialplugin|force_close", { path }); } /** * @description Closes all open serial ports * @returns {Promise} A promise that resolves when all ports are closed */ static async closeAll() { return await invoke("plugin:serialplugin|close_all"); } /** * @description Cancels monitoring of the serial port * @returns {Promise} A promise that resolves when monitoring is cancelled */ async cancelListen() { try { if (this.unListen) { this.unListen(); this.unListen = void 0; } return; } catch (error) { return Promise.reject("Failed to cancel serial monitoring: " + error); } } /** * @description Cancels reading data from the serial port * @returns {Promise} A promise that resolves when reading is cancelled */ async cancelRead() { try { await invoke("plugin:serialplugin|cancel_read", { path: this.options.path }); } catch (error) { return Promise.reject(error instanceof Error ? error : new Error(String(error))); } } /** * @description Changes the serial port configuration * @param {object} options Configuration options * @param {string} [options.path] New port path * @param {number} [options.baudRate] New baud rate * @returns {Promise} A promise that resolves when configuration is changed */ async change(options) { try { let isOpened = false; if (this.isOpen) { isOpened = true; await this.close(); } if (options.path) { this.options.path = options.path; } if (options.baudRate) { this.options.baudRate = options.baudRate; } if (isOpened) { await this.open(); } return Promise.resolve(); } catch (error) { return Promise.reject(error); } } /** * @description Closes the currently open serial port * @returns {Promise} A promise that resolves when the port is closed */ async close() { try { if (!this.isOpen) { return; } await this.cancelRead(); let res = void 0; res = await invoke("plugin:serialplugin|close", { path: this.options.path }); await this.cancelListen(); this.isOpen = false; return res; } catch (error) { return Promise.reject(error); } } /** * @description Sets up a listener for port disconnection events * @param {Function} fn Callback function to handle disconnection * @returns {Promise} A promise that resolves when the listener is set up */ async disconnected(fn) { var _a; let sub_path = (_a = this.options.path) == null ? void 0 : _a.toString().replaceAll(".", "-").replaceAll("/", "-"); let checkEvent = `plugin-serialplugin-disconnected-${sub_path}`; console.log("listen event: " + checkEvent); let unListen = await listen(checkEvent, () => { try { fn(); unListen(); unListen = void 0; } catch (error) { console.error(error); } }); } /** * @description Monitors serial port data * @param {Function} fn Callback function to handle received data * @param {boolean} [isDecode=true] Whether to decode the received data * @returns {Promise} A promise that resolves when monitoring starts */ async listen(fn, isDecode = true) { var _a; try { if (!this.isOpen) { return Promise.reject("Port is not open"); } await this.cancelListen(); let sub_path = (_a = this.options.path) == null ? void 0 : _a.toString().replaceAll(".", "-").replaceAll("/", "-"); let readEvent = `plugin-serialplugin-read-${sub_path}`; console.log("listen event: " + readEvent); this.unListen = await listen(readEvent, ({ payload }) => { try { if (isDecode) { const decoder = new TextDecoder(this.encoding); const data = decoder.decode(new Uint8Array(payload.data)); fn(data); } else { fn(new Uint8Array(payload.data)); } } catch (error) { console.error(error); } }); return; } catch (error) { return Promise.reject("Failed to monitor serial port data: " + error); } } /** * @description Opens the serial port with current settings * @returns {Promise} A promise that resolves when the port is opened */ async open() { try { if (!this.options.path) { return Promise.reject(`path Can not be empty!`); } if (!this.options.baudRate) { return Promise.reject(`baudRate Can not be empty!`); } if (this.isOpen) { return; } const res = await invoke("plugin:serialplugin|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 }); this.isOpen = true; this.disconnected(() => { this.isOpen = false; }).catch((err) => console.error(err)); return Promise.resolve(res); } catch (error) { return Promise.reject(error); } } /** * Starts listening for data on the serial port * The port will continuously monitor for incoming data and emit events * @returns {Promise} A promise that resolves when listening starts * @throws {Error} If starting listener fails or port is not open * @example * const port = new SerialPort({ path: '/dev/ttyUSB0' }); * await port.startListening(); * // Listen for data events * port.listen((data) => { * console.log('listen', data) * receivedData += data; * }); */ async startListening() { try { await invoke("plugin:serialplugin|start_listening", { path: this.options.path, size: this.options.size, timeout: this.options.timeout }); } catch (error) { return Promise.reject(error); } } /** * Stops listening for data on the serial port * Cleans up event listeners and monitoring threads * @returns {Promise} A promise that resolves when listening stops * @throws {Error} If stopping listener fails or port is not open * @example * await port.stopListening(); */ async stopListening() { try { await invoke("plugin:serialplugin|stop_listening", { path: this.options.path }); } catch (error) { return Promise.reject(error); } } /** * @description Reads data from the serial port * @param {ReadOptions} [options] Read options * @returns {Promise} A promise that resolves when data is read */ async read(options) { try { if (!this.isOpen) { return Promise.reject("Port is not open"); } return await invoke("plugin:serialplugin|read", { path: this.options.path, timeout: (options == null ? void 0 : options.timeout) || this.options.timeout, size: (options == null ? void 0 : options.size) || this.size }); } catch (error) { return Promise.reject(error); } } /** * @description Reads binary data from the serial port * @param {ReadOptions} [options] Read options * @returns {Promise} A promise that resolves with binary data */ async readBinary(options) { try { const result = await invoke("plugin:serialplugin|read_binary", { path: this.options.path, timeout: (options == null ? void 0 : options.timeout) || this.options.timeout, size: (options == null ? void 0 : options.size) || this.size }); return new Uint8Array(result); } catch (error) { return Promise.reject(error); } } /** * @description Sets the baud rate of the serial port * @param {number} value The new baud rate * @returns {Promise} A promise that resolves when baud rate is set */ async setBaudRate(value) { try { return await invoke("plugin:serialplugin|set_baud_rate", { path: this.options.path, baudRate: value }); } catch (error) { return Promise.reject(error); } } /** * @description Sets the data bits configuration * @param {DataBits} value The new data bits setting * @returns {Promise} A promise that resolves when data bits are set */ async setDataBits(value) { try { return await invoke("plugin:serialplugin|set_data_bits", { path: this.options.path, dataBits: value }); } catch (error) { return Promise.reject(error); } } /** * @description Sets the flow control mode * @param {FlowControl} value The new flow control setting * @returns {Promise} A promise that resolves when flow control is set */ async setFlowControl(value) { try { return await invoke("plugin:serialplugin|set_flow_control", { path: this.options.path, flowControl: value }); } catch (error) { return Promise.reject(error); } } /** * @description Sets the parity checking mode * @param {Parity} value The new parity setting * @returns {Promise} A promise that resolves when parity is set */ async setParity(value) { try { return await invoke("plugin:serialplugin|set_parity", { path: this.options.path, parity: value }); } catch (error) { return Promise.reject(error); } } /** * @description Sets the number of stop bits * @param {StopBits} value The new stop bits setting * @returns {Promise} A promise that resolves when stop bits are set */ async setStopBits(value) { try { return await invoke("plugin:serialplugin|set_stop_bits", { path: this.options.path, stopBits: value }); } catch (error) { return Promise.reject(error); } } /** * @description Sets the timeout duration * @param {number} value The new timeout in milliseconds * @returns {Promise} A promise that resolves when timeout is set */ async setTimeout(value) { try { return await invoke("plugin:serialplugin|set_timeout", { path: this.options.path, timeout: value }); } catch (error) { return Promise.reject(error); } } /** * @description Sets the RTS (Request To Send) control signal * @param {boolean} value The signal level to set * @returns {Promise} A promise that resolves when RTS is set */ async setRequestToSend(value) { try { return await invoke("plugin:serialplugin|write_request_to_send", { path: this.options.path, level: value }); } catch (error) { return Promise.reject(error); } } /** * @description Sets the DTR (Data Terminal Ready) control signal * @param {boolean} value The signal level to set * @returns {Promise} A promise that resolves when DTR is set */ async setDataTerminalReady(value) { try { return await invoke("plugin:serialplugin|write_data_terminal_ready", { path: this.options.path, level: value }); } catch (error) { return Promise.reject(error); } } /** * @description Reads the CTS (Clear To Send) control signal state * @returns {Promise} A promise that resolves to the CTS state */ async readClearToSend() { try { return await invoke("plugin:serialplugin|read_clear_to_send", { path: this.options.path }); } catch (error) { return Promise.reject(error); } } /** * @description Reads the DSR (Data Set Ready) control signal state * @returns {Promise} A promise that resolves to the DSR state */ async readDataSetReady() { try { return await invoke("plugin:serialplugin|read_data_set_ready", { path: this.options.path }); } catch (error) { return Promise.reject(error); } } /** * @description Reads the RI (Ring Indicator) control signal state * @returns {Promise} A promise that resolves to the RI state */ async readRingIndicator() { try { return await invoke("plugin:serialplugin|read_ring_indicator", { path: this.options.path }); } catch (error) { return Promise.reject(error); } } /** * @description Reads the CD (Carrier Detect) control signal state * @returns {Promise} A promise that resolves to the CD state */ async readCarrierDetect() { try { return await invoke("plugin:serialplugin|read_carrier_detect", { path: this.options.path }); } catch (error) { return Promise.reject(error); } } /** * @description Gets the number of bytes available to read * @returns {Promise} A promise that resolves to the number of bytes */ async bytesToRead() { try { return await invoke("plugin:serialplugin|bytes_to_read", { path: this.options.path }); } catch (error) { return Promise.reject(error); } } /** * @description Gets the number of bytes waiting to be written * @returns {Promise} A promise that resolves to the number of bytes */ async bytesToWrite() { try { return await invoke("plugin:serialplugin|bytes_to_write", { path: this.options.path }); } catch (error) { return Promise.reject(error); } } /** * @description Clears the specified buffer * @param {ClearBuffer} buffer The buffer to clear * @returns {Promise} A promise that resolves when the buffer is cleared */ async clearBuffer(buffer) { try { return await invoke("plugin:serialplugin|clear_buffer", { path: this.options.path, bufferType: buffer }); } catch (error) { return Promise.reject(error); } } /** * @description Starts transmitting a break signal * @returns {Promise} A promise that resolves when break signal starts */ async setBreak() { try { return await invoke("plugin:serialplugin|set_break", { path: this.options.path }); } catch (error) { return Promise.reject(error); } } /** * @description Stops transmitting a break signal * @returns {Promise} A promise that resolves when break signal stops */ async clearBreak() { try { return await invoke("plugin:serialplugin|clear_break", { path: this.options.path }); } catch (error) { return Promise.reject(error); } } /** * @description Writes string data to the serial port * @param {string} value The data to write * @returns {Promise} A promise that resolves to the number of bytes written */ async write(value) { try { if (!this.isOpen) { return Promise.reject(`serial port ${this.options.path} not opened!`); } return await invoke("plugin:serialplugin|write", { value, path: this.options.path }); } catch (error) { return Promise.reject(error); } } /** * @description Writes binary data to the serial port * @param {Uint8Array | number[]} value The binary data to write * @returns {Promise} A promise that resolves to the number of bytes written */ async writeBinary(value) { try { if (!this.isOpen) { return Promise.reject(`serial port ${this.options.path} not opened!`); } if (value instanceof Uint8Array || value instanceof Array) { return await invoke("plugin:serialplugin|write_binary", { value: Array.from(value), path: this.options.path }); } else { return Promise.reject("value Argument type error! Expected type: string, Uint8Array, number[]"); } } catch (error) { return Promise.reject(error); } } }; export { ClearBuffer, DataBits, FlowControl, Parity, SerialPort, StopBits }; //# sourceMappingURL=tauri-plugin-serialplugin.js.map