1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 var EventEmitter = require('events').EventEmitter;
17 var util = require('util');
18 var uart = process.binding(process.binding.uart);
21 var BAUDRATE = [0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400
22 , 4800, 9600, 19200, 38400, 57600, 115200, 230400];
23 var DATABITS = [5, 6, 7, 8];
25 var defaultConfiguration = {
32 if (!(this instanceof Uart)) {
37 Uart.prototype.open = function(configuration, callback) {
38 return new UartPort(configuration, callback);
42 function UartPort(configuration, callback) { //constructor
45 if (util.isObject(configuration)) {
46 if (!util.isString(configuration.device)) {
48 'Bad configuration - device is mandatory and should be String');
51 throw new TypeError('Bad arguments - configuration should be Object');
55 if (!util.isUndefined(configuration.baudRate)) {
56 if (BAUDRATE.indexOf(configuration.baudRate) === -1) {
57 throw new TypeError("Invalid 'baudRate': " + configuration.baudRate);
60 configuration.baudRate = defaultConfiguration.baudRate;
64 if (!util.isUndefined(configuration.dataBits)) {
65 if (DATABITS.indexOf(configuration.dataBits) === -1) {
66 throw new TypeError("Invalid 'databits': " + configuration.dataBits);
69 configuration.dataBits = defaultConfiguration.dataBits;
72 EventEmitter.call(this);
74 this._binding = new uart(configuration, this, function(err) {
75 util.isFunction(callback) && callback.call(self, err);
78 process.on('exit', (function(self) {
80 if (!util.isNull(self._binding)) {
87 util.inherits(UartPort, EventEmitter);
89 UartPort.prototype.write = function(buffer, callback) {
92 if (util.isNull(this._binding)) {
93 throw new Error('UART port is not opened');
96 this._binding.write(buffer, function(err) {
97 util.isFunction(callback) && callback.call(self, err);
101 UartPort.prototype.writeSync = function(buffer) {
104 if (util.isNull(this._binding)) {
105 throw new Error('UART port is not opened');
108 this._binding.write(buffer);
111 UartPort.prototype.close = function(callback) {
114 if (util.isNull(this._binding)) {
115 throw new Error('UART port is not opened');
118 this._binding.close(function(err) {
119 util.isFunction(callback) && callback.call(self, err);
121 this._binding = null;
124 UartPort.prototype.closeSync = function() {
125 if (util.isNull(this._binding)) {
126 throw new Error('UART port is not opened');
129 this._binding.close();
130 this._binding = null;
133 module.exports = Uart;