Flushing changes for the new flowcontrol handling.

This commit is contained in:
Chris Williams 2013-08-13 14:37:10 -04:00
parent 701675e11e
commit 8bd63d8a24
5 changed files with 101 additions and 36 deletions

View file

@ -1,6 +1,6 @@
{
"name": "serialport",
"version": "1.1.3",
"version": "1.2.0",
"description": "Welcome your robotic javascript overlords. Better yet, program them!",
"author": {
"name": "Chris Williams",

View file

@ -14,10 +14,15 @@ var child_process = require('child_process');
// Removing check for valid BaudRates due to ticket: #140
// var BAUDRATES = [500000, 230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1800, 1200, 600, 300, 200, 150, 134, 110, 75, 50];
var DATABITS = [8, 7, 6, 5];
var STOPBITS = [1, 2, 1.5];
// VALIDATION ARRAYS
var DATABITS = [5, 6, 7, 8];
var STOPBITS = [1, 1.5, 2];
var PARITY = ['none', 'even', 'mark', 'odd', 'space'];
var FLOWCONTROL = [false, true];
var FLOWCONTROLS = ["XON", "XOFF", "XANY", "RTSCTS"];
var parsers = {
raw: function (emitter, buffer) {
@ -48,13 +53,12 @@ var _options = {
baudrate: 9600,
parity: 'none',
rtscts: false,
xonxoff: false,
rts: 'none',
dtr: 'none',
xon: false,
xoff: false,
xany: false,
databits: 8,
stopbits: 1,
flowcontrol: false,
buffersize: 255,
buffersize: 256,
parser: parsers.raw
};
function SerialPort (path, options, openImmediately) {
@ -69,6 +73,9 @@ function SerialPort (path, options, openImmediately) {
// }
stream.Stream.call(this);
if (DATABITS.indexOf(options.databits) == -1) {
throw new Error('Invalid "databits": ' + options.databits);
@ -79,23 +86,43 @@ function SerialPort (path, options, openImmediately) {
if (PARITY.indexOf(options.parity) == -1) {
throw new Error('Invalid "parity": ' + options.parity);
}
if (FLOWCONTROL.indexOf(options.flowcontrol) == -1) {
throw new Error('Invalid "flowcontrol": ' + options.flowcontrol);
}
if (!path) {
throw new Error('Invalid port specified: ' + path);
}
stream.Stream.call(this);
// flush defaults, then update with provided details
options.rtscts = _options.rtscts;
options.xon = _options.xon;
options.xoff = _options.xoff;
options.xany = _options.xany;
if (options.flowControl || options.flowcontrol) {
var fc = options.flowControl || options.flowcontrol;
if (typeof fc == 'boolean') {
options.rtscts = true;
} else {
fc.forEach(function (flowControl) {
var fcup = flowControl.toUpperCase();
var idx = FLOWCONTROLS.indexOf(fcup);
if (idx < 0) {
throw new Error('Invalid "flowControl": ' + fcup + ". Valid options: "+FLOWCONTROLS.join(", "));
} else {
// "XON", "XOFF", "XANY", "DTRDTS", "RTSCTS"
switch (idx) {
case 0: options.xon = true; break;
case 1: options.xoff = true; break;
case 2: options.xany = true; break;
case 3: options.rtscts = true; break;
}
}
})
}
}
options.baudRate = options.baudRate || options.baudrate || _options.baudrate;
options.parity = options.parity || _options.parity;
options.rtscts = options.rtscts || _options.rtscts;
options.xonxoff = options.xonxoff || _options.xonxoff;
options.rts = options.rts || _options.rts;
options.dtr = options.dtr || _options.dtr;
options.dataBits = options.dataBits || options.databits || _options.databits;
options.stopBits = options.stopBits || options.stopbits || _options.stopbits;
options.bufferSize = options.bufferSize || options.buffersize || _options.buffersize;

View file

@ -41,7 +41,12 @@ v8::Handle<v8::Value> Open(const v8::Arguments& args) {
baton->bufferSize = options->Get(v8::String::New("bufferSize"))->ToInt32()->Int32Value();
baton->parity = ToParityEnum(options->Get(v8::String::New("parity"))->ToString());
baton->stopBits = ToStopBitEnum(options->Get(v8::String::New("stopBits"))->ToNumber()->NumberValue());
baton->flowControl = options->Get(v8::String::New("flowControl"))->ToBoolean()->BooleanValue();
baton->rtscts = options->Get(v8::String::New("rtscts"))->ToBoolean()->BooleanValue();
baton->xon = options->Get(v8::String::New("xon"))->ToBoolean()->BooleanValue();
baton->xoff = options->Get(v8::String::New("xoff"))->ToBoolean()->BooleanValue();
baton->xany = options->Get(v8::String::New("xany"))->ToBoolean()->BooleanValue();
baton->callback = v8::Persistent<v8::Value>::New(callback);
baton->dataCallback = v8::Persistent<v8::Value>::New(options->Get(v8::String::New("dataCallback")));
baton->disconnectedCallback = v8::Persistent<v8::Value>::New(options->Get(v8::String::New("disconnectedCallback")));

View file

@ -60,7 +60,11 @@ public:
int baudRate;
int dataBits;
int bufferSize;
bool flowControl;
bool rtscts;
bool xon;
bool xoff;
bool xany;
bool dsrdtr;
SerialPortParity parity;
SerialPortStopBits stopBits;
char errorString[1024];

View file

@ -26,7 +26,6 @@ Boolean lockInitialised = FALSE;
int ToBaudConstant(int baudRate);
int ToDataBitsConstant(int dataBits);
int ToStopBitsConstant(SerialPortStopBits stopBits);
int ToFlowControlConstant(bool flowControl);
void AfterOpenSuccess(int fd, v8::Handle<v8::Value> dataCallback, v8::Handle<v8::Value> disconnectedCallback, v8::Handle<v8::Value> errorCallback) {
@ -98,9 +97,6 @@ int ToDataBitsConstant(int dataBits) {
return -1;
}
int ToFlowControlConstant(bool flowControl) {
return flowControl ? CRTSCTS : 0;
}
void EIO_Open(uv_work_t* req) {
OpenBaton* data = static_cast<OpenBaton*>(req->data);
@ -120,12 +116,7 @@ void EIO_Open(uv_work_t* req) {
return;
}
int flowControl = ToFlowControlConstant(data->flowControl);
if(flowControl == -1) {
snprintf(data->errorString, sizeof(data->errorString), "Invalid flow control setting %d", data->flowControl);
return;
}
int flags = (O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY);
int fd = open(data->path, flags);
@ -155,13 +146,47 @@ void EIO_Open(uv_work_t* req) {
// Removing check for valid BaudRates due to ticket: #140
// #endif
/*
IGNPAR : ignore bytes with parity errors
ICRNL : map CR to NL (otherwise a CR input on the other computer
will not terminate input)
otherwise make device raw (no other input processing)
*/
options.c_iflag = IGNPAR | ICRNL;
// Specify data bits
options.c_cflag &= ~CSIZE;
options.c_cflag |= dataBits;
// Specify flow control
options.c_cflag &= ~CRTSCTS;
options.c_cflag |= flowControl;
if (data->rtscts) {
options.c_cflag &= ~CRTSCTS;
options.c_cflag |= 1;
// evaluate specific flow control options
}
if (data->xon) {
options.c_iflag &= ~IXON;
options.c_iflag |= 1;
}
if (data->xoff) {
options.c_iflag &= ~IXOFF;
options.c_iflag |= 1;
}
if (data->xany) {
options.c_iflag &= ~IXANY;
options.c_iflag |= 1;
}
switch (data->parity)
{
@ -207,13 +232,17 @@ void EIO_Open(uv_work_t* req) {
options.c_cflag |= CLOCAL; //ignore status lines
options.c_cflag |= CREAD; //enable receiver
options.c_cflag |= HUPCL; //drop DTR (i.e. hangup) on close
options.c_iflag = IGNPAR;
// options.c_iflag = IGNPAR;
// Raw output
options.c_oflag = 0;
options.c_lflag = 0; //ICANON;
options.c_lflag = ICANON; //0;
options.c_cc[VMIN]=1;
options.c_cc[VTIME]=0;
sleep(1);
// removed this unneeded sleep.
// sleep(1);
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);