node-serialport-bindings-cpp/examples/drain.js
Francis Gulotta 61e23d0907 [all] Have drain wait for pending JS write operations before calling system drain
This should allow drain to be a lot easier to use.

Bonus:
- More logging!
- Bluebird in tests with it's great long stack traces!
2017-07-16 22:05:58 -04:00

25 lines
985 B
JavaScript

/* eslint-disable node/no-missing-require */
'use strict';
const Buffer = require('safe-buffer').Buffer;
const SerialPort = require('serialport');
const port = new SerialPort('/dev/my-great-device');
port.on('open', () => {
console.log('port opened');
});
const largeMessage = Buffer.alloc(1024 * 10, '!');
port.write(largeMessage, () => {
console.log('Write callback returned');
});
// At this point, data may still be buffered in the os or node serialport and not sent
// out over the port yet. Serialport will wait until any pending writes have completed and then ask
// the operating system to wait until all data has been written to the file descriptor.
console.log('Calling drain');
port.drain(() => {
console.log('Drain callback returned');
// Now the data has "left the pipe" (tcdrain[1]/FlushFileBuffers[2] finished blocking).
// [1] http://linux.die.net/man/3/tcdrain
// [2] http://msdn.microsoft.com/en-us/library/windows/desktop/aa364439(v=vs.85).aportx
});