diff --git a/types/serialport/index.d.ts b/types/serialport/index.d.ts index 41dba7d66c..6c7134b6d6 100644 --- a/types/serialport/index.d.ts +++ b/types/serialport/index.d.ts @@ -1,8 +1,9 @@ -// Type definitions for serialport 7.0 +// Type definitions for serialport 8.0 // Project: https://github.com/node-serialport/node-serialport // Definitions by: Jeremy Foster // Andrew Pearson // Cameron Tacklind +// Doug Brunner // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// @@ -41,14 +42,13 @@ declare class SerialPort extends Stream.Duplex { static Binding: SerialPort.BaseBinding; - static list(callback?: SerialPort.ListCallback): Promise; + static list(): Promise; } declare namespace SerialPort { // Callbacks Type Defs type ErrorCallback = (error?: Error | null) => void; type ModemBitsCallback = (error: Error | null | undefined, status: {cts: boolean, dsr: boolean, dcd: boolean }) => void; - type ListCallback = (error: Error | null | undefined, ports: any[]) => void; // Options Type Defs interface OpenOptions { @@ -81,7 +81,7 @@ declare namespace SerialPort { } interface PortInfo { - comName: string; + path: string; manufacturer?: string; serialNumber?: string; pnpId?: string; diff --git a/types/serialport/serialport-tests.ts b/types/serialport/serialport-tests.ts index 9ab7ac45a9..f29869364f 100644 --- a/types/serialport/serialport-tests.ts +++ b/types/serialport/serialport-tests.ts @@ -139,7 +139,3 @@ function test_list_ports_promise() { .then((ports: SerialPort.PortInfo[]) => {}) .catch((err: Error) => {}); } - -function test_list_ports_callback() { - const ports = SerialPort.list((error: Error | null | undefined, ports: any[]) => {}); -} diff --git a/types/serialport/v7/index.d.ts b/types/serialport/v7/index.d.ts new file mode 100644 index 0000000000..41dba7d66c --- /dev/null +++ b/types/serialport/v7/index.d.ts @@ -0,0 +1,137 @@ +// Type definitions for serialport 7.0 +// Project: https://github.com/node-serialport/node-serialport +// Definitions by: Jeremy Foster +// Andrew Pearson +// Cameron Tacklind +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import * as Stream from 'stream'; + +declare class SerialPort extends Stream.Duplex { + constructor(path: string, callback?: SerialPort.ErrorCallback); + constructor(path: string, options?: SerialPort.OpenOptions, callback?: SerialPort.ErrorCallback); + + readonly baudRate: number; + readonly binding: SerialPort.BaseBinding; + readonly isOpen: boolean; + readonly path: string; + + open(callback?: SerialPort.ErrorCallback): void; + update(options: SerialPort.UpdateOptions, callback?: SerialPort.ErrorCallback): void; + + write(data: string| number[] | Buffer, callback?: (error: Error | null | undefined, bytesWritten: number) => void): boolean; + write(buffer: string| number[] | Buffer, encoding?: 'ascii'|'utf8'|'utf16le'|'ucs2'|'base64'|'binary'|'hex', callback?: (error: Error | null | undefined, bytesWritten: number) => void): boolean; + + read(size?: number): string | Buffer | null; + + close(callback?: (error?: Error | null) => void): void; + + set(options: SerialPort.SetOptions, callback?: SerialPort.ErrorCallback): void; + get(callback?: SerialPort.ModemBitsCallback): void; + + flush(callback?: SerialPort.ErrorCallback): void; + drain(callback?: SerialPort.ErrorCallback): void; + + pause(): this; + resume(): this; + + on(event: string, callback: (data?: any) => void): this; + + static Binding: SerialPort.BaseBinding; + + static list(callback?: SerialPort.ListCallback): Promise; +} + +declare namespace SerialPort { + // Callbacks Type Defs + type ErrorCallback = (error?: Error | null) => void; + type ModemBitsCallback = (error: Error | null | undefined, status: {cts: boolean, dsr: boolean, dcd: boolean }) => void; + type ListCallback = (error: Error | null | undefined, ports: any[]) => void; + + // Options Type Defs + interface OpenOptions { + autoOpen?: boolean; + baudRate?: 115200|57600|38400|19200|9600|4800|2400|1800|1200|600|300|200|150|134|110|75|50|number; + dataBits?: 8|7|6|5; + highWaterMark?: number; + lock?: boolean; + stopBits?: 1|2; + parity?: 'none'|'even'|'mark'|'odd'|'space'; + rtscts?: boolean; + xon?: boolean; + xoff?: boolean; + xany?: boolean; + binding?: BaseBinding; + bindingOptions?: { + vmin?: number; + vtime?: number; + }; + } + interface UpdateOptions { + baudRate?: 115200|57600|38400|19200|9600|4800|2400|1800|1200|600|300|200|150|134|110|75|50|number; + } + interface SetOptions { + brk?: boolean; + cts?: boolean; + dsr?: boolean; + dtr?: boolean; + rts?: boolean; + } + + interface PortInfo { + comName: string; + manufacturer?: string; + serialNumber?: string; + pnpId?: string; + locationId?: string; + productId?: string; + vendorId?: string; + } + + namespace parsers { + class ByteLength extends Stream.Transform { + constructor(options: {length: number}); + } + class CCTalk extends Stream.Transform { + constructor(); + } + class Delimiter extends Stream.Transform { + constructor(options: {delimiter: string | Buffer | number[], includeDelimiter?: boolean}); + } + class Readline extends Delimiter { + constructor(options: {delimiter: string | Buffer | number[], encoding?: 'ascii'|'utf8'|'utf16le'|'ucs2'|'base64'|'binary'|'hex', includeDelimiter?: boolean}); + } + class Ready extends Stream.Transform { + constructor(options: {delimiter: string | Buffer | number[]}); + } + class Regex extends Stream.Transform { + constructor(options: {regex: RegExp}); + } + } + + // Binding Type Defs + type win32Binding = BaseBinding; + type darwinBinding = BaseBinding; + type linuxBinding = BaseBinding; + + // Binding Type Def + class BaseBinding { + constructor(options: any); + + open(path: string, options: OpenOptions): Promise; + close(): Promise; + + read(data: Buffer, offset: number, length: number): Promise; + write(data: Buffer): Promise; + update(options?: UpdateOptions): Promise; + set(options?: SetOptions): Promise; + get(): Promise; + flush(): Promise; + drain(): Promise; + static list(): Promise; + } +} + +export = SerialPort; diff --git a/types/serialport/v7/serialport-tests.ts b/types/serialport/v7/serialport-tests.ts new file mode 100644 index 0000000000..9ab7ac45a9 --- /dev/null +++ b/types/serialport/v7/serialport-tests.ts @@ -0,0 +1,145 @@ +// Tests taken from documentation samples. + +import SerialPort = require('serialport'); + +function test_basic_connect() { + const port = new SerialPort(''); +} + +function test_connect_config() { + const port1 = new SerialPort('', { + }, error => {}); + + const port4 = new SerialPort('', { + autoOpen: false, + lock: false, + baudRate: 115200, + dataBits: 5, + stopBits: 2, + parity: 'odd', + rtscts: true, + xon: true, + xoff: true, + highWaterMark: 1024, + bindingOptions: { + vmin: 1, + vtime: 1 + } + }, + error => { + if (error !== null) { + console.error(error); + } + } + ); +} + +function test_open() { + const port = new SerialPort(''); + port.open(() => {}); +} + +function test_update() { + const port = new SerialPort(''); + port.update({baudRate: 57600}); +} + +function test_write() { + const port = new SerialPort(''); + + port.write('test', (error?: Error | null) => {}); + port.write('test', 'utf8', (error?: Error | null) => {}); +} + +function test_read() { + const port = new SerialPort(''); + + const data = port.read(8); +} + +function test_close() { + const port = new SerialPort(''); + + port.close((error?: Error | null) => {}); +} + +function test_set() { + const port = new SerialPort(''); + + port.set({}, (error?: Error | null) => {}); +} + +function test_get() { + const port = new SerialPort(''); + + port.get((error, status) => {}); +} + +function test_flush() { + const port = new SerialPort(''); + + port.flush((error?: Error | null) => {}); +} + +function test_drain() { + const port = new SerialPort(''); + + port.drain((error?: Error | null) => {}); +} + +function test_pause_resume() { + const port = new SerialPort(''); + + const pauseItem: SerialPort = port.pause(); + const resumeItem: SerialPort = port.resume(); +} + +function test_on_events() { + const port = new SerialPort(''); + + const onItem: SerialPort = port.on('event', (data: any) => {}); +} + +function test_binding() { + const port = new SerialPort(''); + + const bindingItem: SerialPort.BaseBinding = SerialPort.Binding; +} + +function test_parsers() { + const port = new SerialPort(''); + + const ByteLengthParser = new SerialPort.parsers.ByteLength({length: 8}); + const CCTalkParser = new SerialPort.parsers.CCTalk(); + const DelimiterParser = new SerialPort.parsers.Delimiter({ delimiter: Buffer.from('EOL'), includeDelimiter: true }); + const ReadlineParser = new SerialPort.parsers.Readline({ delimiter: '\r\n', includeDelimiter: false }); + const ReadyParser = new SerialPort.parsers.Ready({ delimiter: 'READY' }); + const RegexParser = new SerialPort.parsers.Regex({regex: /.*/}); + + port.pipe(ByteLengthParser); + port.pipe(CCTalkParser); + port.pipe(DelimiterParser); + port.pipe(ReadlineParser); + port.pipe(ReadyParser); + port.pipe(RegexParser); +} + +function test_properties() { + const port = new SerialPort(''); + + const baudRate: number = port.baudRate; + const binding: SerialPort.BaseBinding = port.binding; + const isOpen: boolean = port.isOpen; + const path: string = port.path; +} + +function test_list_ports_promise() { + const ports = SerialPort + .list() + .then((ports: SerialPort.PortInfo[]) => {}) + .catch((err: Error) => {}); +} + +function test_list_ports_callback() { + const ports = SerialPort.list((error: Error | null | undefined, ports: any[]) => {}); +} diff --git a/types/serialport/v7/tsconfig.json b/types/serialport/v7/tsconfig.json new file mode 100644 index 0000000000..ae681474a4 --- /dev/null +++ b/types/serialport/v7/tsconfig.json @@ -0,0 +1,28 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "paths": { + "serialport": [ + "serialport/v7" + ] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "serialport-tests.ts" + ] +} diff --git a/types/serialport/v7/tslint.json b/types/serialport/v7/tslint.json new file mode 100644 index 0000000000..fcae8d5d63 --- /dev/null +++ b/types/serialport/v7/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "strict-type-predicates": true + } +}