UNPKG

5.48 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import * as Stream from "stream";
4
5declare class SerialPort extends Stream.Duplex {
6 constructor(path: string, callback?: SerialPort.ErrorCallback);
7 constructor(path: string, options?: SerialPort.OpenOptions, callback?: SerialPort.ErrorCallback);
8
9 readonly baudRate: number;
10 readonly binding: SerialPort.BaseBinding;
11 readonly isOpen: boolean;
12 readonly path: string;
13
14 open(callback?: SerialPort.ErrorCallback): void;
15 update(options: SerialPort.UpdateOptions, callback?: SerialPort.ErrorCallback): void;
16
17 write(
18 data: string | number[] | Buffer,
19 callback?: (error: Error | null | undefined, bytesWritten: number) => void,
20 ): boolean;
21 write(
22 buffer: string | number[] | Buffer,
23 encoding?: "ascii" | "utf8" | "utf16le" | "ucs2" | "base64" | "binary" | "hex",
24 callback?: (error: Error | null | undefined, bytesWritten: number) => void,
25 ): boolean;
26
27 read(size?: number): string | Buffer | null;
28
29 close(callback?: (error?: Error | null) => void): void;
30
31 set(options: SerialPort.SetOptions, callback?: SerialPort.ErrorCallback): void;
32 get(callback?: SerialPort.ModemBitsCallback): void;
33
34 flush(callback?: SerialPort.ErrorCallback): void;
35 drain(callback?: SerialPort.ErrorCallback): void;
36
37 pause(): this;
38 resume(): this;
39
40 on(event: string, callback: (data?: any) => void): this;
41
42 static Binding: SerialPort.BaseBinding;
43
44 static list(): Promise<SerialPort.PortInfo[]>;
45}
46
47declare namespace SerialPort {
48 // Callbacks Type Defs
49 type ErrorCallback = (error?: Error | null) => void;
50 type ModemBitsCallback = (
51 error: Error | null | undefined,
52 status: { cts: boolean; dsr: boolean; dcd: boolean },
53 ) => void;
54
55 // Options Type Defs
56 interface OpenOptions {
57 autoOpen?: boolean | undefined;
58 baudRate?:
59 | 115200
60 | 57600
61 | 38400
62 | 19200
63 | 9600
64 | 4800
65 | 2400
66 | 1800
67 | 1200
68 | 600
69 | 300
70 | 200
71 | 150
72 | 134
73 | 110
74 | 75
75 | 50
76 | number
77 | undefined;
78 dataBits?: 8 | 7 | 6 | 5 | undefined;
79 highWaterMark?: number | undefined;
80 lock?: boolean | undefined;
81 stopBits?: 1 | 2 | undefined;
82 parity?: "none" | "even" | "mark" | "odd" | "space" | undefined;
83 rtscts?: boolean | undefined;
84 xon?: boolean | undefined;
85 xoff?: boolean | undefined;
86 xany?: boolean | undefined;
87 binding?: BaseBinding | undefined;
88 bindingOptions?: {
89 vmin?: number | undefined;
90 vtime?: number | undefined;
91 } | undefined;
92 }
93 interface UpdateOptions {
94 baudRate?:
95 | 115200
96 | 57600
97 | 38400
98 | 19200
99 | 9600
100 | 4800
101 | 2400
102 | 1800
103 | 1200
104 | 600
105 | 300
106 | 200
107 | 150
108 | 134
109 | 110
110 | 75
111 | 50
112 | number
113 | undefined;
114 }
115 interface SetOptions {
116 brk?: boolean | undefined;
117 cts?: boolean | undefined;
118 dsr?: boolean | undefined;
119 dtr?: boolean | undefined;
120 rts?: boolean | undefined;
121 }
122
123 interface PortInfo {
124 path: string;
125 manufacturer?: string | undefined;
126 serialNumber?: string | undefined;
127 pnpId?: string | undefined;
128 locationId?: string | undefined;
129 productId?: string | undefined;
130 vendorId?: string | undefined;
131 }
132
133 namespace parsers {
134 class ByteLength extends Stream.Transform {
135 constructor(options: { length: number });
136 }
137 class CCTalk extends Stream.Transform {
138 constructor();
139 }
140 class Delimiter extends Stream.Transform {
141 constructor(options: { delimiter: string | Buffer | number[]; includeDelimiter?: boolean | undefined });
142 }
143 class Readline extends Delimiter {
144 constructor(
145 options: {
146 delimiter: string | Buffer | number[];
147 encoding?: "ascii" | "utf8" | "utf16le" | "ucs2" | "base64" | "binary" | "hex" | undefined;
148 includeDelimiter?: boolean | undefined;
149 },
150 );
151 }
152 class Ready extends Stream.Transform {
153 constructor(options: { delimiter: string | Buffer | number[] });
154 }
155 class Regex extends Stream.Transform {
156 constructor(options: { regex: RegExp });
157 }
158 }
159
160 // Binding Type Defs
161 type win32Binding = BaseBinding;
162 type darwinBinding = BaseBinding;
163 type linuxBinding = BaseBinding;
164
165 // Binding Type Def
166 class BaseBinding {
167 constructor(options: any);
168
169 open(path: string, options: OpenOptions): Promise<any>;
170 close(): Promise<any>;
171
172 read(data: Buffer, offset: number, length: number): Promise<any>;
173 write(data: Buffer): Promise<any>;
174 update(options?: UpdateOptions): Promise<any>;
175 set(options?: SetOptions): Promise<any>;
176 get(): Promise<any>;
177 flush(): Promise<any>;
178 drain(): Promise<any>;
179 static list(): Promise<PortInfo[]>;
180 }
181}
182
183export = SerialPort;