node-serialport-bindings-cpp/examples/drain.js
Francis Gulotta 9a98ede77f Remove the serial port factory and throw errors on invalid arguments (#822)
- Constructor now throws errors when given invalid options
 - The Constructor’s callback is given to `.open()` if `openImmediately` is set
 - Depreciation warning when using the old style SerialPort.SerialPort
2016-05-26 11:40:58 -04:00

25 lines
897 B
JavaScript

'use strict';
var SerialPort = require('serialport');
var port = new SerialPort('/dev/cu.Cubelet-RGB');
port.on('open', function() {
var largeMessage = new Buffer(1024 * 10).fill('!');
console.log('Calling write');
port.write(largeMessage, function() {
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(function() {
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', function(data) {
console.log('Received: \t', data);
});