UNPKG

5.22 kBTypeScriptView Raw
1// Type definitions for serialport 8.0
2// Project: https://github.com/node-serialport/node-serialport
3// Definitions by: Jeremy Foster <https://github.com/codefoster>
4// Andrew Pearson <https://github.com/apearson>
5// Cameron Tacklind <https://github.com/cinderblock>
6// Doug Brunner <https://github.com/doug-a-brunner>
7// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8
9/// <reference types="node" />
10
11import * as Stream from 'stream';
12
13declare class SerialPort extends Stream.Duplex {
14 constructor(path: string, callback?: SerialPort.ErrorCallback);
15 constructor(path: string, options?: SerialPort.OpenOptions, callback?: SerialPort.ErrorCallback);
16
17 readonly baudRate: number;
18 readonly binding: SerialPort.BaseBinding;
19 readonly isOpen: boolean;
20 readonly path: string;
21
22 open(callback?: SerialPort.ErrorCallback): void;
23 update(options: SerialPort.UpdateOptions, callback?: SerialPort.ErrorCallback): void;
24
25 write(data: string| number[] | Buffer, callback?: (error: Error | null | undefined, bytesWritten: number) => void): boolean;
26 write(buffer: string| number[] | Buffer, encoding?: 'ascii'|'utf8'|'utf16le'|'ucs2'|'base64'|'binary'|'hex', callback?: (error: Error | null | undefined, bytesWritten: number) => void): boolean;
27
28 read(size?: number): string | Buffer | null;
29
30 close(callback?: (error?: Error | null) => void): void;
31
32 set(options: SerialPort.SetOptions, callback?: SerialPort.ErrorCallback): void;
33 get(callback?: SerialPort.ModemBitsCallback): void;
34
35 flush(callback?: SerialPort.ErrorCallback): void;
36 drain(callback?: SerialPort.ErrorCallback): void;
37
38 pause(): this;
39 resume(): this;
40
41 on(event: string, callback: (data?: any) => void): this;
42
43 static Binding: SerialPort.BaseBinding;
44
45 static list(): Promise<SerialPort.PortInfo[]>;
46}
47
48declare namespace SerialPort {
49 // Callbacks Type Defs
50 type ErrorCallback = (error?: Error | null) => void;
51 type ModemBitsCallback = (error: Error | null | undefined, status: {cts: boolean, dsr: boolean, dcd: boolean }) => void;
52
53 // Options Type Defs
54 interface OpenOptions {
55 autoOpen?: boolean | undefined;
56 baudRate?: 115200|57600|38400|19200|9600|4800|2400|1800|1200|600|300|200|150|134|110|75|50|number | undefined;
57 dataBits?: 8|7|6|5 | undefined;
58 highWaterMark?: number | undefined;
59 lock?: boolean | undefined;
60 stopBits?: 1|2 | undefined;
61 parity?: 'none'|'even'|'mark'|'odd'|'space' | undefined;
62 rtscts?: boolean | undefined;
63 xon?: boolean | undefined;
64 xoff?: boolean | undefined;
65 xany?: boolean | undefined;
66 binding?: BaseBinding | undefined;
67 bindingOptions?: {
68 vmin?: number | undefined;
69 vtime?: number | undefined;
70 } | undefined;
71 }
72 interface UpdateOptions {
73 baudRate?: 115200|57600|38400|19200|9600|4800|2400|1800|1200|600|300|200|150|134|110|75|50|number | undefined;
74 }
75 interface SetOptions {
76 brk?: boolean | undefined;
77 cts?: boolean | undefined;
78 dsr?: boolean | undefined;
79 dtr?: boolean | undefined;
80 rts?: boolean | undefined;
81 }
82
83 interface PortInfo {
84 path: string;
85 manufacturer?: string | undefined;
86 serialNumber?: string | undefined;
87 pnpId?: string | undefined;
88 locationId?: string | undefined;
89 productId?: string | undefined;
90 vendorId?: string | undefined;
91 }
92
93 namespace parsers {
94 class ByteLength extends Stream.Transform {
95 constructor(options: {length: number});
96 }
97 class CCTalk extends Stream.Transform {
98 constructor();
99 }
100 class Delimiter extends Stream.Transform {
101 constructor(options: {delimiter: string | Buffer | number[], includeDelimiter?: boolean | undefined});
102 }
103 class Readline extends Delimiter {
104 constructor(options: {delimiter: string | Buffer | number[], encoding?: 'ascii'|'utf8'|'utf16le'|'ucs2'|'base64'|'binary'|'hex' | undefined, includeDelimiter?: boolean | undefined});
105 }
106 class Ready extends Stream.Transform {
107 constructor(options: {delimiter: string | Buffer | number[]});
108 }
109 class Regex extends Stream.Transform {
110 constructor(options: {regex: RegExp});
111 }
112 }
113
114 // Binding Type Defs
115 type win32Binding = BaseBinding;
116 type darwinBinding = BaseBinding;
117 type linuxBinding = BaseBinding;
118
119 // Binding Type Def
120 class BaseBinding {
121 constructor(options: any);
122
123 open(path: string, options: OpenOptions): Promise<any>;
124 close(): Promise<any>;
125
126 read(data: Buffer, offset: number, length: number): Promise<any>;
127 write(data: Buffer): Promise<any>;
128 update(options?: UpdateOptions): Promise<any>;
129 set(options?: SetOptions): Promise<any>;
130 get(): Promise<any>;
131 flush(): Promise<any>;
132 drain(): Promise<any>;
133 static list(): Promise<PortInfo[]>;
134 }
135}
136
137export = SerialPort;