Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const debug = require('debug')('serialport/binding-abstract')
/**
* @name Binding
* @type {AbstractBinding}
* @since 5.0.0
* @description 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 at [npm](https://npmjs.org/).
Prevent auto loading the default bindings by requiring SerialPort with:
```js
var SerialPort = require('@serialport/stream');
SerialPort.Binding = MyBindingClass;
```
*/
/**
* You never have to use `Binding` objects directly. SerialPort uses them to access the underlying hardware. This documentation is geared towards people who are making bindings for different platforms. This class can be inherited from to get type checking for each method.
* @class AbstractBinding
* @param {object} options options for the binding
* @property {boolean} isOpen Required property. `true` if the port is open, `false` otherwise. Should be read-only.
* @throws {TypeError} When given invalid arguments, a `TypeError` is thrown.
* @since 5.0.0
*/
class AbstractBinding {
/**
* Retrieves a list of available serial ports with metadata. The `path` must be guaranteed, and all other fields should be undefined if unavailable. The `path` is either the path or an identifier (eg `COM1`) used to open the serialport.
* @returns {Promise} resolves to an array of port [info objects](#module_serialport--SerialPort.list).
*/
static async list() {
debug('list')
}
constructor(opt = {}) {
if (typeof opt !== 'object') {
throw new TypeError('"options" is not an object')
}
}
/**
* Opens a connection to the serial port referenced by the path.
* @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.
* @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected.
*/
async open(path, options) {
if (!path) {
throw new TypeError('"path" is not a valid port')
}
if (typeof options !== 'object') {
throw new TypeError('"options" is not an object')
}
debug('open')
if (this.isOpen) {
throw new Error('Already open')
}
}
/**
* Closes an open connection
* @returns {Promise} Resolves once the connection is closed.
* @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected.
*/
async close() {
debug('close')
if (!this.isOpen) {
throw new Error('Port is not open')
}
}
/**
* 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.
* @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected.
*/
async read(buffer, offset, length) {
if (!Buffer.isBuffer(buffer)) {
throw new TypeError('"buffer" is not a Buffer')
}
if (typeof offset !== 'number' || isNaN(offset)) {
throw new TypeError(`"offset" is not an integer got "${isNaN(offset) ? 'NaN' : typeof offset}"`)
}
if (typeof length !== 'number' || isNaN(length)) {
throw new TypeError(`"length" is not an integer got "${isNaN(length) ? 'NaN' : typeof length}"`)
}
debug('read')
if (buffer.length < offset + length) {
throw new Error('buffer is too small')
}
if (!this.isOpen) {
throw new Error('Port is not open')
}
}
/**
* 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.
* @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected.
*/
async write(buffer) {
if (!Buffer.isBuffer(buffer)) {
throw new TypeError('"buffer" is not a Buffer')
}
debug('write', buffer.length, 'bytes')
if (!this.isOpen) {
debug('write', 'error port is not open')
throw new Error('Port is not open')
}
}
/**
* 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 reject.
* @returns {Promise} Resolves once the port's baud rate changes.
* @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected.
*/
async update(options) {
if (typeof options !== 'object') {
throw TypeError('"options" is not an object')
}
if (typeof options.baudRate !== 'number') {
throw new TypeError('"options.baudRate" is not a number')
}
debug('update')
if (!this.isOpen) {
throw new Error('Port is not open')
}
}
/**
* 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.
* @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected.
*/
async set(options) {
if (typeof options !== 'object') {
throw new TypeError('"options" is not an object')
}
debug('set')
if (!this.isOpen) {
throw new Error('Port is not open')
}
}
/**
* Get the control flags (CTS, DSR, DCD) on the open port.
* @returns {Promise} Resolves with the retrieved flags.
* @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected.
*/
async get() {
debug('get')
if (!this.isOpen) {
throw new Error('Port is not open')
}
}
/**
* Get the OS reported baud rate for the open port.
* Used mostly for debugging custom baud rates.
* @returns {Promise} Resolves with the current baud rate.
* @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected.
*/
async getBaudRate() {
debug('getbaudRate')
if (!this.isOpen) {
throw new Error('Port is not open')
}
}
/**
* Flush (discard) data received but not read, and written but not transmitted.
* @returns {Promise} Resolves once the flush operation finishes.
* @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected.
*/
async flush() {
debug('flush')
if (!this.isOpen) {
throw new Error('Port is not open')
}
}
/**
* 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.
* @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected.
*/
async drain() {
debug('drain')
if (!this.isOpen) {
throw new Error('Port is not open')
}
}
}
module.exports = AbstractBinding