From 4bf0b3d79180f6fe00848bbba592eab2ef81f714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ondrej=20Slint=C3=A1k?= Date: Mon, 29 Oct 2018 17:18:16 +0100 Subject: [PATCH 1/4] Move v6 typings for node-serialport to separate directory --- types/serialport/v6/index.d.ts | 126 +++++++++++++++++++++++ types/serialport/v6/serialport-tests.ts | 128 ++++++++++++++++++++++++ types/serialport/v6/tsconfig.json | 28 ++++++ types/serialport/v6/tslint.json | 1 + 4 files changed, 283 insertions(+) create mode 100644 types/serialport/v6/index.d.ts create mode 100644 types/serialport/v6/serialport-tests.ts create mode 100644 types/serialport/v6/tsconfig.json create mode 100644 types/serialport/v6/tslint.json diff --git a/types/serialport/v6/index.d.ts b/types/serialport/v6/index.d.ts new file mode 100644 index 0000000000..2f5f8fc55d --- /dev/null +++ b/types/serialport/v6/index.d.ts @@ -0,0 +1,126 @@ +// Type definitions for serialport 6.0 +// Project: https://github.com/EmergingTechnologyAdvisors/node-serialport +// Definitions by: Jeremy Foster +// Andrew Pearson +// 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: any, bytesWritten: number) => void): boolean; + write(buffer: string| number[] | Buffer, encoding?: 'ascii'|'utf8'|'utf16le'|'ucs2'|'base64'|'binary'|'hex', callback?: (error: any, bytesWritten: number) => void): boolean; + + read(size?: number): string | Buffer | null; + + close(callback?: (error: Error) => 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(): Promise; +} + +declare namespace SerialPort { + // Callbacks Type Defs + type ErrorCallback = (error: Error) => void; + type ModemBitsCallback = (error: Error, status: {cts: boolean, dsr: boolean, dcd: boolean }) => void; + type ListCallback = (error: Error, port: 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; + } + + 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'}); + } + class Ready extends Stream.Transform { + constructor(options: {data: 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/v6/serialport-tests.ts b/types/serialport/v6/serialport-tests.ts new file mode 100644 index 0000000000..7b540c5e12 --- /dev/null +++ b/types/serialport/v6/serialport-tests.ts @@ -0,0 +1,128 @@ +// 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: 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: 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) => {}); + port.write('test', 'utf8', (error: Error) => {}); +} + +function test_read() { + const port = new SerialPort(''); + + const data = port.read(8); +} + +function test_close() { + const port = new SerialPort(''); + + port.close((error: Error) => {}); +} + +function test_set() { + const port = new SerialPort(''); + + port.set({}, (error: Error) => {}); +} + +function test_get() { + const port = new SerialPort(''); + + port.get((error, status) => {}); +} + +function test_flush() { + const port = new SerialPort(''); + + port.flush((error: Error) => {}); +} + +function test_drain() { + const port = new SerialPort(''); + + port.drain((error: Error) => {}); +} + +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') }); + const ReadlineParser = new SerialPort.parsers.Readline({ delimiter: '\r\n' }); + const ReadyParser = new SerialPort.parsers.Ready({ data: '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; +} diff --git a/types/serialport/v6/tsconfig.json b/types/serialport/v6/tsconfig.json new file mode 100644 index 0000000000..fbce55cebb --- /dev/null +++ b/types/serialport/v6/tsconfig.json @@ -0,0 +1,28 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "paths": { + "serialport": [ + "serialport/v6" + ] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "serialport-tests.ts" + ] +} diff --git a/types/serialport/v6/tslint.json b/types/serialport/v6/tslint.json new file mode 100644 index 0000000000..2750cc0197 --- /dev/null +++ b/types/serialport/v6/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } \ No newline at end of file From 42b2226022943251c9a23867e3e7dcc88812265f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ondrej=20Slint=C3=A1k?= Date: Mon, 29 Oct 2018 17:30:23 +0100 Subject: [PATCH 2/4] Bump root serialport bindings to 7.0 --- types/serialport/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/serialport/index.d.ts b/types/serialport/index.d.ts index 2f5f8fc55d..af560f6ddc 100644 --- a/types/serialport/index.d.ts +++ b/types/serialport/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for serialport 6.0 +// Type definitions for serialport 7.0 // Project: https://github.com/EmergingTechnologyAdvisors/node-serialport // Definitions by: Jeremy Foster // Andrew Pearson From 7860e824670d0c9f02b39b4480129c82894cebf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ondrej=20Slint=C3=A1k?= Date: Mon, 29 Oct 2018 17:31:00 +0100 Subject: [PATCH 3/4] Add optional callback to SerialPort.list --- types/serialport/index.d.ts | 2 +- types/serialport/serialport-tests.ts | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/types/serialport/index.d.ts b/types/serialport/index.d.ts index af560f6ddc..b015182fdf 100644 --- a/types/serialport/index.d.ts +++ b/types/serialport/index.d.ts @@ -40,7 +40,7 @@ declare class SerialPort extends Stream.Duplex { static Binding: SerialPort.BaseBinding; - static list(): Promise; + static list(callback?: SerialPort.ListCallback): Promise; } declare namespace SerialPort { diff --git a/types/serialport/serialport-tests.ts b/types/serialport/serialport-tests.ts index 7b540c5e12..16cf2b6a70 100644 --- a/types/serialport/serialport-tests.ts +++ b/types/serialport/serialport-tests.ts @@ -126,3 +126,14 @@ function test_properties() { const isOpen: boolean = port.isOpen; const path: string = port.path; } + +function test_list_ports_promise() { + const ports = SerialPort + .list() + .then((ports: any) => {}) + .catch((err: Error) => {}); +} + +function test_list_ports_callback() { + const ports = SerialPort.list((error: Error, port: any[]) => {}); +} From 30d1040e72d62be6280f9d5b66fe5b40557d97c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ondrej=20Slint=C3=A1k?= Date: Mon, 29 Oct 2018 17:53:05 +0100 Subject: [PATCH 4/4] Add PortInfo interface to serialport typings --- types/serialport/index.d.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/types/serialport/index.d.ts b/types/serialport/index.d.ts index b015182fdf..14ded3b6e3 100644 --- a/types/serialport/index.d.ts +++ b/types/serialport/index.d.ts @@ -40,7 +40,7 @@ declare class SerialPort extends Stream.Duplex { static Binding: SerialPort.BaseBinding; - static list(callback?: SerialPort.ListCallback): Promise; + static list(callback?: SerialPort.ListCallback): Promise; } declare namespace SerialPort { @@ -77,6 +77,16 @@ declare namespace SerialPort { dsr?: boolean; dtr?: boolean; rts?: boolean; + } + + interface PortInfo { + comName: string; + manufacturer?: string; + serialNumber?: string; + pnpId?: string; + locationId?: string; + productId?: string; + vendorId?: string; } namespace parsers { @@ -119,7 +129,7 @@ declare namespace SerialPort { get(): Promise; flush(): Promise; drain(): Promise; - static list(): Promise; + static list(): Promise; } }