node-serialport-bindings-cpp/examples/drain.js
Francis Gulotta 6455ecbb51 [unix] New read/write subsystem (#1212)
This has always been the dream. Use `fs.read` and `fs.write` to do reading and writing. To make that happen I've created a cross platform `Poller` object that notifies us once on `readable` and `writable` states.

- Reduce large write cpu usage from 100% to 1-2%
- Replace ReadPoller with a multi purpose poller because you can only have a single uv_poll_t on a fd at a time because reasons
- Experimented with doing this all in c++, maybe there are gains to doing that but I'd never finish.
- Some unrelated eslint changes
- Arduino echo should just echo, writing random bytes was messing it up
- use a file to disable tests in envs
- read and write errors can trigger disconnects
- disconnect event only happens if there are listeners, otherwise error events will be triggered
- a disconnect during write will always have a write error too
- win32 bindings seem to only trigger disconnects on writes
2017-07-05 09:12:31 -04:00

28 lines
948 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/cu.Cubelet-RGB');
port.on('open', () => {
console.log('port opened');
});
const largeMessage = Buffer.from(1024 * 10).fill('!');
port.write(largeMessage, () => {
console.log('Write callback returned');
// At this point, data may still be buffered and not sent out over the port yet
// write function returns asynchronously even on the system level.
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
});
});
port.on('data', (data) => {
console.log('Received: \t', data);
});