forked from serialport/node-serialport
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialport.js
More file actions
139 lines (116 loc) · 3.72 KB
/
Copy pathserialport.js
File metadata and controls
139 lines (116 loc) · 3.72 KB
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
"use strict";
/*global process require exports console */
// Copyright 2011 Chris Williams <chris@iterativedesigns.com>
var sys = require('sys');
var Buffer = require('buffer').Buffer;
var stream = require('stream');
var fs = require('fs');
var net = require('net');
var serialport_native = require('./serialport_native');
var IOWatcher = process.binding('io_watcher').IOWatcher;
var BAUDRATES = [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];
var PARITY = [0, 1, 2];
var FLOWCONTROL = [0, 1];
var parsers = {
raw: function (emitter, buffer) {
emitter.emit("data", buffer);
},
readline: function (delimiter) {
if (typeof delimiter === "undefined" || delimiter === null) { delimiter = "\r"; }
// Delimiter buffer saved in closure
var data = "";
return function (emitter, buffer) {
// Collect data
data += buffer.toString();
// Split collected data by delimiter
data.split(delimiter).forEach(function (part, i, array) {
if (i !== array.length-1) {
// Fully delimited part. Lets emit it.
emitter.emit('data', part);
}
else {
// Last split part might be partial. We can't emit it just yet.
data = part;
}
});
};
}
};
// The default options, can be overwritten in the 'SerialPort' constructor
var _options = {
baudrate: 38400,
databits: 8,
stopbits: 1,
parity: 0,
flowcontrol: 0,
buffersize: 255,
parser: parsers.raw
};
function SerialPort(path, options) {
options = options || {};
options.__proto__ = _options;
if (BAUDRATES.indexOf(options.baudrate) == -1) {
throw new Error('Invalid "baudrate": ' + options.baudrate);
}
if (DATABITS.indexOf(options.databits) == -1) {
throw new Error('Invalid "databits": ' + options.databits);
}
if (STOPBITS.indexOf(options.stopbits) == -1) {
throw new Error('Invalid "stopbits": ' + options.stopbits);
}
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);
}
stream.Stream.call(this);
this.port = path;
this.fd = serialport_native.open(this.port, options.baudrate, options.databits, options.stopbits, options.parity, options.flowcontrol);
this.readWatcher = new IOWatcher();
this.empty_reads = 0;
this.readWatcher.callback = (function (file_id, me) {
return function () {
var buffer = new Buffer(options.buffersize);
var bytes_read = 0, err = null;
try {
bytes_read = serialport_native.read(file_id, buffer);
} catch (e) {
err = e;
}
if (bytes_read <= 0) {
// assume issue with reading.
me.emit("error", (err ? err :"Read triggered, but no bytes available. Assuming error with serial port shutting down."));
me.close();
} else {
options.parser(me, buffer.slice(0, bytes_read));
}
}
})(this.fd, this);
this.readWatcher.set(this.fd, true, false);
this.readWatcher.start();
}
sys.inherits(SerialPort, stream.Stream);
SerialPort.prototype.close = function () {
this.readWatcher.stop();
if (this.fd) {
serialport_native.close(this.fd);
this.fd = null;
}
};
SerialPort.prototype.write = function (b) {
if (Buffer.isBuffer(b))
serialport_native.write(this.fd, b);
else
serialport_native.write(this.fd, new Buffer(b));
};
SerialPort.prototype.end = function(buf, enc) {
if (buf) {
this.write(buf, enc);
}
this.close();
}
module.exports.SerialPort = SerialPort;
module.exports.parsers = parsers;