diff --git a/README.md b/README.md index 6440200..c87f60c 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,9 @@ Chances are you're looking for the [`serialport`](https://serialport.io/docs/api ## Bindings The Bindings provide a low level interface to work with your serialport. It is possible to use them alone but it's usually easier to use them with an interface. -- [`@serialport/bindings`](https://serialport.io/docs/api-bindings.md) bindings for Linux, Mac and Windows -- [`@serialport/binding-abstract`](https://serialport.io/docs/api-bindings-abstract.md) as an abstract class to use if you're making your own bindings -- [`@serialport/binding-mock`](https://serialport.io/docs/api-bindings-mock.md) for a mock binding package for testing +- [`@serialport/bindings`](https://serialport.io/docs/api-bindings) bindings for Linux, Mac and Windows +- [`@serialport/binding-abstract`](https://serialport.io/docs/api-bindings-abstract) as an abstract class to use if you're making your own bindings +- [`@serialport/binding-mock`](https://serialport.io/docs/api-binding-mock) for a mock binding package for testing ## Interfaces Interfaces take a binding object and provide a different API on top of it. Currently we only ship a Node Stream Interface. diff --git a/docs/api-binding-abstract.md b/docs/api-binding-abstract.md new file mode 100644 index 0000000..5459349 --- /dev/null +++ b/docs/api-binding-abstract.md @@ -0,0 +1,93 @@ +--- +id: api-binding-abstract +title: Abstract Binding +--- + +Typescript signature of Abstract Binding + +```ts + +class AbstractBinding { + static list(): Promise + + constructor(opt: OpenOptions) + + /** + * Opens a connection to the serial port referenced by the path. Promise resolves after the port is opened, configured and ready for use. + * @param {string} path the path or com port to open + * @param {openOptions} options openOptions for the serialport + * @returns {Promise} Resolves after the port is opened and configured. + */ + open(path: string, options: OpenOptions): Promise + + /** + * Closes an open port + */ + close(): Promise + + /** + * Request a number of bytes from the SerialPort. This function is similar to Node's [`fs.read`](http://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback) except it will always return at least one byte. + +The in progress reads must error when the port is closed with an error object that has the property `canceled` equal to `true`. Any other error will cause a disconnection. + + * @param {buffer} buffer Accepts a [`Buffer`](http://nodejs.org/api/buffer.html) object. + * @param {integer} offset The offset in the buffer to start writing at. + * @param {integer} length Specifies the maximum number of bytes to read. + * @returns {Promise} Resolves with the number of bytes read after a read operation. + */ + read(buffer: Buffer, offset: number, length: number): Promise + + /** + * Write bytes to the SerialPort. Only called when there is no pending write operation. + +The in progress writes must error when the port is closed with an error object that has the property `canceled` equal to `true`. Any other error will cause a disconnection. + + * @param {buffer} buffer - Accepts a [`Buffer`](http://nodejs.org/api/buffer.html) object. + * @returns {Promise} Resolves after the data is passed to the operating system for writing. + */ + write(buffer: Buffer): Promise + + /** + * Changes connection settings on an open port. Only `baudRate` is supported. + * @returns {Promise} Resolves once the port's baud rate changes. + */ + update(options: { baudRate: number }): Promise + + /** + * Set control flags on an open port. + * @param {object=} options All options are operating system default when the port is opened. Every flag is set on each call to the provided or default values. All options are always provided. + * @param {Boolean} [options.brk=false] flag for brk + * @param {Boolean} [options.cts=false] flag for cts + * @param {Boolean} [options.dsr=false] flag for dsr + * @param {Boolean} [options.dtr=true] flag for dtr + * @param {Boolean} [options.rts=true] flag for rts + * @returns {Promise} Resolves once the port's flags are set. + */ + set(options): Promise + + /** + * Get the control flags (CTS, DSR, DCD) on the open port. + * @returns {Promise} Resolves with the retrieved flags. + */ + get(): Promise + + /** + * Get the OS reported baud rate for the open port. Used mostly for debugging custom baud rates. + */ + getBaudRate(): Promise + + /** + * Flush (discard) data received but not read, and written but not transmitted. + * @returns {Promise} Resolves once the flush operation finishes. + */ + flush(): Promise + + /** + * Drain waits until all output data is transmitted to the serial port. An in progress write should be completed before this returns. + * @returns {Promise} Resolves once the drain operation finishes. + * @throws {TypeError} When given invalid arguments, a `TypeError` is thrown. + */ + drain(): Promise +} + +``` diff --git a/docs/api-binding-mock.md b/docs/api-binding-mock.md new file mode 100644 index 0000000..129c7a0 --- /dev/null +++ b/docs/api-binding-mock.md @@ -0,0 +1,72 @@ +--- +id: api-binding-mock +title: Mock Bindings +--- + +```typescript +new MockBinding(options: OpenOptions) +``` + + +Testing is an important feature of any library. To aid in our own tests we've developed a `MockBinding` a fake hardware binding that doesn't actually need any hardware to run. This class passes all of the same tests as our hardware based bindings and provides a few additional test related interfaces. + +### Example + +```js +const SerialPort = require('@serialport/stream') +const MockBinding = require('@serialport/binding-mock') + +SerialPort.Binding = MockBinding + +// Create a port and enable the echo and recording. +MockBinding.createPort('/dev/ROBOT', { echo: true, record: true }) +const port = new SerialPort('/dev/ROBOT') +``` + +```typescript +const AbstractBinding = require('@serialport/binding-abstract') +const debug = require('debug')('serialport/binding-mock') + +let ports = {} +let serialNumber = 0 + +function resolveNextTick(value) { + return new Promise(resolve => process.nextTick(() => resolve(value))) +} + +/** + * Mock bindings for pretend serialport access + */ +class MockBinding extends AbstractBinding { + // if record is true this buffer will have all data that has been written to this port + readonly recording: Buffer + + // the buffer of the latest written data + readonly lastWrite: null | Buffer + + // Create a mock port + static createPort(path: string, opt: { echo?: boolean, record?: boolean, readyData?: Buffer}): void + + // Reset available mock ports + static reset(): void + + // list mock ports + static list(): Promise + + // Emit data on a mock port + emitData(data: Buffer | string | number[]) + +// Standard bindings interface + open(path: string, opt: OpenOpts): Promise + close(): Promise + read(buffer: Buffer, offset: number, length: number): Promise + write(buffer: Buffer): Promise + update(options: { baudRate: number }): Promise + set(options): Promise + get(): Promise + getBaudRate(): Promise + flush(): Promise + drain(): Promise +} + +``` diff --git a/docs/api-bindings.md b/docs/api-bindings.md index 4406e93..fc6cdc6 100644 --- a/docs/api-bindings.md +++ b/docs/api-bindings.md @@ -2,12 +2,15 @@ id: api-bindings title: Bindings --- -Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac euismod odio, eu consequat dui. Nullam molestie consectetur risus id imperdiet. Proin sodales ornare turpis, non mollis massa ultricies id. Nam at nibh scelerisque, feugiat ante non, dapibus tortor. Vivamus volutpat diam quis tellus elementum bibendum. Praesent semper gravida velit quis aliquam. Etiam in cursus neque. Nam lectus ligula, malesuada et mauris a, bibendum faucibus mi. Phasellus ut interdum felis. Phasellus in odio pulvinar, porttitor urna eget, fringilla lectus. Aliquam sollicitudin est eros. Mauris consectetur quam vitae mauris interdum hendrerit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. -Duis et egestas libero, imperdiet faucibus ipsum. Sed posuere eget urna vel feugiat. Vivamus a arcu sagittis, fermentum urna dapibus, congue lectus. Fusce vulputate porttitor nisl, ac cursus elit volutpat vitae. Nullam vitae ipsum egestas, convallis quam non, porta nibh. Morbi gravida erat nec neque bibendum, eu pellentesque velit posuere. Fusce aliquam erat eu massa eleifend tristique. +The `Binding` is how Node-SerialPort talks to the underlying system. By default, we auto detect Windows, Linux and OS X, and load the appropriate module for your system. You can assign `SerialPort.Binding` to any binding you like. Find more by searching ["serialport-binding" at npm](https://www.npmjs.com/search?q=serialport-binding). -Sed consequat sollicitudin ipsum eget tempus. Integer a aliquet velit. In justo nibh, pellentesque non suscipit eget, gravida vel lacus. Donec odio ante, malesuada in massa quis, pharetra tristique ligula. Donec eros est, tristique eget finibus quis, semper non nisl. Vivamus et elit nec enim ornare placerat. Sed posuere odio a elit cursus sagittis. + You can prevent auto loading the default bindings by requiring the [SerialPort Stream](api-stream.md) package. + ```js + var SerialPort = require('@serialport/stream'); + SerialPort.Binding = MyBindingClass; + ``` -Phasellus feugiat purus eu tortor ultrices finibus. Ut libero nibh, lobortis et libero nec, dapibus posuere eros. Sed sagittis euismod justo at consectetur. Nulla finibus libero placerat, cursus sapien at, eleifend ligula. Vivamus elit nisl, hendrerit ac nibh eu, ultrices tempus dui. Nam tellus neque, commodo non rhoncus eu, gravida in risus. Nullam id iaculis tortor. +You never have to use `Binding` objects directly. `@serialPort/stream` uses them to access the underlying hardware. This documentation is geared towards people who are making bindings for different platforms. The `AbstractBinding` class from the [`@serialport/binding-abstract`](api-binding-abstract.md) package can be inherited from to get the base api. -Nullam at odio in sem varius tempor sit amet vel lorem. Etiam eu hendrerit nisl. Fusce nibh mauris, vulputate sit amet ex vitae, congue rhoncus nisl. Sed eget tellus purus. Nullam tempus commodo erat ut tristique. Cras accumsan massa sit amet justo consequat eleifend. Integer scelerisque vitae tellus id consectetur. +There is also a [`MockBinding`](api-binding-mock.md) package to assist with testing. diff --git a/docs/api-overview.md b/docs/api-overview.md index 8e39fca..347c541 100644 --- a/docs/api-overview.md +++ b/docs/api-overview.md @@ -8,8 +8,8 @@ Chances are you're looking for the [`serialport`](api-serialport.md) package whi ## Bindings The Bindings provide a low level interface to work with your serialport. It is possible to use them alone but it's usually easier to use them with an interface. - [`@serialport/bindings`](api-bindings.md) bindings for Linux, Mac and Windows -- [`@serialport/binding-abstract`](api-bindings-abstract.md) as an abstract class to use if you're making your own bindings -- [`@serialport/binding-mock`](api-bindings-mock.md) for a mock binding package for testing +- [`@serialport/binding-abstract`](api-binding-abstract.md) as an abstract class to use if you're making your own bindings +- [`@serialport/binding-mock`](api-binding-mock.md) for a mock binding package for testing ## Interfaces Interfaces take a binding object and provide a different API on top of it. Currently we only ship a Node Stream Interface. diff --git a/docs/api-parser-delimiter.md b/docs/api-parser-delimiter.md index b7f98fd..6957ab4 100644 --- a/docs/api-parser-delimiter.md +++ b/docs/api-parser-delimiter.md @@ -3,7 +3,7 @@ id: api-parser-delimiter title: Delimiter Parser --- ```typescript -new Delimiter(options) +new Delimiter(options: { delimiter: string | Buffer | number[] }) ``` A transform stream that emits data each time a byte sequence is received. To use the `Delimiter` parser, provide a delimiter as a string, buffer, or array of bytes. Runs in O(n) time. diff --git a/docs/guide-testing.md b/docs/guide-testing.md index f7c5089..3a82181 100644 --- a/docs/guide-testing.md +++ b/docs/guide-testing.md @@ -3,15 +3,17 @@ id: guide-testing title: Testing --- -Testing is an important feature of any library. To aid in our own tests we've developed a `MockBinding` a fake hardware binding that doesn't actually need any hardware to run. This class passes all of the same tests as our hardware based bindings and provides a few additional test related interfaces. +Testing is an important feature of any library. To aid in our own tests we've developed a [`MockBinding`](api-binding-mock.md) a fake hardware binding that doesn't actually need any hardware to run. This class passes all of the same tests as our hardware based bindings and provides a few additional test related interfaces. ```js -const SerialPort = require('serialport/test'); -const MockBinding = SerialPort.Binding; +const SerialPort = require('@serialport/stream') +const MockBinding = require('@serialport/binding-mock') + +SerialPort.Binding = MockBinding // Create a port and enable the echo and recording. MockBinding.createPort('/dev/ROBOT', { echo: true, record: true }) const port = new SerialPort('/dev/ROBOT') ``` -The code can be found in the `@serialport/binding-mock` package. +The code can be found in the [`@serialport/binding-mock`](api-binding-mock.md) package. diff --git a/packages/binding-abstract/binding-abstract.js b/packages/binding-abstract/binding-abstract.js index 65cd473..6fce868 100644 --- a/packages/binding-abstract/binding-abstract.js +++ b/packages/binding-abstract/binding-abstract.js @@ -131,7 +131,7 @@ The in progress writes must error when the port is closed with an error object t /** * Changes connection settings on an open port. Only `baudRate` is supported. * @param {object=} options Only supports `baudRate`. - * @param {number=} [options.baudRate] If provided a baud rate that the bindings do not support, it should pass an error to the callback. + * @param {number=} [options.baudRate] If provided a baud rate that the bindings do not support, it should reject. * @returns {Promise} Resolves once the port's baud rate changes. * @throws {TypeError} When given invalid arguments, a `TypeError` is thrown. */ diff --git a/packages/binding-abstract/package.json b/packages/binding-abstract/package.json index c9a9d78..758faa1 100644 --- a/packages/binding-abstract/package.json +++ b/packages/binding-abstract/package.json @@ -2,6 +2,9 @@ "name": "@serialport/binding-abstract", "version": "2.0.1", "main": "binding-abstract.js", + "keywords": [ + "serialport-binding" + ], "dependencies": { "debug": "^4.0.1" }, diff --git a/packages/binding-mock/package.json b/packages/binding-mock/package.json index 58cf851..923839a 100644 --- a/packages/binding-mock/package.json +++ b/packages/binding-mock/package.json @@ -2,6 +2,9 @@ "name": "@serialport/binding-mock", "version": "2.0.1", "main": "binding-mock.js", + "keywords": [ + "serialport-binding" + ], "dependencies": { "@serialport/binding-abstract": "^2.0.1", "debug": "^4.0.1" diff --git a/packages/bindings/package.json b/packages/bindings/package.json index 730ab01..a4207a6 100644 --- a/packages/bindings/package.json +++ b/packages/bindings/package.json @@ -2,6 +2,9 @@ "name": "@serialport/bindings", "version": "2.0.2", "main": "lib", + "keywords": [ + "serialport-binding" + ], "dependencies": { "@serialport/binding-abstract": "^2.0.1", "@serialport/parser-readline": "^2.0.1", diff --git a/packages/website/i18n/en.json b/packages/website/i18n/en.json index 7afebe9..9937bb7 100644 --- a/packages/website/i18n/en.json +++ b/packages/website/i18n/en.json @@ -5,6 +5,12 @@ "previous": "Previous", "tagline": "Node.js package to access serial ports for Linux, OSX and Windows.", "docs": { + "api-binding-abstract": { + "title": "Abstract Binding" + }, + "api-binding-mock": { + "title": "Mock Bindings" + }, "api-bindings": { "title": "Bindings" }, @@ -68,7 +74,8 @@ }, "links": { "Guides": "Guides", - "API": "API" + "API": "API", + "GitHub": "GitHub" }, "categories": { "Getting Started": "Getting Started", diff --git a/packages/website/sidebars.json b/packages/website/sidebars.json index 13269d8..d3940ea 100644 --- a/packages/website/sidebars.json +++ b/packages/website/sidebars.json @@ -5,7 +5,7 @@ }, "api": { "Core": ["api-overview", "api-serialport"], - "Bindings": ["api-bindings"], + "Bindings": ["api-bindings", "api-binding-mock", "api-binding-abstract"], "Interfaces": ["api-stream"], "Parsers": [ "api-parsers-overview", diff --git a/packages/website/siteConfig.js b/packages/website/siteConfig.js index f8f65f9..fe7701f 100644 --- a/packages/website/siteConfig.js +++ b/packages/website/siteConfig.js @@ -34,6 +34,7 @@ const siteConfig = { headerLinks: [ { doc: 'guide-about', label: 'Guides' }, { doc: 'api-overview', label: 'API' }, + { href: 'https://github.com/node-serialport/node-serialport', label: 'GitHub' }, // { page: 'help', label: 'Help' }, // { blog: true, label: 'Blog' }, ],