tauri-plugin-serialplugin-api 2.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.spdx +20 -0
- package/README.md +1678 -0
- package/dist-js/auto-reconnect-manager.d.ts +69 -0
- package/dist-js/index.cjs +1114 -0
- package/dist-js/index.d.ts +372 -0
- package/dist-js/index.js +1112 -0
- package/dist-js/listener-manager.d.ts +34 -0
- package/package.json +55 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import { UnlistenFn } from '@tauri-apps/api/event';
|
|
2
|
+
export interface PortInfo {
|
|
3
|
+
path: "Unknown" | string;
|
|
4
|
+
manufacturer: "Unknown" | string;
|
|
5
|
+
pid: "Unknown" | string;
|
|
6
|
+
product: "Unknown" | string;
|
|
7
|
+
serial_number: "Unknown" | string;
|
|
8
|
+
type: "PCI" | string;
|
|
9
|
+
vid: "Unknown" | string;
|
|
10
|
+
}
|
|
11
|
+
export interface ReadDataResult {
|
|
12
|
+
size: number;
|
|
13
|
+
data: number[];
|
|
14
|
+
}
|
|
15
|
+
export interface SerialportOptions {
|
|
16
|
+
path: string;
|
|
17
|
+
baudRate: number;
|
|
18
|
+
encoding?: string;
|
|
19
|
+
dataBits?: DataBits;
|
|
20
|
+
flowControl?: FlowControl;
|
|
21
|
+
parity?: Parity;
|
|
22
|
+
stopBits?: StopBits;
|
|
23
|
+
timeout?: number;
|
|
24
|
+
size?: number;
|
|
25
|
+
[key: string]: any;
|
|
26
|
+
}
|
|
27
|
+
export interface Options {
|
|
28
|
+
path?: string;
|
|
29
|
+
baudRate?: number;
|
|
30
|
+
dataBits: DataBits;
|
|
31
|
+
flowControl: FlowControl;
|
|
32
|
+
parity: Parity;
|
|
33
|
+
stopBits: StopBits;
|
|
34
|
+
size?: number;
|
|
35
|
+
timeout: number;
|
|
36
|
+
[key: string]: any;
|
|
37
|
+
}
|
|
38
|
+
export interface ReadOptions {
|
|
39
|
+
timeout?: number;
|
|
40
|
+
size?: number;
|
|
41
|
+
}
|
|
42
|
+
export declare enum DataBits {
|
|
43
|
+
Five = "Five",
|
|
44
|
+
Six = "Six",
|
|
45
|
+
Seven = "Seven",
|
|
46
|
+
Eight = "Eight"
|
|
47
|
+
}
|
|
48
|
+
export declare enum FlowControl {
|
|
49
|
+
None = "None",
|
|
50
|
+
Software = "Software",
|
|
51
|
+
Hardware = "Hardware"
|
|
52
|
+
}
|
|
53
|
+
export declare enum Parity {
|
|
54
|
+
None = "None",
|
|
55
|
+
Odd = "Odd",
|
|
56
|
+
Even = "Even"
|
|
57
|
+
}
|
|
58
|
+
export declare enum StopBits {
|
|
59
|
+
One = "One",
|
|
60
|
+
Two = "Two"
|
|
61
|
+
}
|
|
62
|
+
export declare enum ClearBuffer {
|
|
63
|
+
Input = "Input",
|
|
64
|
+
Output = "Output",
|
|
65
|
+
All = "All"
|
|
66
|
+
}
|
|
67
|
+
export type SerialPortConfig = {
|
|
68
|
+
path: string;
|
|
69
|
+
baudRate: number;
|
|
70
|
+
dataBits?: DataBits;
|
|
71
|
+
flowControl?: FlowControl;
|
|
72
|
+
parity?: Parity;
|
|
73
|
+
stopBits?: StopBits;
|
|
74
|
+
timeout?: number;
|
|
75
|
+
size?: number;
|
|
76
|
+
encoding?: string;
|
|
77
|
+
};
|
|
78
|
+
export type BaudRate = 110 | 300 | 600 | 1200 | 2400 | 4800 | 9600 | 14400 | 19200 | 38400 | 57600 | 115200 | 230400 | 460800 | 921600;
|
|
79
|
+
declare class SerialPort {
|
|
80
|
+
isOpen: boolean;
|
|
81
|
+
encoding: string;
|
|
82
|
+
options: Options;
|
|
83
|
+
size: number;
|
|
84
|
+
private listeners;
|
|
85
|
+
private autoReconnectManager;
|
|
86
|
+
constructor(options: SerialportOptions);
|
|
87
|
+
/**
|
|
88
|
+
* @description Lists all available serial ports
|
|
89
|
+
* @returns {Promise<{ [key: string]: PortInfo }>} A promise that resolves to a map of port names to port information
|
|
90
|
+
*/
|
|
91
|
+
static available_ports(): Promise<{
|
|
92
|
+
[key: string]: PortInfo;
|
|
93
|
+
}>;
|
|
94
|
+
/**
|
|
95
|
+
* @description Lists all available serial ports using platform-specific commands
|
|
96
|
+
* @returns {Promise<{ [key: string]: PortInfo }>} A promise that resolves to a map of port names to port information
|
|
97
|
+
*/
|
|
98
|
+
static available_ports_direct(): Promise<{
|
|
99
|
+
[key: string]: PortInfo;
|
|
100
|
+
}>;
|
|
101
|
+
/**
|
|
102
|
+
* @description Lists all managed serial ports (ports that are currently open and managed by the application).
|
|
103
|
+
* @returns {Promise<string[]>} A promise that resolves to an array of port paths (names).
|
|
104
|
+
*/
|
|
105
|
+
static managed_ports(): Promise<string[]>;
|
|
106
|
+
/**
|
|
107
|
+
* @description Forcefully closes a specific serial port
|
|
108
|
+
* @param {string} path The path of the serial port to close
|
|
109
|
+
* @returns {Promise<void>} A promise that resolves when the port is closed
|
|
110
|
+
*/
|
|
111
|
+
static forceClose(path: string): Promise<void>;
|
|
112
|
+
/**
|
|
113
|
+
* @description Closes all open serial ports
|
|
114
|
+
* @returns {Promise<void>} A promise that resolves when all ports are closed
|
|
115
|
+
*/
|
|
116
|
+
static closeAll(): Promise<void>;
|
|
117
|
+
/**
|
|
118
|
+
* @description Gets the full serial number for a specific port
|
|
119
|
+
* On Windows, this can fix truncated serial numbers (e.g., "B4" → "B4:3D:4D:B1:5A:C8" for ESP32 devices)
|
|
120
|
+
* @param {string} path The port path (e.g., "COM3", "/dev/ttyUSB0")
|
|
121
|
+
* @returns {Promise<string>} A promise that resolves to the full serial number, or "Unknown" if not found
|
|
122
|
+
*/
|
|
123
|
+
static get_full_serial_number(path: string): Promise<string>;
|
|
124
|
+
/**
|
|
125
|
+
* @description Cancels listening for serial port data (does not affect disconnect listeners)
|
|
126
|
+
* @returns {Promise<void>} A promise that resolves when listening is cancelled
|
|
127
|
+
*/
|
|
128
|
+
cancelListen(): Promise<void>;
|
|
129
|
+
/**
|
|
130
|
+
* @description Cancels all listeners (both data and disconnect listeners)
|
|
131
|
+
* @returns {Promise<void>} A promise that resolves when all listeners are cancelled
|
|
132
|
+
*/
|
|
133
|
+
cancelAllListeners(): Promise<void>;
|
|
134
|
+
/**
|
|
135
|
+
* @description Gets information about active listeners (for debugging)
|
|
136
|
+
* @returns {Object} Information about active listeners
|
|
137
|
+
*/
|
|
138
|
+
getListenersInfo(): {
|
|
139
|
+
total: number;
|
|
140
|
+
data: number;
|
|
141
|
+
disconnect: number;
|
|
142
|
+
ids: string[];
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* @description Cancels reading data from the serial port
|
|
146
|
+
* @returns {Promise<void>} A promise that resolves when reading is cancelled
|
|
147
|
+
*/
|
|
148
|
+
private cancelRead;
|
|
149
|
+
/**
|
|
150
|
+
* @description Changes the serial port configuration
|
|
151
|
+
* @param {object} options Configuration options
|
|
152
|
+
* @param {string} [options.path] New port path
|
|
153
|
+
* @param {number} [options.baudRate] New baud rate
|
|
154
|
+
* @returns {Promise<void>} A promise that resolves when configuration is changed
|
|
155
|
+
*/
|
|
156
|
+
change(options: {
|
|
157
|
+
path?: string;
|
|
158
|
+
baudRate?: number;
|
|
159
|
+
}): Promise<void>;
|
|
160
|
+
/**
|
|
161
|
+
* @description Closes the currently open serial port
|
|
162
|
+
* @returns {Promise<void>} A promise that resolves when the port is closed
|
|
163
|
+
*/
|
|
164
|
+
close(): Promise<void>;
|
|
165
|
+
/**
|
|
166
|
+
* @description Sets up a listener for port disconnection events
|
|
167
|
+
* @param {Function} fn Callback function to handle disconnection
|
|
168
|
+
* @returns {Promise<void>} A promise that resolves when the listener is set up
|
|
169
|
+
*/
|
|
170
|
+
disconnected(fn: (...args: any[]) => void): Promise<void>;
|
|
171
|
+
/**
|
|
172
|
+
* @description Enables auto-reconnect functionality
|
|
173
|
+
* @param {Object} options Auto-reconnect configuration options
|
|
174
|
+
* @param {number} [options.interval=5000] Reconnection interval in milliseconds
|
|
175
|
+
* @param {number | null} [options.maxAttempts=10] Maximum number of reconnection attempts (null for infinite)
|
|
176
|
+
* @param {Function} [options.onReconnect] Callback function called on each reconnection attempt
|
|
177
|
+
* @returns {Promise<void>} A promise that resolves when auto-reconnect is enabled
|
|
178
|
+
*/
|
|
179
|
+
enableAutoReconnect(options?: {
|
|
180
|
+
interval?: number;
|
|
181
|
+
maxAttempts?: number | null;
|
|
182
|
+
onReconnect?: (success: boolean, attempt: number) => void;
|
|
183
|
+
}): Promise<void>;
|
|
184
|
+
/**
|
|
185
|
+
* @description Disables auto-reconnect functionality
|
|
186
|
+
* @returns {Promise<void>} A promise that resolves when auto-reconnect is disabled
|
|
187
|
+
*/
|
|
188
|
+
disableAutoReconnect(): Promise<void>;
|
|
189
|
+
/**
|
|
190
|
+
* @description Gets auto-reconnect status and configuration
|
|
191
|
+
* @returns {Object} Auto-reconnect information
|
|
192
|
+
*/
|
|
193
|
+
getAutoReconnectInfo(): {
|
|
194
|
+
enabled: boolean;
|
|
195
|
+
interval: number;
|
|
196
|
+
maxAttempts: number | null;
|
|
197
|
+
currentAttempts: number;
|
|
198
|
+
hasCallback: boolean;
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* @description Manually triggers a reconnection attempt
|
|
202
|
+
* @returns {Promise<boolean>} A promise that resolves to true if reconnection was successful
|
|
203
|
+
*/
|
|
204
|
+
manualReconnect(): Promise<boolean>;
|
|
205
|
+
/**
|
|
206
|
+
* @description Monitors serial port data
|
|
207
|
+
* @param {Function} fn Callback function to handle received data
|
|
208
|
+
* @param {boolean} [isDecode=true] Whether to decode the received data
|
|
209
|
+
* @returns {Promise<UnlistenFn>} A promise that resolves to an unlisten function
|
|
210
|
+
*/
|
|
211
|
+
listen(fn: (...args: any[]) => void, isDecode?: boolean): Promise<UnlistenFn>;
|
|
212
|
+
/**
|
|
213
|
+
* @description Opens the serial port with current settings
|
|
214
|
+
* @returns {Promise<void>} A promise that resolves when the port is opened
|
|
215
|
+
*/
|
|
216
|
+
open(): Promise<void>;
|
|
217
|
+
/**
|
|
218
|
+
* Starts listening for data on the serial port
|
|
219
|
+
* The port will continuously monitor for incoming data and emit events
|
|
220
|
+
* @returns {Promise<void>} A promise that resolves when listening starts
|
|
221
|
+
* @throws {Error} If starting listener fails or port is not open
|
|
222
|
+
* @example
|
|
223
|
+
* const port = new SerialPort({ path: '/dev/ttyUSB0' });
|
|
224
|
+
* await port.startListening();
|
|
225
|
+
* // Listen for data events
|
|
226
|
+
* port.listen((data) => {
|
|
227
|
+
* console.log('listen', data)
|
|
228
|
+
* receivedData += data;
|
|
229
|
+
* });
|
|
230
|
+
*/
|
|
231
|
+
startListening(): Promise<void>;
|
|
232
|
+
/**
|
|
233
|
+
* Stops listening for data on the serial port
|
|
234
|
+
* Cleans up event listeners and monitoring threads
|
|
235
|
+
* @returns {Promise<void>} A promise that resolves when listening stops
|
|
236
|
+
* @throws {Error} If stopping listener fails or port is not open
|
|
237
|
+
* @example
|
|
238
|
+
* await port.stopListening();
|
|
239
|
+
*/
|
|
240
|
+
stopListening(): Promise<void>;
|
|
241
|
+
/**
|
|
242
|
+
* @description Reads data from the serial port
|
|
243
|
+
* @param {ReadOptions} [options] Read options
|
|
244
|
+
* @returns {Promise<void>} A promise that resolves when data is read
|
|
245
|
+
*/
|
|
246
|
+
read(options?: ReadOptions): Promise<string>;
|
|
247
|
+
/**
|
|
248
|
+
* @description Reads binary data from the serial port
|
|
249
|
+
* @param {ReadOptions} [options] Read options
|
|
250
|
+
* @returns {Promise<Uint8Array>} A promise that resolves with binary data
|
|
251
|
+
*/
|
|
252
|
+
readBinary(options?: ReadOptions): Promise<Uint8Array>;
|
|
253
|
+
/**
|
|
254
|
+
* @description Sets the baud rate of the serial port
|
|
255
|
+
* @param {number} value The new baud rate
|
|
256
|
+
* @returns {Promise<void>} A promise that resolves when baud rate is set
|
|
257
|
+
*/
|
|
258
|
+
setBaudRate(value: number | BaudRate): Promise<void>;
|
|
259
|
+
/**
|
|
260
|
+
* @description Sets the data bits configuration
|
|
261
|
+
* @param {DataBits} value The new data bits setting
|
|
262
|
+
* @returns {Promise<void>} A promise that resolves when data bits are set
|
|
263
|
+
*/
|
|
264
|
+
setDataBits(value: DataBits): Promise<void>;
|
|
265
|
+
/**
|
|
266
|
+
* @description Sets the flow control mode
|
|
267
|
+
* @param {FlowControl} value The new flow control setting
|
|
268
|
+
* @returns {Promise<void>} A promise that resolves when flow control is set
|
|
269
|
+
*/
|
|
270
|
+
setFlowControl(value: FlowControl): Promise<void>;
|
|
271
|
+
/**
|
|
272
|
+
* @description Sets the parity checking mode
|
|
273
|
+
* @param {Parity} value The new parity setting
|
|
274
|
+
* @returns {Promise<void>} A promise that resolves when parity is set
|
|
275
|
+
*/
|
|
276
|
+
setParity(value: Parity): Promise<void>;
|
|
277
|
+
/**
|
|
278
|
+
* @description Sets the number of stop bits
|
|
279
|
+
* @param {StopBits} value The new stop bits setting
|
|
280
|
+
* @returns {Promise<void>} A promise that resolves when stop bits are set
|
|
281
|
+
*/
|
|
282
|
+
setStopBits(value: StopBits): Promise<void>;
|
|
283
|
+
/**
|
|
284
|
+
* @description Sets the timeout duration
|
|
285
|
+
* @param {number} value The new timeout in milliseconds
|
|
286
|
+
* @returns {Promise<void>} A promise that resolves when timeout is set
|
|
287
|
+
*/
|
|
288
|
+
setTimeout(value: number): Promise<void>;
|
|
289
|
+
/**
|
|
290
|
+
* @description Sets the RTS (Request To Send) control signal
|
|
291
|
+
* @param {boolean} value The signal level to set
|
|
292
|
+
* @returns {Promise<void>} A promise that resolves when RTS is set
|
|
293
|
+
*/
|
|
294
|
+
setRequestToSend(value: boolean): Promise<void>;
|
|
295
|
+
/**
|
|
296
|
+
* @description Sets the DTR (Data Terminal Ready) control signal
|
|
297
|
+
* @param {boolean} value The signal level to set
|
|
298
|
+
* @returns {Promise<void>} A promise that resolves when DTR is set
|
|
299
|
+
*/
|
|
300
|
+
setDataTerminalReady(value: boolean): Promise<void>;
|
|
301
|
+
/**
|
|
302
|
+
* @description Writes the RTS (Request To Send) control signal
|
|
303
|
+
* @param {boolean} level The signal level to set
|
|
304
|
+
* @returns {Promise<void>} A promise that resolves when RTS is set
|
|
305
|
+
*/
|
|
306
|
+
writeRequestToSend(level: boolean): Promise<void>;
|
|
307
|
+
/**
|
|
308
|
+
* @description Writes the DTR (Data Terminal Ready) control signal
|
|
309
|
+
* @param {boolean} level The signal level to set
|
|
310
|
+
* @returns {Promise<void>} A promise that resolves when DTR is set
|
|
311
|
+
*/
|
|
312
|
+
writeDataTerminalReady(level: boolean): Promise<void>;
|
|
313
|
+
/**
|
|
314
|
+
* @description Reads the CTS (Clear To Send) control signal state
|
|
315
|
+
* @returns {Promise<boolean>} A promise that resolves to the CTS state
|
|
316
|
+
*/
|
|
317
|
+
readClearToSend(): Promise<boolean>;
|
|
318
|
+
/**
|
|
319
|
+
* @description Reads the DSR (Data Set Ready) control signal state
|
|
320
|
+
* @returns {Promise<boolean>} A promise that resolves to the DSR state
|
|
321
|
+
*/
|
|
322
|
+
readDataSetReady(): Promise<boolean>;
|
|
323
|
+
/**
|
|
324
|
+
* @description Reads the RI (Ring Indicator) control signal state
|
|
325
|
+
* @returns {Promise<boolean>} A promise that resolves to the RI state
|
|
326
|
+
*/
|
|
327
|
+
readRingIndicator(): Promise<boolean>;
|
|
328
|
+
/**
|
|
329
|
+
* @description Reads the CD (Carrier Detect) control signal state
|
|
330
|
+
* @returns {Promise<boolean>} A promise that resolves to the CD state
|
|
331
|
+
*/
|
|
332
|
+
readCarrierDetect(): Promise<boolean>;
|
|
333
|
+
/**
|
|
334
|
+
* @description Gets the number of bytes available to read
|
|
335
|
+
* @returns {Promise<number>} A promise that resolves to the number of bytes
|
|
336
|
+
*/
|
|
337
|
+
bytesToRead(): Promise<number>;
|
|
338
|
+
/**
|
|
339
|
+
* @description Gets the number of bytes waiting to be written
|
|
340
|
+
* @returns {Promise<number>} A promise that resolves to the number of bytes
|
|
341
|
+
*/
|
|
342
|
+
bytesToWrite(): Promise<number>;
|
|
343
|
+
/**
|
|
344
|
+
* @description Clears the specified buffer
|
|
345
|
+
* @param {ClearBuffer} buffer The buffer to clear
|
|
346
|
+
* @returns {Promise<void>} A promise that resolves when the buffer is cleared
|
|
347
|
+
*/
|
|
348
|
+
clearBuffer(buffer: ClearBuffer): Promise<void>;
|
|
349
|
+
/**
|
|
350
|
+
* @description Starts transmitting a break signal
|
|
351
|
+
* @returns {Promise<void>} A promise that resolves when break signal starts
|
|
352
|
+
*/
|
|
353
|
+
setBreak(): Promise<void>;
|
|
354
|
+
/**
|
|
355
|
+
* @description Stops transmitting a break signal
|
|
356
|
+
* @returns {Promise<void>} A promise that resolves when break signal stops
|
|
357
|
+
*/
|
|
358
|
+
clearBreak(): Promise<void>;
|
|
359
|
+
/**
|
|
360
|
+
* @description Writes string data to the serial port
|
|
361
|
+
* @param {string} value The data to write
|
|
362
|
+
* @returns {Promise<number>} A promise that resolves to the number of bytes written
|
|
363
|
+
*/
|
|
364
|
+
write(value: string): Promise<number>;
|
|
365
|
+
/**
|
|
366
|
+
* @description Writes binary data to the serial port
|
|
367
|
+
* @param {Uint8Array | number[]} value The binary data to write
|
|
368
|
+
* @returns {Promise<number>} A promise that resolves to the number of bytes written
|
|
369
|
+
*/
|
|
370
|
+
writeBinary(value: Uint8Array | number[]): Promise<number>;
|
|
371
|
+
}
|
|
372
|
+
export { SerialPort };
|